简体   繁体   中英

jquery keydown event detector not detecting keypresses?

I've been making another project, and I was using something that was working fine in a previous code: $(document).keydown(function(keypressed){});, something I used to detect certain keys being pressed. However, though it still works in my previous project, in this new one it seems to read over it once and then not detect when a key is down. Does anyone know what's up?

 var plrPosTest = 6; $(document).keydown(function(keyPressed){ if (keyPressed.keyCode == 65 && plrPosTest != 1){ // A plrPosTest -= 1; } else if (keyPressed.keyCode == 68 && plrPosTest != 6){ // D plrPosTest += 1; } console.log(plrPosTest); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Note: I am linking the jQuery library properly.

Apparently; it wasn't the keydown event detector, but rather (in the original code, in the A & D if statements), there was a check for a Boolean (which I had forgotten about), which was set to true (and the if statements were checking for false).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM