tags:

views:

44

answers:

1

Hello guys!

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

+6  A: 

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.

Ahmet Kakıcı
OK, but then if the user has pressed only the UP arrow(not on the numpad) the event will be fired. How to prevent this?
Petar Minchev
If user will press only UP arrow then e.Shift will be false (on my configuration).
Pavel Belousov
OK, the user presses SHIFT + 8. Then e.Shift is false and e.Keys == Keys.Up is true. This is the same as pressing the arrows not on the keypad. But pressing the right arrow in a textbox is for navigating the text:( I need navigating left, right, up and down with SHIFT + 4, SHIFT + 6, SHIFT + 8 and SHIFT + 2.
Petar Minchev
Is there something I don't understand? Look at my updated question. On a single SHIFT + 8 press, three things are printed. First only Shift, then only up, and as last, only Shift again.
Petar Minchev
e object doesn't have a variable that you can directly get if its shift+numpad8 or just UP key.If you want to catch this you both implement key down and key up events.If you catch shift and then up key before any key is up that's shift+numpad8.
Ahmet Kakıcı
But I receive the SHIFT key release event before the UP press event.
Petar Minchev
@Petar - Does it actually matter if the up arrow behaves the same as Shift + 8 in the application? Maybe easier to let it go than follow the requirements to the letter in this case. Just a thought...
Paolo
I don't care about the UP arrow, but if the arrow is LEFT or RIGHT, you know that it is used for moving the cursor in the textbox. I want all the 4 directions to be working, not only the UP and DOWN.
Petar Minchev
Can you tell about the big picture? What are you trying to do? If you just trying to move something then do it with direction keys. If it is not for you, maybe you can just try 2,4,6 and 8 keys.
Ahmet Kakıcı
@Ahmet First, thanks for wanting to help me. There will be a form, which is full of textbox controls for data entry. The user wants to navigate through them, like through the Desktop icons on Windows. Also your suggestion to use only the 2,4,6,8 can't help me because numbers are entered as data.
Petar Minchev
I talked with the user and he accepted to use the CTRL + 2,4,6,8 instead of the SHIFT + 2,4,6,8. I am happy now:)
Petar Minchev
@Ahmet I will accept your answer cause it gave me enough information that the SHIFT + 2,4,6,8 can't be done.
Petar Minchev