[.net] detecting keyboard events
Im trying to learn how to detect keyboard button presses in my simple app.
So far I have this :
//note - Program1 : Form1 : Form
//in my constructor is this
this.KeyPress += new KeyPressEventHandler(Program_KeyPress);
//I believe this is a delegate (right?) and tells my app to call
//a function called 'Program_Keypress' when a keyboard button is pressed.
//and this function (location is Program1 Program_KeyPress(), so
//same scope as the delegate) is meant to handle the input
void Program_KeyPress(object sender, KeyPressEventArgs e)
{
label1.Text = e.KeyChar.ToString();
}
It compiles and runs fine. It just dosent change label1.Text no matter where I click or what keyboard button I press.
Stumped.
I just tested the following (pretty much the same as yours), and it works fine.
You need to make sure that the form has the focus when you are trying to hit the key. Besides that I don't know why yours is not working.
Also please put your code between source tags.
theTroll
public partial class Form1 : Form { public Form1() { InitializeComponent(); this.KeyPress += new KeyPressEventHandler(Form1_KeyPress); } void Form1_KeyPress(object sender, KeyPressEventArgs e) { label1.Text = e.KeyChar.ToString(); } private void Form1_Load(object sender, EventArgs e) { } }
You need to make sure that the form has the focus when you are trying to hit the key. Besides that I don't know why yours is not working.
Also please put your code between source tags.
theTroll
The keyboard messages are sent to your form, and the various controls on the form (including the form itself) have to work out which control to send the keystrokes to. My guess is that you have another control on the form that is stealing the focus and hence your keyboard messages.
This is easy enough to fix; simply set this.KeyPreview = true; so that your form will get the input events before anything else. In your event handler, set e.Handled = true; to indicate that the event has been handled and the character has been consumed.
Another pitfall is specifying whether a key is a valid input key or a character is a valid input character for the control. You can override the control's Control.IsInputKey and/or Control.IsInputChar to help with this; this is useful for catching cursor keys from a form and not letting a button or text box steal the focus.
This is easy enough to fix; simply set this.KeyPreview = true; so that your form will get the input events before anything else. In your event handler, set e.Handled = true; to indicate that the event has been handled and the character has been consumed.
Another pitfall is specifying whether a key is a valid input key or a character is a valid input character for the control. You can override the control's Control.IsInputKey and/or Control.IsInputChar to help with this; this is useful for catching cursor keys from a form and not letting a button or text box steal the focus.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement