The key up event gets triggered when a key is released. This holds true for normal keys like a, b etc. However, holding down the arrow key or escape key produses different results.
Instead of firing a key up event when the key is released, it gets fired soon after the key down event. So, holding down a arrow key becomes equal to pressing and releasing the key many times very fast.
Any explaination and work around to determine when the key is actually released?
The KeyUp event isn't actually fired (for all keys not just arrow keys etc.) until you release the key, there are just many repeated KeyDown events. At least, that's the way it looks to me from some test code.
My workaround is to disable the KeyDown handler after a KeyDown event is detected using
RemoveHandler Me.KeyDown, AddressOf Form1_KeyDown
and then re-enabling when the KeyUP event is fired.
AddHandler Me.KeyDown, AddressOf Form1_KeyDown
Of course if you want to handle simultaneous multiple key presses then this won't work. You'll have to store whether or not the Key in question is already down and ignore the respective KeyDown event.
I've just run into similar problem. Keyboard handler behaves weird:
If focus and handlers are in textbox:
When alphanumeric key is held down, I got multiple Press and Down events
Arrows and function keys produce multiple Down events
If handling events for a form with KeyPreview enabled, only KeyUp event for the arrow keys is handled.
It's possible to handle all keys on lower level by overriding ProcessCmdKey.
If you want to handle a left arrow key:
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
Select Case keyData
Case Keys.Left
Debug.WriteLine("Left")
Return True
Case Else
Return MyBase.ProcessCmdKey(msg, keyData)
End Select
End Function
Related
Background: I am trying to display all available keyboard shortcuts when the user presses the Alt key - similar to the behavior in Office 2010.
But I am having trouble to detect when the Alt key is pressed because the PreviewKeyDown-Event is not always fired. More precisely: It is fired when the key is pressed for the first time, but when I release it and press it again, the event is not fired. That means, it is only fired every second time. I assume that this is related to the fact that the Alt key is a special system key and triggers the window's main menu which somehow swallows the second event. Interestingly, the event does work for the right Alt key. Also, the PreviewKeyUp-Event is always fired for both keys (left and right).
I have also tried to handle the WM_KEYDOWN and WM_SYSKEYDOWN messages directly, but the behavior is exactly the same.
I managed to get the desired behavior when I registered for the InputManager.Current.PostNotifyInput event, but this event is fired pretty often - even for every mouse movement etc. and I would like to avoid this overkill.
Any ideas how I can always be notified when the Alt key is pressed? How does Office 2010 do this? Maybe, is there an event which notifies if the window's menu is activated by pressing the Alt key?
Using C#, .NET 4, WPF.
I have a Telerik rich text control that is losing certain key events (tab, backspace, delete, and the arrow keys are specifics).
For debugging purposes I have added handlers for PreviewKeyDown, KeyDown, CommandExecuting, and DocumentContentChanged. The behavior presents both with and without the handlers present, in both DEBUG and RELEASE mode.
If I press a key other than those listed above I get the events in the order listed above. As an example, if I press the 'a' key I get PreviewKeyDown, KeyDown, CommandExecuting, and DocumentContentChanged.
If I press the right arrow key I get PreviewKeyDown and no other of the events fire.
My suspicion is that there is something trapping the KeyDown event at some point in the message chain before it gets to me and setting e.Handled = true.
Is there any tool available that would allow me to detect the KeyDown event and see in what code it's e.Handled is modified? I know I'm stretching here...
Thanks!
rjsjr
You could use Snoop. It can tell you, which Element set handled = true.
If you need to process these events, you can use EventManager.RegisterClassHandler().
This is ridiculous. I have a KeyDown event I am interested in(to get DownArrow Key event) for a WinForm. I added a trackbar, which gets Autofocus(I dont know how). And now, when I press the DOWN arrow key - it automatically changes the value of the Trackbar and my code for the Winform is not working. I tried HIDING the Trackbar with a button but to no avail. I even have
e.SuppressKeyPress = true;
in my Form1_KeyDown() handler.
Help, I am going haywire.
You can override ProcessCmdKey method. check out below links for more information.
Up, Down, Left and Right arrow keys do not trigger KeyDown event
http://www.getdotnetcode.com/gdncstore/free/Articles/Overriding%20a%20Controls%20ProcessCmdKey%20Function.htm
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.processcmdkey(v=vs.85).aspx
I'm doing a C # application for Windows that will detect when a key is no longer pressed.
So how can I do this?
PS: I know how to detect when a key is pressed.
Thanks for your atention.
You handle the KeyDown event to detect when the key goes down, and then handle the KeyUp event to detect when it goes back up.
I have a windows form and an OnKeyPress function. I also have KeyPreview turned on.
The function responds to most keys except up, down, left, right, home, pgdown, end and so on...
It's like whenever I press those keys, it doesn't send them to the form, it just toggles between the buttons and the other controls.
Anyone experienced that and can help out?
From MSDN
The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events.