如何防止用户控件表单在 C# 中处理键盘输入(箭头键)

发布于 2024-11-10 11:42:28 字数 144 浏览 4 评论 0原文

我的用户控件包含可以选择的其他控件,我想实现使用箭头键在子控件上导航的方法

问题是父控件拦截箭头键并使用它来滚动其视图我想避免的事情是什么。我想自己解决控制内容的导航问题。

我如何控制由箭头键引起的标准行为?

提前致谢 MTH

my usercontrol contains other controls that can be selected, i would like to implement way of navigating over child controls with arrow keys

The problem is that parent controll intercepts arrow keys and use it to scroll its view what is the thing i want to avoid. I want to solve navigating over contents of control by myself.

How i can take control over standard behaviour that is caused by arrow keys?

Thanks in advance
MTH

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请帮我爱他 2024-11-17 11:42:28

这通常是通过重写 IsInputKey() 方法来告诉您需要这些键来完成的。然而,这对 UserControl 不起作用,它永远不会获得焦点,因此 OnKeyDown 方法永远不会运行。相反,重写 ProcessCmdKey() 方法,该方法在表单看到击键之前调用。像这样:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
        switch (keyData) {
            case Keys.Left: MessageBox.Show("Left!"); break;
            case Keys.Right: MessageBox.Show("Right!"); break;
            default: return base.ProcessCmdKey(ref msg, keyData);
        }
        return true;  // used
    }

This is normally done by overriding IsInputKey() method to tell that you want these keys. That however won't work for a UserControl, it never gets the focus so the OnKeyDown method never runs. Instead, override the ProcessCmdKey() method, it is called before the form sees the keystroke. Like this:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
        switch (keyData) {
            case Keys.Left: MessageBox.Show("Left!"); break;
            case Keys.Right: MessageBox.Show("Right!"); break;
            default: return base.ProcessCmdKey(ref msg, keyData);
        }
        return true;  // used
    }
孤城病女 2024-11-17 11:42:28

您可以在用户控件中重写 OnKeyDown,但这对于箭头键不起作用,因为它们不是“输入键”(它们是导航键)。

要更改行为,请覆盖 IsInputKey() 以告诉系统您想要获取箭头键事件:

    protected override bool IsInputKey(Keys keyData)
    {
        bool result = false;

        Keys key = keyData & Keys.KeyCode;

        switch (key)
        {
            case Keys.Up:
            case Keys.Down:
            case Keys.Right:
            case Keys.Left:
                result = true;
                break;

            default:
                result = base.IsInputKey(keyData);
                break;
        }

        return result;
    }

You can override OnKeyDown in your user control, but that doesn't work for arrow keys because they are not 'input keys' (they are navigation keys).

To change the behaviour, override IsInputKey() to tell the system you want to get events for arrow keys:

    protected override bool IsInputKey(Keys keyData)
    {
        bool result = false;

        Keys key = keyData & Keys.KeyCode;

        switch (key)
        {
            case Keys.Up:
            case Keys.Down:
            case Keys.Right:
            case Keys.Left:
                result = true;
                break;

            default:
                result = base.IsInputKey(keyData);
                break;
        }

        return result;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文