上、下、左、右方向键不触发 KeyDown 事件 [英] Up, Down, Left and Right arrow keys do not trigger KeyDown event

查看:33
本文介绍了上、下、左、右方向键不触发 KeyDown 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,其中所有的键输入都必须由窗口本身处理.

I am building an application where all the key input must be handled by the windows itself.

我将 tabstop 设置为 false,因为每个控件都可以抓取除面板之外的焦点(但我不知道它是否有效果).

I set tabstop to false for each control witch could grab the focus except a panel (but I don't know if it has effect).

我将 KeyPreview 设置为 true 并且我正在处理此表单上的 KeyDown 事件.

I set KeyPreview to true and I am handling the KeyDown event on this form.

我的问题是有时箭头键不再响应:

My problem is that sometimes the arrow key aren't responsive anymore:

  • 当我只按下一个箭头键时不会触发 keydown 事件.

  • The keydown event is not fired when I pressed only an arrow key.

如果我按下带有控制修饰符的箭头键,就会触发 keydown 事件.

The keydown event is fired if I press an arrow key with the control modifier.

你知道为什么我的方向键突然停止触发事件吗?

Have you an idea why my arrow key suddenly stop firing 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 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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