Up, Down, Left and Right Arrow Keys Do Not Trigger Keydown Event

Up, Down, Left and Right arrow keys do not trigger KeyDown event

    protected override bool IsInputKey(Keys keyData)
{
switch (keyData)
{
case Keys.Right:
case Keys.Left:
case Keys.Up:
case Keys.Down:
return true;
case Keys.Shift | Keys.Right:
case Keys.Shift | Keys.Left:
case Keys.Shift | Keys.Up:
case Keys.Shift | Keys.Down:
return true;
}
return base.IsInputKey(keyData);
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
switch (e.KeyCode)
{
case Keys.Left:
case Keys.Right:
case Keys.Up:
case Keys.Down:
if (e.Shift)
{

}
else
{
}
break;
}
}

KeyDown not firing for Up, Down, Left and Right

Two basic reasons. First the mysterious one: a Label control cannot receive the focus so can't see keystrokes. The reason its KeyDown event is hidden in the designer. Not so sure why you see any keystrokes at all. The more common reason is that the cursor and TAB keys are used for navigation, moving the focus from one control to another. Which is done before the key is passed to the control. You'd have to override the control so you can override its IsInputKey() method. But more practically you'd override the UserControl's ProcessCmdKey() instead to solve both issues.

Also note that you've got a nasty handle leak in your program. Never call Controls.Clear() without also calling the Dispose() method on the controls you remove. Unless you intended to reuse them later, not so common. It is a nasty kind of leak that the garbage collector doesn't solve and ultimately crashes your program after first making it slow and unwieldy.

Detecting arrow key presses in JavaScript

Arrow keys are only triggered by onkeydown, not onkeypress.

The keycodes are:

  • left = 37
  • up = 38
  • right = 39
  • down = 40

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

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.

HandleKeyPress not recognising down arrow

event.which will give you the numeric value of the key.

event.key and event.code will give you a string value.

Try this tool: http://keycode.info

if (event.key === 'ArrowDown') {
console.log('Down arrow key fired');
}

As @devserkan mentioned you should use onKeyDown instead of onKeyPress.

The keydown event is fired when a key is pressed down. Unlike the keypress event, the keydown event is fired for keys that produce a character value and for keys that do not produce a character value.

Textbox Keydown event not firing when arrow key press

Instead of that Use PreviewKeyDown

Key Event doesn't actives by arrow keys

It depends what Controls you have placed inside your ContentControl and what control has the focus. Some keys like TAB, RETURN, ESC, and arrow keys, are typically ignored by some controls because they are not considered input key presses. The arrow keys are considered navigation keys and pressing these keys typically do not raise the KeyDown (for a Button as an example)

You can try to use the PreviewKeyDown-Event for this case.

If I use your provided code in a minimal solution I receive the KeyDown-Events for all keys, but it is required that the ContentControl has the focus.

public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
dockingContentControl.Focus();
}

private void dockingContentControl_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
Console.WriteLine(e.Key);
}
}

I need to get arrow key presses, but keyDown won't call (Swift)

Subclassing is less difficult than it seems.

Short tutorial:

General assumption: keystrokes are going to be received in the view of a NSViewController subclass and to be processed in the view controller class

  • Create a new Cocoa Class named MyView as a subclass of NSView
  • Replace the contents of the created class with

    import Cocoa

    let leftArrowKey = 123
    let rightArrowKey = 124

    protocol MyViewDelegate {
    func didPressLeftArrowKey()
    func didPressRightArrowKey()
    }

    class MyView: NSView {

    var delegate : MyViewDelegate?

    override func keyDown(event: NSEvent) {
    let character = Int(event.keyCode)
    switch character {
    case leftArrowKey, rightArrowKey:
    break
    default:
    super.keyDown(event)
    }
    }

    override func keyUp(event: NSEvent) {
    let character = Int(event.keyCode)
    switch character {
    case leftArrowKey:
    delegate?.didPressLeftArrowKey()
    case rightArrowKey:
    delegate?.didPressRightArrowKey()
    default:
    super.keyUp(event)
    }
    }

    override var acceptsFirstResponder : Bool {
    return true
    }
    }
    • Change the class of the view of the ViewController in Interface Builder to MyView
    • In the ViewController class add the protocol MyViewDelegate- for example

      class ViewController: NSViewController, MyViewDelegate {
    • In viewDidLoad() add

      let view = self.view as! MyView
      view.delegate = self
      self.nextResponder = view
    • Implement the following delegate methods and add your code to switch the image(s)

      func didPressLeftArrowKey() {
      println("didPressLeftArrowKey")
      // process keystroke left arrow
      }

      func didPressRightArrowKey() {
      println("didPressRightArrowKey")
      // process keystroke right arrow
      }

The delegate methods are called when the appropriate arrow keys are released



Related Topics



Leave a reply



Submit