-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeyboard.js
51 lines (45 loc) · 1.02 KB
/
keyboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
To read a key: user must check if control[0]==1, then read from control[1] and clear control[0]
*/
function Keyboard()
{
this.control = new Uint8Array(2);
this.clear();
}
Keyboard.prototype.getControlBuffer = function()
{
var k = this;
return {memory:this.control, handler:this, size:this.control.length };
}
Keyboard.prototype.onBeforeMemoryRead = function(address,size)
{
if (address==1)
{
if (this.keyEvents.length>0)
{
var v = this.keyEvents.shift();
this.control[1] = (v.key)>>>0;
}
else
{
this.control[1] = 0;
}
}
else if (address==0)
{
this.control[0] = this.keyEvents.length;
}
}
Keyboard.prototype.clear = function()
{
this.keyEvents = [];
for (var i=0;i<this.control.length;i++) this.control[i]=0;
}
Keyboard.prototype.onkeypress = function(key)
{
this.keyEvents.push({"key":key});
}
Keyboard.prototype.hasPendingIRQ = function()
{
return (this.keyEvents.length>0);
}