Good day, friends! I am writing a program in which you can set hotkeys for actions. And ran into a problem in the numeric keypad. If you click for example 0 (on the numeric keypad ), then in the textbox in the KeyDown , e.KeyCode will be Keys.NumPad0 , and if you press SHIFT + 0 (on the numeric keypad ), then e.KeyCode = Keys.Insert , and e.Modifiers = Keys.None and e.Shift = false;

 private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.Shift && e.KeyCode == Keys.NumPad0) { MessageBox.Show("SHIFT + NumPad0"); } } 

Through experiments, I found out that when the OS is pressed, the OS and NumPad0 convert it to Insert , and to be exact, it programmatically “squeezes out” Shift and replaces this combination with Insert . Therefore, a condition is added to the KeyDown method.

 if (e.Modifiers == Keys.Shift) { _FlagShiftPressed = true; } 

and in the keyup method

 if (e.Shift == false & e.Modifiers == Keys.Shift) { _FlagShiftPressed = false; } 

Thus, it turned out to handle any clicks on the numeric keypad with any modifier. But after that the underwater rock came out. When you click on SHIFT , release and then click on Insert (for example), we get the result as if you press SHIFT+0 (on the digital keyboard). Those. in the KeyUp event, when the Shift released, the flag is not reset and the processing proceeds as if the numeric keypad is pressed. If it was not clear expressed, then you can look at the example of setting the "hot buttons" in the properties of the shortcut. How can I get around this problem, or can there be other more rational solutions?

    1 answer 1

    Found a solution here, on stackoverflow , and so far it works without errors. I will give it here to reduce the degree of entropy. In my case, I check the scancodes for compliance with the trace. buttons: Insert , Home , PageUp , PageDown , End , Left , Up , Down , Right .

     protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { switch (msg.Msg) { case WM_KEYDOWN: { int lParam = msg.LParam.ToInt32(); int scanCode = (lParam >> 16) & 0x000000ff; // extract bit 16-23 int ext = (lParam >> 24) & 0x00000001; // extract bit 24 if (ext == 1) { scanCode += 128; } if (scanCode == 210 || scanCode == 199 || scanCode == 201 || scanCode == 209 || scanCode == 200 || scanCode == 203 || scanCode == 205 || scanCode == 208 || scanCode == 207) { _FlagShiftPressed = false; } break; } } return base.ProcessCmdKey(ref msg, keyData); }