KeyDown可以识别左右方​​向箭头键,但不能识别上下方向键 [英] KeyDown recognizes the left and right directional arrow keys, but not up and down

查看:183
本文介绍了KeyDown可以识别左右方​​向箭头键,但不能识别上下方向键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,向左和向右箭头键可按预期运行,但无法识别向上和向下箭头(逐步执行此步,在适当的情况下满足前两个条件,但从不满足前两个条件):

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?

这是我逐步浏览时将鼠标悬停在e.Keycode上时看到的内容.如果我按下

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

  • ...向左箭头键,我看到:e.KeyCode = "LButton | MButton | Space"
  • ...向右箭头键,我看到:e.KeyCode = "LButton | RButton | MButton | Space"
  • ...向上箭头键,我看到:e.KeyCode = "RButton | MButton | Space"
  • ...向下箭头键,我看到:e.KeyCode = "Backspace | Space"
  • ...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"

这让我感到困惑(显示的内容是什么),但是在keyleft和keyright上,输入了我的代码-不管我握紧牙齿有多快,它都不是用于keyup和keydown的.

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.

推荐答案

我发现使用PreviewKeyDown确实可行(我必须删除"e.Handled = true"代码,因为它不适用于PreviewKeyDown事件):

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;
    }
}

因此,需要三个不同的事件来处理我正在寻找的各种键:KeyPress用于常规字符,KeyDown用于非字符(左右箭头键),而这个(PreviewKeyDown)用于向上和向下箭头键

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.

这篇关于KeyDown可以识别左右方​​向箭头键,但不能识别上下方向键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆