serhio
serhio

Reputation: 28586

KeyDown event in a Form

I need to associate Ctrl+Z key combination to a custom form action (Undo).

I handle Me.KeyDown event, but don't receive it each time I press keys. Perhaps it depends of what currently active control in a form I have.

As I read from this article I need to

  Private Sub MyForm_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
    e.Handled = True
  End Sub

but even this event I don't receive but when having some controls focused, but not others.

Upvotes: 1

Views: 3469

Answers (5)

emilio
emilio

Reputation: 1

 Private Sub form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        KeyPreview = True
    End Sub

Upvotes: -1

T3hc13h
T3hc13h

Reputation: 602

If your form has a menu then you could create an Undo MenuItem and set it's Shortcut properties to the desired key combo.

Upvotes: 1

jwnace
jwnace

Reputation: 360

You need to set the KeyPreview property of your form to true so that your form will receive the key events for all controls on the form. That way, your shortcuts should work no matter what control currently has focus. Here is a quick example you can play with to test this out. Create a new form with several different controls on it and modify the code like so:

public Form1()
{
    InitializeComponent();

    KeyPreview = true;  // indicates that key events for controls on the form
                        // should be registered with the form

    KeyDown += new KeyEventHandler(Form1_KeyDown);
}

void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Modifiers == Keys.Control)
    {
        switch (e.KeyCode)
        {
            case Keys.A:
                MessageBox.Show("Ctrl + A was pressed!");
                break;
            case Keys.C:
                MessageBox.Show("Ctrl + C was pressed!");
                break;
            case Keys.V:
                MessageBox.Show("Ctrl + V was pressed!");
                break;
        }
    }
}

No matter what control has focus when you enter the key combinations, your form's Form1_KeyDown method will be called to handle it.

Upvotes: 2

Tony Abrams
Tony Abrams

Reputation: 4673

  1. Sounds to me like you are wanting a global hook into what keys are pressed.

  2. If you do it the way you started, then you will have to handle the keypress/keydown event of every control on a form (or each form).

EDIT

If you use the KeyPress event mixed with the e.handled = true, that might get you where you want to be. Setting handled to true should mean that the form receives all of the events instead of the individual controls. If you then use the KeyPress event to handle the Ctrl+Z, then you should be able to run your custom command on a form regardless of what control has focus (according to the documentation).

Upvotes: 0

unholysampler
unholysampler

Reputation: 17341

You can override ProcessCmdKey to handle key presses on the form level.

See this question for more details and examples: Hotkey (not global) in Windows Forms .NET

Upvotes: 4

Related Questions