Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>They should happen in that order when they happen, in most apps. But your window might not receive every event for every character entered.</p> <p>For example, in Windows, if you hold down a key you'll see a <code>KeyDown</code> and/or <code>KeyPressed</code> repeatedly as the key repeats, and a single <code>KeyUp</code> once the key is released.</p> <h2>Edit:</h2> <p>Now that i think about it, though...Windows starts out only posting <code>WM_KEYDOWN</code> and <code>WM_KEYUP</code> messages to your window (and only if you have focus). <code>WM_CHAR</code> messages (the Win32 analogue to <code>KeyPressed</code>) are posted based on those when the message loop calls <code>TranslateMessage</code>. This means two things:</p> <ul> <li>If the window has a backlog of messages, the API <em>might</em> just add the message to the end of the queue (IDK); this makes more sense to me (the message queue being a <em>queue</em>, after all, and not a stack), but it does mean that if there is already a <code>WM_KEYUP</code> for that key in the queue (for example, if the user hit and released a key while another message was being processed), the corresponding <code>WM_CHAR</code> might appear <em>after</em> it.</li> <li>Languages like C and C++ have more flexibility (read: less built-in automation) in how they handle messages; in order for them to get <code>WM_CHAR</code> messages, they have to either call <code>TranslateMessage</code> explicitly or do the translation themselves (barring another app posting such messages to it). With the latter, there's no telling what order the messages will be posted in.</li> </ul> <p>Also, as mentioned in the comments, if the focus switches while a key is down, you might only see a <code>KeyUp</code> or <code>KeyDown</code>. (The key state was already that way before the focus switch, and Windows probably won't tell you about it.) And dead keys (ones that don't generate a character on their own) won't trigger a <code>KeyPressed</code> at all (though they do typically trigger <code>KeyUp</code> and <code>KeyDown</code>). They typically just wait for the next real character and modify it.</p> <p><strong>Short version of all that</strong>: You can rely on the order in which <code>KeyDown</code> events appear in relation to each other. Same with <code>KeyPressed</code>, and even <code>KeyUp</code>. But, at least in Windows, the three message types are not really coordinated with each other. So don't assume that every <code>KeyUp</code> will match a <code>KeyDown</code> (or vice versa), or that <code>KeyPressed</code> appears before <code>KeyUp</code>.</p> <p>You should probably also decide whether you want to process <em>characters</em> or <em>keys</em>. That's part of the confusion here, i think; the two are actually different concepts, and most of the time you only really care about one or the other. While Win32 would allegedly tell you the key code when you're handling a <code>KeyPressed</code> event, the real intent of that event is to tell you what <em>character</em> was entered. (There's a reason it's called <code>WM_CHAR</code> and not <code>WM_KEYPRESS</code>. :) And why .net's <code>KeyPressEventArgs</code> type only has a <code>KeyChar</code> and not a <code>KeyCode</code>.) The other two treat the keyboard as basically a bunch of buttons.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload