Hey,
Can anyone explain to me why this super simple script ain't working? I
want to listen for key strokes until the user presses the Esc-button.
What am I doing wrong?
// Test Class
var KeyboardListener = new Class({
logging: true,
initialize: function() {
this.startListening();
},
log: function() {
if (this.logging) {
if (window['console']) {
console.log($A(arguments).join('\n'));
}
else {
alert($A(arguments).join('\n'));
}
}
},
startListening: function() {
this.log('start listening ...');
window.addEvent('keydown', this.keydownHandler.bindWithEvent
(this));
},
keydownHandler: function(e) {
if (e.key == 'esc') {
this.log(e.key, 'stop listening ...');
window.removeEvent('keydown', this.keydownHandler);
}
else {
this.log(e.key);
}
}
});
// Implementation
window.addEvent('domready', function() {
new KeyboardListener();
});
Thanks!
/Martin