How can I avoid autorepeated keydown events in JavaScript?

后端 未结 4 1625
离开以前
离开以前 2020-12-01 07:59

If the user holds down the key, multiple keydown events are fired. For usability reasons I need to use keydown, not keyup, but I want to avoid this situation. My relevant co

4条回答
  •  情歌与酒
    2020-12-01 08:14

    (Copied from another question which turned out to be a duplicate of this one.)

    dmaurolizer's KeyPress library gives fine control over all keyboard events. You provide callback functions for events. The keydown event sends an optional isRepeat param so you can ignore auto-repeats. Here's a sample using that functionality for the thrust control in a video game:

    var my_combos = listener.register_many([
        {
            "keys"          : "up",
            "on_keydown"    : function(e, num, isRepeat) {
                if (isRepeat) return ;
                thrust = 0.1;
            },
            "on_keyup"      : function() {
                thrust = 0;
            }
        }
    ]);
    

提交回复
热议问题