C# Window_KeyUp() not working

Joscplan picture Joscplan · Jun 3, 2013 · Viewed 7.8k times · Source

Hey I have this piece of code:

private void Window_KeyUp(object sender, KeyEventArgs e)
{
    if (playing == false)
    {
        return;
    }
    if (e.KeyCode == Keys.D1)
    {
        pictureBox6.Image = Form.Properties.Resources.black_square_button;
        player.Stop();
        player.Close();
        playing = false;
    }
}

I'ts not working but the Window_KeyDown() works.

What is wrong with my code?

Thanks.

Answer

Steve picture Steve · Jun 3, 2013

The KeyUp event (also KeyDown and KeyPress) are triggered at the form level only if the form has

KeyPreview = true; 

MSDN here

true if the form will receive all key events; false if the currently selected control on the form receives key events. The default is false.