Click here to Skip to main content
15,038,115 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have my Form1 full of other controls.
This code is working ONLY in the textbox KeyDown event as shown.
It is not working in the Form1 KeyDown event , which it is what I want. If my mouse is over some control inside Form1, KeyDown event doesnt fire.
I only have to specifically click my textbox and then make the keyboard combination and it is working, but only in this manner.
How to make a functionable KeyDown event, indiferent of the controls on Form1?

What I have tried:

        //Refresh Key Ctrl+ R
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        void RefreshKey(KeyEventArgs e)
            if (e.Control & e.KeyCode == Keys.R)
//Thank you !
Updated 1-Aug-21 22:13pm

This is a very old problem, full of answers online. Try this Stack Overflow answer (c# - Keydown Event not firing - Stack Overflow[^]). You have to register the KeyDown event with the form itself & set its KeyPreview property to true. If you google it, you'll find dozens of solutions.

Key preview is a Microsoft answer to this Windows form event propagation problem back in the 90's, if you are curious about the history of Windows programming from its very beginning. It was first seen in 1991, I believe, very well-advertised in Turbo C++ 3.0 (a Borland product), MFC & VB 2 & 3. You still can find them with Google. Anyways. History is boring. I just want to let you know that history is the answer to every convoluted sick design.
_Q12_ 1-Aug-21 14:28pm
I googled the problem, but I didn't know what to search exactly.
Thank you! (both)
It resolved the problem now.
Though with a little exception: another textbox was set to "Read Only=true" and(again) it did not let the event fire. But the rest of the controls are no problem anymore.
My solution was to make the textbox back as "Read Only=false" as originally was, though I would had like it read only= true. I can survive i suppose.
Code Fan 1-Aug-21 15:49pm
You may want to disable all your read-only controls, just so they won't receive focus anymore. Lastly, set the tab indices for all your controls. I don't do Windows programming at all, as I am a backend guy, all about performance & scalability. However, if I were you, I would leverage menu key shortcuts instead of wiring KeyDown events myself. Menus never fail.
You setup the KeyDown event only on the TextBox.

The Form has it's own KeyDown event. Wire that event up and in the properties of your form, set the KeyPreview property to true. Now the event will fire with the focus anywhere on the form.

BUT! You have to be a little careful with this. If you don't set the event arguments Handled properly correctly, your controls might not get the KeyDown event and you'll be wondering why.

Read: Form.KeyPreview Property (System.Windows.Forms) | Microsoft Docs[^]
Just throwing this out there as well, it looks like you're adding this event handler in order to call a ToolStripMenuItem command. Are you aware that there is a ShortcutKeys[^] property you can assign to a menu item and it registers it as a shortcut key on the form?
_Q12_ 2-Aug-21 13:10pm
I was not aware of that. Thank you, I will try it.
_Q12_ 2-Aug-21 14:04pm
Mister Chris, this was very good hint!
Im starting to dig into this new way of assigning shortcut keys.
Is the menustrip cleaned of all the bugs you usually encounter in the KeyDown or KeyPress events ? For sure it looks and feels like it is !!!
Super answer !
Thank you very much !
Chris Copeland 3-Aug-21 4:14am
You are very welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900