简体   繁体   中英

C# Key Events - KeyDown

I have a Windows Form Application. 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)

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

Solution for 1: set KeyPreview property of your form to true

Solution for 2:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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