1

I am making a program to record the position of a mouse when someone presses the Space button.

This works fine, however when I put the cursor in any textBox in the form the code becomes useless of course because the space gets typed in the textBox. I tried to change the focus() or try other keys like LeftWin ... but none worked!

Any advice on how can I detect the Space button (or any other key) all the time in a form?

private void lebel1_KeyDown(object sender, KeyEventArgs e)
{
   if (e.KeyCode == Keys.Space)
   {
      //bla bla
   }
}
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
e2saleh
  • 11
  • 2
  • You'll need to think about this for a while. Are you *sure* you want to prevent the user from typing a space in a TextBox? Something like the F1 key is of course a much better choice. – Hans Passant Nov 14 '15 at 01:41

1 Answers1

0

The event is not firing because the Textbox is handled the event, with that being said, the event will not be propagated to the parent controls (Bubbling events).

You could use a KeyPress, which means that the event will be fired from the most outer control and will be propagated to their children. (Tunnel events)

enter image description here

You could learn more about here.

Bruno Joaquim
  • 1,423
  • 1
  • 14
  • 15