简体   繁体   English

C#按键事件-KeyDown

[英]C# Key Events - KeyDown

I have a Windows Form Application. 我有一个Windows窗体应用程序。 I want some functions to work with the space key. 我希望某些功能可以使用空格键。 But when I press the space key, the function I want is not working and it goes to the next form. 但是,当我按空格键时,我想要的功能不起作用,它将转到下一个表格。 (I did KeyPreview = true) (我做了KeyPreview = true)

private void Form7_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Space)
        {
            IEyeTracker eyeTracker = EyeTrackingOperations.FindAllEyeTrackers().FirstOrDefault();
            GazeDataStop(eyeTracker);
        }
    }

Because: 因为:

1- If you have buttons, ... keydown won't work as form won't have focus anymore 2-you must handle the keydown so that it is not passed to ohter controls 1-如果您有按钮,则...键按下将不再起作用,因为窗体将不再具有焦点2-您必须处理该键按下,以免将其传递给其他控件

Solution for 1: set KeyPreview property of your form to true 1的解决方案:将表单的KeyPreview属性设置为true

Solution for 2: 解决方案2:

set e.Handled = true : 设置e.Handled = true

private void Form7_KeyDown(object sender, KeyEventArgs e)
    {
        e.Handled = true;
        if (e.KeyCode == Keys.Space)
        {
            IEyeTracker eyeTracker = EyeTrackingOperations.FindAllEyeTrackers().FirstOrDefault();
            GazeDataStop(eyeTracker);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM