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
Related
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().
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
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.
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.
I have succesfully replaced the Windows Shell following the approach given in this SO question.
However, I have detected that the first key press is lost and is a bit of an annoyance for the user. The solution we have tried is to activate the form in the OnShown event:
private void OnShownLoginForm(object z_sender, EventArgs z_e)
{
Activate();
m_loginTextBox.Focus();
}
But this hasn't solved the problem. Do you have any clue of what is happening?
You could try using the System.Windows.Forms.SendKeys Class (MSDN Documentation) to send a key press event to the form when in the Form Load event. If that doesn't help, try sending the keyboard events in the Form.Shown event since Form.Shown is the last event in the form start-up process.
Sounds like something caused by maybe another control getting focus first. Does the textbox have a taborder, and can you set it to 0? Focus should then be on it after the form loads.
Otherwise try creating a new form to test with, it really doesn't seem reproducible.
I do not know if it is related but I had a similar problem where the tabindex property of a webform did not work by pressing the TAB key, after focusing on the first input at page load, until the user first clicked on the form with the mouse.
I did not have access to the source code so I tried solving it with javascript.
Until the first mouse click, all keyboard strokes, including the TAB key, activated the keypress event, TAB key was undetected by keydown/keyup on page load.
I learned that the TAB key activated the keypress event and I could access the keycode through it. simply registering the keypress event and manually switching to the next input with jQuery worked.
after the first mouse click the form behaved as expected, TAB key was no longer caught by keypress event.
here is a code sample:
function tabNext(e){
if(e.keyCode == 9){
// do work
}
}
$('input').keypress(tabNext);