KeyPress event not responding to some keys (up, down, left, right, home...) - c#

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.

Related

PreviewKeyDown-Event not always firing when ALT key is pressed

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?

DataGridViewTextBoxCell in edit mode steals keystrokes from Form even when using Form.KeyPreview

I have:
this.KeyPreview = true;
set in my Form, which allows me to typically receive KeyDown events even if a child element has focus. However, I have a DataGridView containing DataGridViewTextBoxCell's and when I click them, I enter "edit mode" for the cell and this somehow has such exclusive access to the keyboard that I can no longer receive these key presses. As long as anything in my entire Form has focus I want my keyboard functionality to actually work.
Does anyone know a way of getting these KeyDown events anyway?
Try handling the KeyUp event instead of KeyDown

intercepting KeyDown event on a WinForm when Trackbar exists

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

Problems with the KeyPress, KeyDown and KeyUp events in C#

So how to begin.
I am asking when you make a game for example and you add a controlls how do I make the form's key events run when the focus isnt on the form but on some of the controlls and when i call this.Focus() or this.Select() it doesnt't happen anything, but if I use a empty form(with no controls) it works(the events respond).And when i have for example 2 buttons and call button1.focus() and press a key the button1's event handler responds (only it) adn then when i call button2.Focus() it responds for button2.How is focus distributed througt the controlls?
I know for muttons you have to click them once to put the focus on them automaticly and for textboxtes too,but when i click the form it doesnt move the focus on the form.
Im have almost no experience with Key events please explayn me how to use them and how they function understandly.
PS: sorry for the long questin
You can set the Form.KeyPreview to true to have the form react to key events.

keyboard events in .NET

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

Categories