Using keyup makes no sense and is inconsistent with user expectations. For triggering actions (which includes normal typing), you only ever use keydown. (Well, there’s one exception for reasons unclear to me: activating a button by pressing Space. That triggers on keyup like how clicks are on release, while Enter triggers on keydown.) Keyup is limited to things where you’re constantly reacting to the state of a key, as is common in games.
This affects the functionality, too. It is in fact introducing latency by using keyup instead of keydown. Feels bad.
For triggering actions (which includes normal typing), you only ever use keydown.
I think you have this wrong, actions generally occur on button release, until that you can move the mouse cursor to a different target, tab to another control, use [ESC] to cancel, and so on. Typing, moving a slider with the cursor keys, and similar things that make use of key repetition while holding down the key are the exception to this.
I am speaking specifically of keyboards. For mice, it’s release. (When I made a similar comment two months ago on Lobsters when this article came up there, I mentioned this, but decided to leave it out this time, thinking the context was clear enough. I think I was wrong.)
If it matters, it seemed pretty clear to me. You're also explicit about it, so I'm not sure how one would think you were referring to mouse input. Unless people type by clicking on a virtual keyboard? Benefit of the doubt can only stretch so far.
Can you give some specific examples? In my experience (which I also verified to make sure), all of the things you mention act on keydown. The exceptions are on multi-key combinations, which follow a special key that is held down while modifiers are entered, where the action is completed by releasing this special key (ALT + TAB [...TAB | SHIFT]) being a common example.
TAB is otherwise immediate, as is ESC, and the examples of typing, as well. So I'm left wondering, how do they have it wrong?
Non latin characters are sometimes inputted by holding down a key. For example on my keyboard holding down z shows ź,ž and ż with a corresponding number to select which one I would like to type. I haven't checked but one would assume if you're detecting keydown the input at that time will be z, and keyup would record ž (or whatever was selected).
Physical keyboards and virtual keyboards are different. Traditionally, ź and ż (my keyboard doesn't have ž) are inputted on keydown X or keydown Z while Alt modifier is active.
Key down is used for events that can be duplicated by holding the key. Eg in a text editor, when you want multiples of the same letters, you’d press and hold the key.
Key up is used for when it’s important to only have one occurrence of that event.
Technically you could write code that made key down only react once. But the logical separation makes some sense.
Examples where key down is used as an input event?
Basically anything that requires text input from OS-native text input fields to IDEs, word processors, spreadsheets, web browsers, terminal emulators and so on and so forth.
Your message was ambiguous to me. But I will admit that I was also quite hungover so it’s possible that the error was mine.
In answer to your question: I’ve only really used keyup for either one time events (like keyboard shortcuts for UI elements) and for ending the status of repeated events (eg movement in games, dragging UI elements, etc).
I think if you were to search for key up events in GitHub, most of references might likely be games and other visual interactions rather than text entry.
As a user, I’ve not thought too much about this before now. I agree with you mostly, but the keyup on space behaviour actually feels so innate I’d hate any change to it. Keydown on space is “jump”; nothing else.
I’d just like to hear an explanation of why it is. Because if you handled “is key currently pressed” events with polling rather than events (which is how people almost always consume them logically), it would be the only thing in the entire world that I can think of that would/should use keyup.
Edit: I think people are misunderstanding me. I’m asking for an explanation of why, when you have a button focused, pressing Space activates on key up, rather than key down like literally everything else on the keyboard, including Enter on a focused button. (Mouse activates on release.)
Because in those contexts, the Space key is being used as a physical mirror of the on-screen button, and on-screen buttons are UX'd to perform action when released.
These two interactions should behave identically:
- Hover over a button, press mouse button 1, release button 1.
- Tab over to a button, press spacebar, release spacebar.
Why on-screen buttons are UX'd to behave this way is a logical follow-up question. I'd wager that it gives a means to bail out of the clicking (e.g. by moving the mouse out of the on-screen button before releasing mousebutton 1, or by pressing Esc while having spacebar still depressed).
Yes, the bailing-out explanation is right. With a mouse it's intentional that you should be able to push the pointer off the button with the mouse button still pressed to cancel.
Under Linux, Firefox, Chromium and GNOME Web (which is WebKit-based; a.k.a. Epiphany) all have Tab cancel but not Esc.
The GTK 4 behaviour feels very much like a bug. I can reproduce it in gtk4-demo and in gtk3-demo. It leads to obviously-nonsensical behaviour, including messed up window focus. I’m tempted to file a bug report, but haven’t had such productive experiences with filing things on GNOME historically as with Firefox or even Chromium.
Why should Enter being a different key be a different discussion? They’re both used to activate buttons, and one is behaving like every single other keyboard interaction, while the other isn’t. (Related: if a link is focused, Enter activates it, Space doesn’t, just doing the normal scroll-down-one-page.)
It just matches real life. What about lmb to shoot? You don't fire a gun on trigger release, you fire it on trigger down. Walking? Would be weird moving only on keyup.
This raises the question of why real life buttons and virtual buttons behave differently. My unsubstantiated guess is that clicks act on release to give the opportunity to slide off the button to abort, and/or because the button would disappear while pressing if it ex. submits a form.
Games use keydown because it is more responsive and control responsiveness is vital to dynamic gameplay feeling good. There are some exceptions - if action can be charged, then it is on keydown. And then on gamepad sometimes actions are on release when developer has too many actions and not enough buttons, so it can be keyup vs hold.
I may be wrong on this, but I think that pressing esc while space is held down cancels the button press, like moving the mouse off the key while holding down the click does. I'd need to test this, though.
That’s where the input event is really useful as it allows for more input options than just keyboard and then you don’t have to deal with figuring out if it’s down or up.
On my own computer I can open a context menu and select an action with a single click. Whenever I need to use MS Windows, I always need two and I find that infuriating every time.
Comments
Using keyup makes no sense and is inconsistent with user expectations. For triggering actions (which includes normal typing), you only ever use keydown. (Well, there’s one exception for reasons unclear to me: activating a button by pressing Space. That triggers on keyup like how clicks are on release, while Enter triggers on keydown.) Keyup is limited to things where you’re constantly reacting to the state of a key, as is common in games.
This affects the functionality, too. It is in fact introducing latency by using keyup instead of keydown. Feels bad.
For triggering actions (which includes normal typing), you only ever use keydown.
I think you have this wrong, actions generally occur on button release, until that you can move the mouse cursor to a different target, tab to another control, use [ESC] to cancel, and so on. Typing, moving a slider with the cursor keys, and similar things that make use of key repetition while holding down the key are the exception to this.
I am speaking specifically of keyboards. For mice, it’s release. (When I made a similar comment two months ago on Lobsters when this article came up there, I mentioned this, but decided to leave it out this time, thinking the context was clear enough. I think I was wrong.)
If it matters, it seemed pretty clear to me. You're also explicit about it, so I'm not sure how one would think you were referring to mouse input. Unless people type by clicking on a virtual keyboard? Benefit of the doubt can only stretch so far.
Can you give some specific examples? In my experience (which I also verified to make sure), all of the things you mention act on keydown. The exceptions are on multi-key combinations, which follow a special key that is held down while modifiers are entered, where the action is completed by releasing this special key (ALT + TAB [...TAB | SHIFT]) being a common example.
TAB is otherwise immediate, as is ESC, and the examples of typing, as well. So I'm left wondering, how do they have it wrong?
Non latin characters are sometimes inputted by holding down a key. For example on my keyboard holding down z shows ź,ž and ż with a corresponding number to select which one I would like to type. I haven't checked but one would assume if you're detecting keydown the input at that time will be z, and keyup would record ž (or whatever was selected).
Physical keyboards and virtual keyboards are different. Traditionally, ź and ż (my keyboard doesn't have ž) are inputted on keydown X or keydown Z while Alt modifier is active.
The GP means that, if you hold space down on a button, you can then tab to another button without activating the first one.
Because they listed the exceptions to the "act on keyup" rule
If only one case follows your rule, and everything else ever implemented from the start of personal computers is an exception, your rule is awry.
Key down is used for events that can be duplicated by holding the key. Eg in a text editor, when you want multiples of the same letters, you’d press and hold the key.
Key up is used for when it’s important to only have one occurrence of that event.
Technically you could write code that made key down only react once. But the logical separation makes some sense.
Give me an example. Any example beyond this Space one we’re discussing; because I don’t know of any.
Examples where key down is used as an input event?
Basically anything that requires text input from OS-native text input fields to IDEs, word processors, spreadsheets, web browsers, terminal emulators and so on and so forth.
Key UP!
(I confess I’m getting frustrated at the ridiculously large fraction of messages in this thread that have somehow completely misunderstood things.)
Your message was ambiguous to me. But I will admit that I was also quite hungover so it’s possible that the error was mine.
In answer to your question: I’ve only really used keyup for either one time events (like keyboard shortcuts for UI elements) and for ending the status of repeated events (eg movement in games, dragging UI elements, etc).
I think if you were to search for key up events in GitHub, most of references might likely be games and other visual interactions rather than text entry.
I can see both viewpoints here and don’t have a strong personal view
But for reference browsers fire the keypress event on keyup
https://w3c.github.io/uievents/#event-type-keypress
The keypress event is an ancient mistake that I think never had any legitimate use case, and has been deprecated for quite some time now.
As a user, I’ve not thought too much about this before now. I agree with you mostly, but the keyup on space behaviour actually feels so innate I’d hate any change to it. Keydown on space is “jump”; nothing else.
I’d just like to hear an explanation of why it is. Because if you handled “is key currently pressed” events with polling rather than events (which is how people almost always consume them logically), it would be the only thing in the entire world that I can think of that would/should use keyup.
Edit: I think people are misunderstanding me. I’m asking for an explanation of why, when you have a button focused, pressing Space activates on key up, rather than key down like literally everything else on the keyboard, including Enter on a focused button. (Mouse activates on release.)
Because in those contexts, the Space key is being used as a physical mirror of the on-screen button, and on-screen buttons are UX'd to perform action when released.
These two interactions should behave identically:
- Hover over a button, press mouse button 1, release button 1.
- Tab over to a button, press spacebar, release spacebar.
Why on-screen buttons are UX'd to behave this way is a logical follow-up question. I'd wager that it gives a means to bail out of the clicking (e.g. by moving the mouse out of the on-screen button before releasing mousebutton 1, or by pressing Esc while having spacebar still depressed).
Yes, the bailing-out explanation is right. With a mouse it's intentional that you should be able to push the pointer off the button with the mouse button still pressed to cancel.
(Esc while Space is held doesn’t cancel, but Tab does.)
You’re only answering half of the inconsistency: because Enter activates buttons like any other key, on keydown.
While spacebar is depressed:
Qt: Esc cancels. Tab cancels.
Gtk-4: Spacebar auto-clicks before keyup (probably due to auto-repeat).
Win32: Esc sends Esc to the window. Tab cancels.
Firefox: Esc does nothing. Tab cancels.
So, mileage does vary.
Re. Enter key, that's a different key, so different discussion.
Under Linux, Firefox, Chromium and GNOME Web (which is WebKit-based; a.k.a. Epiphany) all have Tab cancel but not Esc.
The GTK 4 behaviour feels very much like a bug. I can reproduce it in gtk4-demo and in gtk3-demo. It leads to obviously-nonsensical behaviour, including messed up window focus. I’m tempted to file a bug report, but haven’t had such productive experiences with filing things on GNOME historically as with Firefox or even Chromium.
Why should Enter being a different key be a different discussion? They’re both used to activate buttons, and one is behaving like every single other keyboard interaction, while the other isn’t. (Related: if a link is focused, Enter activates it, Space doesn’t, just doing the normal scroll-down-one-page.)
It seems that you answered your own question: different keys, different purposes.
Enter is for "execute the focused action". Spacebar is for "operate the button".
Just diving in without full context, but, enter is often a completion action. Historically often a full carriage return. It often means "submit data".
So this could be the oddity
It just matches real life. What about lmb to shoot? You don't fire a gun on trigger release, you fire it on trigger down. Walking? Would be weird moving only on keyup.
This raises the question of why real life buttons and virtual buttons behave differently. My unsubstantiated guess is that clicks act on release to give the opportunity to slide off the button to abort, and/or because the button would disappear while pressing if it ex. submits a form.
Jump is keydown because many games let you adjust your jump height based on how long you hold down space after the jump starts.
Games use keydown because it is more responsive and control responsiveness is vital to dynamic gameplay feeling good. There are some exceptions - if action can be charged, then it is on keydown. And then on gamepad sometimes actions are on release when developer has too many actions and not enough buttons, so it can be keyup vs hold.
I may be wrong on this, but I think that pressing esc while space is held down cancels the button press, like moving the mouse off the key while holding down the click does. I'd need to test this, though.
That’s where the input event is really useful as it allows for more input options than just keyboard and then you don’t have to deal with figuring out if it’s down or up.
this is backwards. Go on.... try it anywhere. Just the start menu (if you're on windows) is enough.
Please reread.
On my own computer I can open a context menu and select an action with a single click. Whenever I need to use MS Windows, I always need two and I find that infuriating every time.