开发者

Keyboard event issue

开发者 https://www.devze.com 2023-01-03 11:45 出处:网络
I have to capture the following keyboard event in a TextBox - SHIFT + 8(on the numpad). This also means the NumLock will be on. When I try the following SHIFT + UP is not printed:

I have to capture the following keyboard event in a TextBox - SHIFT + 8(on the numpad). This also means the NumLock will be on. When I try the following SHIFT + UP is not printed:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Shift && e.KeyCode == Keys.NumPad8)
        Console.WriteLine("SHIFT + UP");

    Console.WriteLine(e.KeyCode);
    Console.WriteLine(e.KeyData);
    Console.WriteLine();
}

But CTRL + 8(on the numpad) is working.

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Control && e.KeyCode == Keys.NumPad8)
        Console.WriteLine("CTRL + UP");
}

Update: 开发者_运维知识库The following is printed, on a single SHIFT + 8 press:

ShiftKey
ShiftKey, Shift

Up
Up

ShiftKey
ShiftKey, Shift

Could someone explain me, why SHIFT + 8 isn't fired, but CTRL + 8 is working?

P.S. I wrote + UP, because the user wants to navigate with the numpad arrows and the SHIFT key, but his NumLock will also be on. That's why I catch Keys.NumPad8.

All the best, Petar


It won't work because shift+numpad8 = UP and when you press these keys together:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    string shift = e.Shift.ToString();    //false
    string code = e.KeyCode.ToString();   //up
}

If you hit shift and up key together than shift - true code - up

So if you want to catch shift & numpad8 you only need to check UP key.

0

精彩评论

暂无评论...
验证码 换一张
取 消