C# Key Events - KeyDown

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


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




1 Answer
1



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


KeyPreview



Solution for 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);
}
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Visual Studio Code: How to configure includePath for better IntelliSense results

Spring cloud config client Could not locate PropertySource

Makefile test if variable is not empty