WinForms keypress not working correctly

update . I modified the code below to find out more information about keystroke.

update # 2 : I found the root cause of the problem: we have an HTML control (Gecko rendering engine) in our form. When this Gecko rendering engine switches to some kind of Flash control, suddenly ~ 2% of our keystrokes do not pass, even after we removed the Gecko HTML control. Wahoo, I blame it on Flash! Now the question is, how can I fix this?

update # 3 : No, this is not Flash. This is the Gecko rendering engine. Navigation even in Google leads to the fact that some keys do not get directly into our application. Hrmmm.

We have a strange case in our WinForms application where the user presses a key combination ( Alt+ in this case S), and WinForms tells us that the following key combination has been pressed (value 262162):

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if(keyData == (Keys.S | Keys.Alt))
    {
       Console.WriteLine("You pressed Alt+S");
    }
    else if(keyData == (Keys.Menu | Keys.Alt))
    {
       Console.WriteLine("What the hell?"); // This sometimes gets hit when I press Alt+S
    }
}

90% of the time You pressed Alt+Swill be shown. But in rare cases, we press Alt+ Sand he said: What the hell?.

Any idea what is wrong?

+3
source share
4 answers

EDIT I found the root cause of the problem! See below.

After additional experiments, I found that if the following is done, it works as expected:

this.KeyPreview = true;
this.KeyDown += KeyDownHandler;
...
private void KeyDownHandler(object sender, KeyEventArgs e)
{
   if (e.KeyCode == Keys.S && e.Alt)
   {
        // This always works.
   }
}

, ProcessCmdKey . . .

. HTML- ( Gecko) . gecko , -, , WM_Char WM_KeyDown .

+3

, , 262162 "Alt".

: ProcessCmdKey "X = 1;" :

    protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
    {
        int x = (int)keyData;
        if (x == 262162)
            x = 1;
        return true;
    }

, Alt.

+1

, S, Alt ?

EDIT2: , " ", ALT, , , Alt-S . , -, key code Alt. , Alt-S Will .

EDIT: METADATA Key.Menu - 18. F12, .

, Keys.Menu Alt.

http://msdn.microsoft.com/en-us/library/system.windows.forms.keys.aspx

, , Alt (18) Alt (262144).

+1

Just out of curiosity, you could try this:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == (Keys.S | Keys.Alt))
        {
            MessageBox.Show("You pressed Alt+S");
        }
        else if (keyData == (Keys.Menu | Keys.Alt))
        {
            return false;
        }

        return true;
    }

return false indicates that this is not a command key.

0
source

Source: https://habr.com/ru/post/1712213/


All Articles