0

I want to detect pressed key on my form. Let's say I want to show message when F1 key is pressed.

I added code to KeyUp event related with Form1

private void Form1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.F1)
    {
        MessageBox.Show("Key Pressed ");
    }
}

The thing is, that pressing F1 does not do anything. Not at all. Does anybody have any idea why does that not work?

Solution

I had to change Form1 KeyPreview properties to "true" and delete this.Controls.Add(this.webControl1); from InitializeComponent() in Form1.Designer.cs

Does anybody know how to solve my problem without deleting row from InitializeComponent()?

Community
  • 1
  • 1
audiophonic
  • 171
  • 1
  • 13

3 Answers3

2

If you want to handle the key press event irrespective of the control that currently has the focus then you should consider override the Control.ProcessCmdKey() method.

Here is a example: How do I capture Keys.F1 regardless of the focused control on a form?

Community
  • 1
  • 1
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • It started to work when I set `KeyPreview` to true and deleted 1 row. You have Solution in my first post. Can you look at it? Im wondering why i had to delete `WebControl` – audiophonic Jul 20 '16 at 11:49
1

Make sure your form has the KeyPreview property set to true.

Form.KeyPreview Property

Gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus.

enter image description here

Sadique
  • 22,572
  • 7
  • 65
  • 91
  • I changed KeyPreview to `true` (it was set to `false` before), but still that does not solve my problem. It still doesn't do anything. What has to be said, when I created new project and did what you said it worked. Messagebox was showing as a charm Every single Form1 properties on my application and on new one was the same. In both projects `Form1()` looks the same. There is only `InitializeComponent();` there, But why in first case it not working, but in brand new project it works? – audiophonic Jul 20 '16 at 11:38
  • @audiophonic Because of input focus. By default, messages like "key pressed" are only sent to the control that has focus. If you have a clean project, with only the form, the message goes to the form, since it has focus. When you add a control, it will have focus and will get the message instead of the form. – Luaan Jul 20 '16 at 12:07
  • So why does that not work with `KeyPreview` set on true? It should ignore currently focused control. – audiophonic Jul 20 '16 at 12:55
1

You surely don't have a blank form but a form that contains some controls like textboxes etc...

If you press a key, this event is raised for the control that currently has the focus. So the for example the KeyUp event for your TextBox is raised, but not for your form.

To ensure the event is raised in your Form before it is raised for the focused control, set the KeyPreview property of your Form to true:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponents();
        this.KeyPreview = true;
    }
    ...

Or as Sadiq showed, set it in the UI designer.

Community
  • 1
  • 1
René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • It started to work when I set `KeyPreview` to true and deleted 1 row. You have Solution in my first post. Can you look at it? Im wondering why i had to delete `WebControl` – audiophonic Jul 20 '16 at 11:49