How do I use the F2 key to open another form?

Asked

Viewed 630 times

1

I have the following code, but this enabled the key ENTER (13) to open the form, I would like to enable the F2, I have already searched ASCII tables everywhere and can’t find the desired code.

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    {
        if (e.KeyChar == 13)
        {
            //sua rotina aqui
            OPERACOES OP = new OPERACOES();
            OP.ShowDialog();
        }
    }
}
  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

4 answers

5

Really have to check the key code and not ASCII table:

private void Form1_KeyPress(object sender, KeyPressEventArgs e) {
    if (e.KeyCode == Keys.F2) {
        //sua rotina aqui
        OPERACOES OP = new OPERACOES();
        OP.ShowDialog();
    }
}

I put in the Github for future reference.

  • With this command you gave me already tried , any button I click opens the form and not only F12.

  • No, so you did it another way. Take a look at the whole code. I noticed that you do not organize the code well, so it is more difficult to understand what is happening, so the problem may be elsewhere. You can post the code you tried for us to see if there are any errors in it. But you have to put the real code.

  • @user33133 if you click and the form opens, then your code is not linked to _KeyPress - is probably in the _OnClick.

  • @Onosendai good, I didn’t even know that he spoke in click :)

  • @mustache unless by button the OP means key, then it can be the Keypress being interpreted even...

4

You need to use the property KeyCode and compare it with the Enum Keys to check which key is being pressed.

Example:

private void Form1_KeyPress(object sender, KeyPressEventArgs evt){
    if(e.KeyCode == Keys.F2)
    {
        OPERACOES OP = new OPERACOES();
        OP.ShowDialog();
    }
}

Don’t forget that you need to change the property KeyPreview from form to true

  • I’ve used Keydown, but any key opens the form and not only F2.

  • Impossible. You can debug and see what the value in e.KeyCode?

2

The ideal way to process keyboard events in a form where the control that receives the event is irrelevant (i.e., the focus may be on any control present in the form) is via override of the method ProcessCmdKey in form:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == Keys.F2)
    {
        MessageBox.Show("Você pressionou a tecla F2.");
        return true;    // Indica que o pressionar desta tecla 
                        // foi gerenciado aqui.
    }

    // Propaga o evento para o método da classe base
    return base.ProcessCmdKey(ref msg, keyData)
}

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

  • The event KeyDown form is not fired with focus on any control either?

  • @Jéfersonbueno I believe so, case KeyPreview = true;. However, 2 points - ProcessCmdKey happens before in the cascade of events, and this method ensures that you will receive the event even if the Keypreview property is changed (accidentally or not) to False.

  • Interesting. Mainly the fact of ProcessCmdKey to be the first to be executed, I didn’t know that.

0

I found a list indicating some keyboard keys to use in Keyup event, I did not check all but "F2" worked.

Then just change in the if.

Example:

//Verificando se foi acionado a tecla F2

if(e.KeyChar == **113**){

//comandos

} 

Follows the list:

8: Put (' [BACKSPACE] on);

9: Place (' [GRT]);

12: Put (' [AM] written);

13: Put (' [ENTER] in);

16: Puts (' [SHIFT] in);

17: Put (' [CONTROL] in);

18: Put (' [AM] written);

20: Put (' [CAPS LOCK] in);

21: Put (' [PAGE UP] in);

27: Set (' [ESC] up);

33: Put (' [PAGE UP] in);

34: Put (' [PAGE DOWN] in);

35: Puts (' [END] in);

36: Place (' [HOME] in);

37: Put (' [LEFT ARROW] in);

38: Place (' [ARROW ABOVE]);

39: Place (' [RIGHT ARROW] Position);

40: Put (' [ARROW BELOW] in);

45: Put (' [INSERT] in);

46: Put (' [DEL] written);

91: Put (' [WIN LEFT] in);

92: Put (' [WIN RIGHT] in);

93: Put (' [POP-UP MENU] in);

96: Place('0&8242;);

97: Place('1&8242;);

98: Place('2&8242;);

99: Place('3&8242;);

100: Place('4&8242;);

101: Put('5&8242;);

102: Place('6&8242;);

103: Place('7&8242;);

104: Place('8&8242;);

105: Place('9&8242;);

106: Places (' [NUM *] );

107: Puts (' [NUM +] in);

109: Puts (' [NUM -] in);

110: Put (' [TO ONE SEP. DECIMAL] in);

111: Put (' [NUM /] above);

112: Put (' [F1] on);

113: Put (' [F2] written);

114: Put (' [F3] written);

115: Put (' [F4] written);

116: Put (' [F5] in);

117: Put (' [F6] written);

118: Put (' [F7] written);

119: Put (' [F8] written);

120: Put (' [F9] in);

121: Place (' [F10] in);

122: Put (' [F11] written);

123: Place (' [F12] in);

144: Places (' [NUM LOCK] on);

186: Place('Ç');

187: Place ('=);

188: Place(',);

189: Place ('-);

190: Put('.);

191: Place(';);

192: Put (' [APOSTROPHE] in);

193: Place ('/);

194: Puts (' [ONE POINT] in);

219: Place (' ');

220: Place (']);

221: Place('[);

222: Place('~);

226: Place(');

Browser other questions tagged

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