I'm trying to detect keypresses only when a checkbox is checked, however after the first activation of the checkbox it always runs its functions.
Here is "my" code:
var keys = {};
$(":checkbox").click(function() {
if(this.checked) {
$(document).keydown(function (e) {
e.preventDefault()
keys[e.which] = true;
printKeys(e);
});
$(document).keyup(function (e) {
e.preventDefault()
delete keys[e.which];
printKeys(e);
});
}
What is inside if(this.checked)
will only get executed once the checkbox is checked, but after that it will always detect my key presses regardless of the checkbox being checked or not.