javascript - keydown (repetition) breaks when keyup event (for ANOTHER key) is fired -
this bit of weird 1 figure i'm either missing obvious or flaw how these events implemented in browsers.
let me first summarize example scenario issue comes before providing isolated case-example;
the arrow keys used move player (as handlekeydown function fired anytime key hit anywhere on page). likewise, anytime key released function fired (handlekeyup).
as (the player) hold left key down, handlekeydown function repeatedly triggered (which admittedly think defies you'd expect , name implies, nonetheless, normal behavior across browsers i'm sure know).
so here's happens: player holds direction walk in direction; press number key (for hot-keyed item) continue walk while holding direction down. happens here release number key hot-keyed item, repetition player movement stops!
i've written small isolated example of behavior:
<html> <head> <script type='text/javascript' src='jquery-1.5.1.min.js'></script> <script type='text/javascript'> var log = {}; var keystate = {}; var keycount = {'down': 0, 'up': 0}; window.addeventlistener('keydown', handlekeydown, false); window.addeventlistener('keyup', handlekeyup, false); function handlekeydown (e) { keystate[e.keycode] = true; keycount.down++; log.prepend(" ["+e.keycode+"] "); return false; } function handlekeyup (e) { keystate[e.keycode] = false; keycount.down++; return true; } $(function(){log = $('#log');}); </script> </head> <body> <div id='debug'></div> <div id='log'></div> </body>
you can try here:
press keys on page , see keycode appear on page. hold down key, keycode repeats on , over.
the behavior gets quirky when press down different key while initial key still being held. upon release of recent key, repetition stops first key. interestingly, if try example , hold down 1 key, , hold second key, instead of releasing second, release first: second key continue repeat.
so seems go on when keyup event fired, seems terminate repetition in keydown events fired on earlier key.
i've tried sorts of things ranging using returning false in both event-handling functions, returning true, trying keypress instead of keydown, saving key-states , continuing movement if button's keystate still true, comes down when keyup fired actively propagating keydown events killed. i've read through javascript madness: keyboard events , not seeing specific sort of behavior.
is design flaw implementation of key-events, or missing something? observe same behavior in chrome, ie & firefox.
any advice or input?
that way os/environment works, , result way browser works.
the keydown repetition shouldn't continue firing after keyup key in browser, since doesn't behave way anywhere else. open notepad, or vim, or pico, or whatever text editor use, , perform same sequence of events. you'll see after keyup, first key pressed does not keep printing new letters. that's way these things designed in os, , result that's way they're propogated through editors , browsers etc.
Comments
Post a Comment