Jump to content
  • Advertisement
Sign in to follow this  
ididntdoit

[.net] detecting keyboard events

This topic is 4012 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
I just tested the following (pretty much the same as yours), and it works fine.


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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
benryves

WOOT it worked!

Yeah there were other controls, one must have been stealing focus.

I love how a 30 minute problem can be solved in 2 seconds. Its like magic.

TY!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!