6
Answers

Key events not working after button placed in form

Mark

Mark

7y
172
1
Hi there,
 
I have a program which has a key down event in it.  When the user presses the left, right, up or down arrow keys it basically moves a character around the screen.
 
This program was working fine, however the second i put a button onto the form then the arrow keys all stop working.
 
I have done some research and found some potential solutions, but none of them worked.
 
I have tried the following to form1_load:
keyPreview = true; //turn this on and no key press is registered
this.focus(); //this line doesn't do anything either
this.BringToFront();
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
I have also added the Form1_KeyDown as a trigger to the button control and that didn't seem to fire the Form1_keyDown either.  Any ideas? 
 
As i say, the key presses worked until i placed the button onto the form.  Also when i put a break point in on the key event handler, nothing seems to get triggered.  I think there is conflict between another control but not sure what the next thing to try is?
 
I can supply some code but any help would be greatful.
 
Mark
Answers (6)
1
Nilesh Shah

Nilesh Shah

NA 22.3k 214.8k 7y
remove that keydown code
 
use this one:
  1. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)  
  2.         {  
  3.             //capture up arrow key  
  4.             if (keyData == Keys.Up)  
  5.             {  
  6.                 MessageBox.Show("You pressed Up arrow key");  
  7.                 return true;  
  8.             }  
  9.   
  10.             return base.ProcessCmdKey(ref msg, keyData);  
  11.         } 
 
Accepted
1
Nilesh Shah

Nilesh Shah

NA 22.3k 214.8k 7y
when you place a button and it is enabled, the events get fired on the button
 
why don't you directly assign event on the textbox,
 
i see you have specified event on the form
1
Nilesh Shah

Nilesh Shah

NA 22.3k 214.8k 7y
at least specify in your question is it windows forms or ASP.net web forms.
 
if its web forms probably button is now considered submit button, hence you have to explicitly specify AutoPostBack="true" for textbox.
 
but if you have written code for that textbox in code behind, then again its wrong becuase it should be in JavaScrit so the form does not post back every time key is pressed
0
Mark

Mark

NA 17 0 7y
That does appear to work now with that code but it doesn't really explain why Microsoft just allow other controls to take presidence of the focus and keyEvents like they do.  I'll mark your answer as accepted though and maybe they'll have a re-think on it.
 
Many thanks for your help Nilesh Shah.
 
Mark 
0
Mark

Mark

NA 17 0 7y
Hi there,
 
Yeah so on my form_load i have the following:
 
private void Form1_Load(object sender, EventArgs e)
{
this.BringToFront();
this.Focus();
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
}
 
Then on the keydown event of the form i have the following:
 
  1. private void Form1_KeyDown(object sender, KeyEventArgs e)  
  2. {  
  3.     if (e.KeyCode == Keys.Left)  
  4.     {  
  5.         character.controllerX -= 10;  
  6.     }  
  7.     if (e.KeyCode == Keys.Right)  
  8.     {  
  9.         character.controllerX += 10;  
  10.     }  
  11.     if (e.KeyCode == Keys.Up)  
  12.     {  
  13.         character.controllerY -= 10;  
  14.     }  
  15.     if (e.KeyCode == Keys.Down)  
  16.     {  
  17.         character.controllerY += 10;  
  18.     }  
  19. }  
 Do you know how i can get the form to then fire the events instead?  It seems a bit of a design floor with visual studio.  In the button i went into events and told the keyDown event to call Form1_keyDown but still when i press the arrow keys, nothing is happening still.  I have put a break point in just inside the keyDown event and i don't see it and so it's definately not firing still
 
How come you mentioned about a textbox?  I don't have any on that form.
 
Mark 
0
Mark

Mark

NA 17 0 7y
Hi there,Yeah this is a windows form. Ironically when I set the button to disabled in properties the key down event code then works. The second the button is enabled again then the key events stop working. Sorry about not specifying, have you got any ideas?