0

my simple WinForms app consists of one Form which has 2 important methods:

private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Q)
    {
        qKeyPressed = true;
    }
    keyTimer.Start();
}

private void MainWindow_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Q)
    {
        qKeyPressed = false;
    }
    keyTimer.Stop();
}

In form's constructor I use button1.TabStop = false; to set button1 focus to false, so my form can handle methods KeyUp and KeyDown. I want my form to be focused even after button1 click event. I tried:

button1.TabStop = false;
this.Focus();

in private void button1_Click(object sender, EventArgs e) but this doesn't seem to work and button after click looks like focused and methods KeyUp and KeyDown are disabled. How can my problem be solved? Thanks in advance

Marcin Zdunek
  • 1,001
  • 1
  • 10
  • 25
  • Implement IMessageFilter instead so you can see the key messages regardless of which control has the focus. – Hans Passant Dec 29 '15 at 14:02
  • If you are seeking for a button which can only be clicked, but never get focused, take a look at this http://stackoverflow.com/questions/32823525/how-to-stop-pressing-button-using-keyboard-keys-like-spacebar-or-enter-c-shar/32825190#32825190 – Ivan Stoev Dec 29 '15 at 14:29

1 Answers1

3

Run this in the button click function:

this.ActiveControl = null;

When you click the button, you set it as the active control. Removing this property will let you put focus on your form.

Phiter
  • 14,570
  • 14
  • 50
  • 84