我正在研究一种简单的键盘记录器,它使用javascript记录用户击键,我可以将其用于各种应用程序。我有一个主类,它有一个对象,它基本上是一个子对象列表,对应键盘上的各种键。每个条目包含键的ID和表示是否按下它的布尔值。我在类中也有一个init函数,在所有加载后立即调用。并且init类基本上通过输入对象运行,并将其与任何键盘事件进行比较,以查看是否有任何ID匹配。我能够在init函数的顶部引用该对象,但是当我尝试在事件函数中引用输入对象时,它返回undefined。我完全不知道为什么会这样,我会非常感谢一些帮助。
function Controls() {
this.input = {
A: {ID: 65, active: false},
B: {ID: 66, active: false},
C: {ID: 67, active: false},
D: {ID: 68, active: false},
E: {ID: 69, active: false},
F: {ID: 70, active: false},
G: {ID: 71, active: false},
H: {ID: 72, active: false},
I: {ID: 73, active: false},
J: {ID: 74, active: false},
K: {ID: 75, active: false},
L: {ID: 76, active: false},
M: {ID: 77, active: false},
N: {ID: 78, active: false},
O: {ID: 79, active: false},
P: {ID: 80, active: false},
Q: {ID: 81, active: false},
R: {ID: 82, active: false},
S: {ID: 83, active: false},
T: {ID: 84, active: false},
U: {ID: 85, active: false},
V: {ID: 86, active: false},
W: {ID: 87, active: false},
X: {ID: 88, active: false},
Y: {ID: 89, active: false},
Z: {ID: 90, active: false},
ZERO: {ID: 48, active: false},
ONE: {ID: 49, active: false},
TWO: {ID: 50, active: false},
THREE: {ID: 51, active: false},
FOUR: {ID: 52, active: false},
FIVE: {ID: 53, active: false},
SIX: {ID: 54, active: false},
SEVEN: {ID: 55, active: false},
EIGHT: {ID: 56, active: false},
NINE: {ID: 57, active: false},
GRAVE: {ID: 192, active: false},
DASH: {ID: 189, active: false},
EQUALS: {ID: 187, active: false},
OPENBRAKET: {ID: 219, active: false},
CLOSEBRACKET: {ID: 221, active: false},
BACKSLASH: {ID: 220, active: false},
SEMICOLON: {ID: 186, active: false},
QUOTE: {ID: 222, active: false},
COMMA: {ID: 188, active: false},
PERIOD: {ID: 190, active: false},
FORWARDSLASH: {ID: 191, active: false},
F1: {ID: 112, active: false},
F2: {ID: 113, active: false},
F3: {ID: 114, active: false},
F4: {ID: 115, active: false},
F5: {ID: 116, active: false},
F6: {ID: 117, active: false},
F7: {ID: 118, active: false},
F8: {ID: 119, active: false},
F9: {ID: 120, active: false},
F10: {ID: 121, active: false},
F11: {ID: 122, active: false},
F12: {ID: 123, active: false},
ESCAPE: {ID: 27, active: false},
INSERT: {ID: 45, active: false},
DELETE: {ID: 46, active: false},
BACKSPACE: {ID: 8, active: false},
TAB: {ID: 9, active: false},
CAPSLOCK: {ID: 20, active: false},
SHIFT: {ID: 16, active: false},
CONTROL: {ID: 17, active: false},
WINDOWSKEY: {ID: 91, active: false},
ALT: {ID: 18, active: false},
ENTER: {ID: 13, active: false},
WINDOWSMENU: {ID: 93, active: false},
UPARROW: {ID: 38, active: false},
LEFTARROW: {ID: 37, active: false},
DOWNARROW: {ID: 40, active: false},
RIGHTARROW: {ID: 39, active: false},
SPACEBAR: {ID: 32, active: false}
}
this.init = function() {
document.onkeydown = function() {
var event = window.event;
console.log(this.input);
for (i in this.input) {
if (this.input[i].ID.toString() == event.keyCode) {
this.input[i].active = true;
}
}
}
document.onkeyup = function() {
var event = window.event;
for (i in this.input) {
if (this.input[i].ID.toString() == event.keyCode) {
this.input[i].active = false;
}
}
}
}
}