private void button1_Click(object sender, EventArgs e)
{
Keys key = new Keys();
while (true)
{
if (key == Keys.F5)
{
MessageBox.Show("F5");
}
else if (key == Keys.F4)
{
MessageBox.Show("F4");
}
}
}
How can I make this run in the background when the form is out of focus? My ultimate goal is to make this run in the background, and wait for the F5/F4 key to be pressed.
I’m not entirely sure what your question is. If you want to register a global hotkey you can use the windows api function RegisterHotKey.
There are already a few questions on SO answering how to use this function in C#.
If you want a hot-key that’s only active on a certain control or form you can use the events supplied by WinForms.
If you want to be notifed of all key-presses on the desktop then a low level keyboard hook is appropriate. But don’t use a hook if you just want a global hotkey.
Your code itself doesn’t make much sense.
new Keys()just returns the default value of theKeysenum(Keys.NoneI guess) which is never equal to F4 or F5.