Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KeyDown recognizes the left and right directional arrow keys, but not up and down

With the code below, the Left and Right arrow keys function as expected, but the up and down arrows are not recognized (stepping through it, the first two conditions are met where appropriate, but the second two never are):

private void textBox1_KeyDown(object sender, KeyEventArgs e) {
    TextBox tb = (TextBox)sender;

    if (e.KeyCode.Equals(Keys.Left)) {
        SetFocusOneColumnBack(tb.Name);
        e.Handled = true;
        return;
    }
    if (e.KeyCode.Equals(Keys.Right)) {
        SetFocusOneColumnForward(tb.Name);
        e.Handled = true;
        return;
    }
    if (e.KeyCode.Equals(Keys.Up)) {
        SetFocusOneRowUp(tb.Name);
        e.Handled = true;
        return;
    }
    if (e.KeyCode.Equals(Keys.Down)) {
        SetFocusOneRowDown(tb.Name);
        e.Handled = true;
        return;
    }
}

Why would this be, and how can I fix it?

UPDATE

Here's what I see when I hover over e.Keycode while stepping through. If I pressed

  • ...Left arrow key, I see: e.KeyCode = "LButton | MButton | Space"
  • ...Right arrow key, I see: e.KeyCode = "LButton | RButton | MButton | Space"
  • ...Up arrow key, I see: e.KeyCode = "RButton | MButton | Space"
  • ...Down arrow key, I see: e.KeyCode = "Backspace | Space"

This has got me baffled (what it's showing me), but on keyleft and keyright, my code is entered - it never is for keyup and keydown, no matter how hard I clench my teeth.

like image 809
B. Clay Shannon-B. Crow Raven Avatar asked May 09 '12 16:05

B. Clay Shannon-B. Crow Raven


People also ask

Why are my up and down arrow keys not working?

Turn off Scroll Lock on your keyboard Most of the time, if your arrow keys aren't moving the cursor from cell to cell, the fix is as simple as disabling the Scroll Lock key on your keyboard.

What is difference between Keydown and keypress?

The keydown event is fired when a key is pressed. Unlike the keypress event, the keydown event is fired for all keys, regardless of whether they produce a character value. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered.

What are the left and right arrow keys called?

Alternatively referred to as cursor keys, direction keys, and navigation keys, the arrow keys are usually located between the standard section and the numeric pad on computer keyboards. It is made up of four keys: the left arrow (back arrow), up arrow, down arrow, and the right arrow (forward arrow).

What do we call the 4 keys that move the cursor up down left or right?

The 'arrow' keys allow you to move your cursor in all directions on the page or screen - up, down, left and right.


2 Answers

Windows captures certain keys for UI navigation before they every get sent to your form. If you want to override this behavior you need to overload the IsInputKey method (and subclass the text field):

    protected override bool IsInputKey(Keys keyData)
    {
        if (keyData == Keys.Right)
            return true;
        return base.IsInputKey(keyData);
    }
like image 50
Hampus Nilsson Avatar answered Sep 21 '22 13:09

Hampus Nilsson


I find that using the PreviewKeyDown does work (I had to remove the "e.Handled = true" code, as it doesn't apply in the PreviewKeyDown event):

private void textBoxQH1_PreviewKeyDown(object sender,   PreviewKeyDownEventArgs e) {
    TextBox tb = (TextBox)sender;

    if (e.KeyCode.Equals(Keys.Up)) {
        SetFocusOneRowUp(tb.Name);
        return;
    }
    if (e.KeyCode.Equals(Keys.Down)) {
        SetFocusOneRowDown(tb.Name);
        return;
    }
}

So, three different events were needed to handle the various keys I was looking for: KeyPress for regular characters, KeyDown for non-characters (left and right arrow keys) and this one (PreviewKeyDown) for the up and down arrow keys.

like image 45
B. Clay Shannon-B. Crow Raven Avatar answered Sep 20 '22 13:09

B. Clay Shannon-B. Crow Raven