[This question underlies WinAPI programming]
In terms of .NET:
When the Keydown event is fired in WinForms, you can set KeyEventArgs.SuppressKeypress = trueand then the subsequent Keypress / Character will not be sent to the control.
I found the following SO answer that describes how Control does this:
Using the SuppressKeyPress event to block the KeyUp event
But now I don’t understand how this can work correctly? What happens if the program creates a couple of messages about unlocking in the queue, and you missed the first few, but the last one that you suppressed using the method above, will the function not RemovePendingMessagesdelete all characters from the queue, and not just the last?
In terms of the Windows API:
Consider a typical message loop that calls TranslateMessageto receive messages WM_CHARfrom the keyboard. On a WM_KEYDOWNcheck if the key is recognized as a command, in which case it should not generate a character. How could you delete a message WM_CHARthat was potentially sent to the message queue to effectively suppress keystrokes? The existing solution in .NET seems to use looped PeekMessage(.., WM_CHAR, WM_CHAR, PM_REMOVE)to remove all characters from the queue, but would it not delete too many characters if there are several messages with a keyboard in the queue?
source
share