How to capture a key pressed in C#

Asked

Viewed 2,333 times

0

I have an application with digit buttons and want to capture keystrokes and run the events of these buttons. I added an event of keypress in the window and tried to capture the typed key, but could not.

Code:

private void CalculadoraDS2_KeyPress(object sender, KeyPressEventArgs e)
{
    if(e.KeyChar.GetType == '0')
    {
        btn0.PerformClick();
    }
}
  • The estate KeyPreview form has to be set to true so that you can manipulate this event. You have already checked it?

  • @Matheusmiranda attribute "Keycode" does not exist, and is Windows Form Application

  • @cat I modified it and it didn’t work ...

  • @Danielsantos da a look here I have no time to answer now.

1 answer

2


Follows code:

if (e.KeyChar == (char)Keys.D0)
{
    //tecla 0 pressionada
    btn0.PerformClick();
    e.Handled = true;
}

For more information: here.

Complete list of all key codes Keys Enum: here.

  • if(e.Keychar == '0') also

Browser other questions tagged

You are not signed in. Login or sign up in order to post.