Which event type is preferable for keyboard interactions, "key up" or "key down"?
When implementing keyboard interactions, platforms provide two kinds of event: "key up" and "key down". For example:
- The browser provides
keyup
andkeydown
. - macOS provides
NSEvent.EventType.keyUp
andNSEvent.EventType.keyUp
. - Windows provides
WM_KEYDOWN
andWM_KEYUP
.
Therefore, when implementing a keyboard interaction, the developer must choose when the interaction is fired. For example:
- In a slideshow app, the user can use the arrow keys to move through the slideshow. But should it go to the next slide when the right arrow key is pressed down, or when it is released again?
- In a video player, the user can hit the spacebar to pause/resume. But should it pause when the spacebar is pressed down, or when it is released again?
The universal provision of both "key up" and "key down" events suggests that the answer is not simple: that, for some situations, "key down" is more appropriate, but for others, "key up" is better.
However, I can find no guidance on this. There are many descriptions of the meaning of "key up" and "key down", but none on how to tastefully choose between them for a given use-case.
In what situations should I prefer a "key up" event? In what situations should I prefer "key down"?