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

1

I have the following code, but it enabled the ENTER (13) key to open the form, I would like to enable F2 , I already looked for ASCII tables everywhere and can not 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();
        }
    }
}
    
asked by anonymous 23.09.2015 / 13:20

4 answers

5

You really have to check the key code and not the ASCII table:

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

You need to use the KeyCode property and compare it with 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();
    }
}

Do not forget that you need to change the KeyPreview property of the form to true

    
23.09.2015 / 13:23
2
The optimal way to process keyboard events in a form where the control that receives the event is irrelevant (that is, the focus can be on any control present in the form) is via override method ProcessCmdKey no 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?

    
23.09.2015 / 14:58
0

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

Then just change the if.

Example:

//Verificando se foi acionado a tecla F2

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

//comandos

} 

Here's the list:

  

8: Place ('[BACKSPACE]');

     

9: Place ('[TAB]');

     

12: Place ('[ALT]');

     

13: Place ('[ENTER]');

     

16: Place ('[SHIFT]');

     

17: Place ('[CONTROL]');

     

18: Place ('[ALT]');

     

20: Place ('[CAPS LOCK]');

     

21: Place ('[PAGE UP]');

     

27: Place ('[ESC]');

     

33: Place ('[PAGE UP]');

     

34: Places ('[PAGE DOWN]');

     

35: Place ('[END]');

     

36: Place ('[HOME]');

     

37: Place ('[LEFT ARROW]');

     

38: Place ('[UP ARROW]');

     

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

     

40: Place ('[DOWN ARROW]');

     

45: Place ('[INSERT]');

     

46: Place ('[DEL]');

     

91: Place ('[WIN LEFT]');

     

92: Place ('[WIN RIGHT]');

     

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

     

96: Places ('0 & 8242;);

     

97: Places ('1 & 8242;);

     

98: Places ('2 & 8242;);

     

99: Places ('3 & 8242;);

     

100: Places ('4 & 8242;);

     

101: Places ('5 & 8242;);

     

102: Places ('6 & 8242;);

     

103: Places ('7 & 8242;);

     

104: Places ('8 & 8242;);

     

105: Places ('9 & 8242;);

     

106: Place ('[NUM *]');

     

107: Places ('[NUM +]');

     

109: Places ('[NUM -]');

     

110: Place ('[DECIMAL SEPTEMBER]');

     

111: Places ('[NUM /]');

     

112: Place ('[F1]');

     

113: Place ('[F2]');

     

114: Place ('[F3]');

     

115: Place ('[F4]');

     

116: Place ('[F5]');

     

117: Place ('[F6]');

     

118: Place ('[F7]');

     

119: Place ('[F8]');

     

120: Place ('[F9]');

     

121: Place ('[F10]');

     

122: Place ('[F11]');

     

123: Place ('[F12]');

     

144: Place ('[NUM LOCK]');

     

186: Places ('Ç');

     

187: Place ('=');

     

188: Place (',');

     

189: Place ('-');

     

190: Place ('.');

     

191: Place (';');

     

192: Place ('[APÓSTROFO]');

     

193: Place ('/');

     

194: Place ('[IN A POINT]');

     

219: Place ('' ');

     

220: Place ('' ');

     

221: Place ('[');

     

222: Place ('~');

     

226: Place ('\');

    
02.06.2016 / 05:57