Using Javascript / jQuery, how can I automatically repeat keydown events or their equivalents when someone holds a key?
What I really want is to check if the key is locked, but from other questions here it seems that this is not possible. The proposed solution to the problem is to record keypress and keyup events, and then, provided that the key does not work if the keydown event was recorded and subsequent activation of the keyboard.
In my case, the problem is resolved. I am developing an online experiment. It is assumed that the user must hold down the "T" key for the entire experiment, never release it. An experiment consists of several tests, and each test does not have access to the information recorded in previous tests. Thus, trial version 1 can record keydown for T, but trial version 2 will not have access to this record and, therefore, will not know whether T was down or not.
Now, if holding down the T key would automatically repeat the keydown events for T, I would not have a problem because trial version 2 just caught the next keydown event so that T appears. But it looks like I am not getting auto-repeat of keydown events by holding down the key, at least in Firefox. From what I see, it seems that there is a difference in how different browsers handle keystrokes. What is a good cross-browser way to solve my problem?
By the way, if that matters, I also need to be able to detect keyup and keydown events for other keys while this all happens.
EDIT: after reading some comments, I came back and confirmed that I was really repeating key change events under normal circumstances. But I really do not understand them in the specific situation in which I need them. I have simple code that I think isolates the problem:
<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> </head> <body> <div id="target"></div> </body> <script type="text/javascript"> var i; function foo() { i++; $('#target').html(i); } function doTrial() { </script> </html>
If you press and hold the key, then release and then press again, the behavior will be as expected, i.e. there will be a counter that increases while holding the key, disappears when it is released, and then starts to increase again when it is pressed again.
But if you press TWO keys down and then release ONE, I would think that another (not issued) key would continue to send blocking events so that the counter (after reset) continued to increase. This does not actually happen. Any idea why and how to do this?