Be a Supporter!

JS: keydown - disable repeating?

  • 737 Views
  • 5 Replies
New Topic Respond to this Topic
uglybetty
uglybetty
  • Member since: Apr. 4, 2009
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
JS: keydown - disable repeating? 2013-05-18 07:03:55 Reply

Is it possible to stop the keydown from executing repeatedly when holding a key down? Like, the code has to execute only one time for every keystroke.

When holding down a key, the score variable is getting + 1 repeatedly.

window.addEventListener("keydown", onKeyDown, false);

function onKeyDown(event) {
    if (event.keyCode == "37") {
        move(1);
        score++;
    } else if (event.keyCode == "39") {
        move(2);
        count++;
    }
    if (event.keyCode == "38") {
        move(3);
        count++;
    } else if (event.keyCode == "40") {
        move(4);
        count++;
    }
}
Dean
Dean
  • Member since: Feb. 16, 2008
  • Online!
Forum Stats
Moderator
Level 47
Gamer
Response to JS: keydown - disable repeating? 2013-05-18 09:51:48 Reply

I wouldn't be surprised if there's a nicer way of handling it, but this is the first solution that popped into my head:

- Store a boolean variable for each of the keys and initialise it as false
- On key down events, set the associated boolean to true. So if up is pressed, set "up" to true
- On key up events, set the associated boolean to false. So if up is released, set "up" to false

This way the booleans represent whether or not the key is currently held down. So on key down events you'd check the keycode and whether or not that button is already pressed before executing the code.

var left, right, up, down;
left = false;
right = false;
up = false;
down = false;

window.addEventListener("keydown", onKeyDown, false);
window.addEventListener("keyup", onKeyUp, false);

function onKeyDown(event) {
	if (event.keyCode == "37" && !left) {
		left = true;
		move(1);
		score++;
	} else if (event.keyCode == "39" && !right) {
		right = true;
		move(2);
		count++;
	}
	if (event.keyCode == "38" && !up) {
		up = true;
		move(3);
		count++;
	} else if (event.keyCode == "40" && !down) {
		down = true;
		move(4);
		count++;
	}
}

function onKeyUp(event) {
	if (event.keyCode == "37") {
		left = false;
	}
	if (event.keyCode == "39") {
		right = false;
	}
	if (event.keycode == "38") {
		up = false;
	}
	if (event.keycode == "40") {
		down = false;
	}
}

BBS + Chat Moderator - Feel free to send me a PM if you have a problem!
Want to instant message me? [ Skype - DeanNewgrounds ]

BBS Signature
uglybetty
uglybetty
  • Member since: Apr. 4, 2009
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to JS: keydown - disable repeating? 2013-05-18 11:12:12 Reply

At 5/18/13 09:51 AM, Dean wrote: I wouldn't be surprised if there's a nicer way of handling it, but this is the first solution that popped into my head:

- Store a boolean variable for each of the keys and initialise it as false
- On key down events, set the associated boolean to true. So if up is pressed, set "up" to true
- On key up events, set the associated boolean to false. So if up is released, set "up" to false

This way the booleans represent whether or not the key is currently held down. So on key down events you'd check the keycode and whether or not that button is already pressed before executing the code.

Thank you so much! I was thinking about doing a check like you did, but it would have been more convenient if it was implemented as a parameter or something.

Patcoola
Patcoola
  • Member since: Mar. 7, 2003
  • Offline.
Forum Stats
Member
Level 60
Animator
Response to JS: keydown - disable repeating? 2013-05-18 15:14:03 Reply

if you want to try without booleans, you can cache the last key down.
example: ondown if cache not keycode then update cache and do something else if cache equal keycode then do nothing.

uglybetty
uglybetty
  • Member since: Apr. 4, 2009
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to JS: keydown - disable repeating? 2013-05-21 03:19:51 Reply

At 5/18/13 03:14 PM, Patcoola wrote: if you want to try without booleans, you can cache the last key down.
example: ondown if cache not keycode then update cache and do something else if cache equal keycode then do nothing.

yes of course and its a fix that is easy.

the thing I want to know IF this is something that is inside the OS itself? and there must be a way to make the code execute once for every keydown without making a lot of trouble coding in booleans.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Online!
Forum Stats
Moderator
Level 13
Programmer
Response to JS: keydown - disable repeating? 2013-05-21 06:46:45 Reply

At 5/21/13 03:19 AM, uglybetty wrote: the thing I want to know IF this is something that is inside the OS itself?

Well you're writing JavaScript so it doesn't matter how the OS behaves since the behaviour of JS is determined by the engine being used (e.g. SpiderMonkey, Rhino, or V8), but the Windows API and X11 do it pretty much the same way: if you hold down a key the key down message will be sent repeatedly to the message loop.

At 5/21/13 03:19 AM, uglybetty wrote: and there must be a way to make the code execute once for every keydown without making a lot of trouble coding in booleans.

Other than what Patcoola said there isn't (though I wouldn't recommend doing it like that unless you know for a fact you'll never, ever, ever need to look up the state of a key).
This sort of thing shouldn't be giving you trouble though; it's really easy.