Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch KeyUp Event on WinForm C#

I attempt to catch F5 on System.Windows.Forms for that I wrote:

partial class MainForm
{
   (...)
   this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.MainForm_KeyUp);
   (...)
}

public partial class MainForm : Form
{
    (...)

    private void MainForm_KeyUp(object sender, KeyEventArgs e)
    {
        Log("MainForm_KeyUp");
        if (e.KeyCode == Keys.F5)
        {
            RefreshStuff();
        }
    }
}

But my event catching looks not working.

Do you know how to cactch EventKey on System.Windows.Forms ?

like image 992
mickro Avatar asked Sep 04 '13 02:09

mickro


People also ask

How do you KeyPress an event in C#?

Select your TextBox control on your Form and go to Properties window. Select Event icon on the properties window and scroll down and find the KeyDown event from the list and double click the Keydown Event. The you will get the KeyDown event in your source code editor.

What is Keyup in Javascript?

The keyup() is an inbuilt method in jQuery which is used to trigger the keyup event whenever User releases a key from the keyboard. So, Using keyup() method we can detect if any key is released from the keyboard. Syntax: $(selector).keyup(function)

What is KeyDown event in Visual Basic?

The KeyDown event occurs when the user presses a key while a form or control has the focus. This event also occurs if you send a keystroke to a form or control by using the SendKeys action in a macro or the SendKeys statement in Visual Basic.


1 Answers

The KeyPreview property of the Form must be set to true.

When this property is set to true, the form will receive all KeyPress, KeyDown, and KeyUp events. After the form's event handlers have completed processing the keystroke, the keystroke is then assigned to the control with focus.

like image 109
Damith Avatar answered Oct 01 '22 23:10

Damith