Jump to content
  • Advertisement
Sign in to follow this  
obi-wan shinobi

[.net] KeyPress event & Form class question

This topic is 4836 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

In trying to test a KeyPressEventHandler on a Windows Form, I've apparently come across a problem - I can't get the KeyPress event to happen unless I attach it to a text box. This kinda strikes me as strange for 2 reasons: 1) I'm wondering how I would be able to even begin writing a small game if I can't even get the form to detect key presses, and 2) I didn't have this problem in Java. This is my code in C#:

using System;
using System.Windows.Forms;
using System.Drawing;

class Windowapp {	

	static private int i = 0;
	static private int j = 0;
	static private Label l;
	static private TextBox T;

	public static void Main() {

		Form f = new Form();
		Button b = new Button();
		Button c = new Button();
		l = new Label();
		T = new TextBox();
		b.Text = "click me!";
		c.Text = "me too!!!";
		l.Text = "Total " + i;
		T.Text = "Key: " + j;
		f.Click += new EventHandler(clicked);

                //T.KeyPress += ... would work, but this does not
		f.KeyPress += new KeyPressEventHandler(pressed);

		l.Size = new Size(30,30);
		T.Size = new Size(30,30);
		b.Location = new Point(10,10);
		c.Location = new Point(40,40);
		l.Location = new Point(70,70);
		T.Location = new Point(110,110);
		f.Text = "Windows Form...";
		f.HelpButton = true;

	public static void clicked(object sender, EventArgs e) {
		l.Text = "Total " + i;
		Console.WriteLine("Button clicked.  Event handled.");

	public static void pressed(object sender, KeyPressEventArgs e) {
		switch(e.KeyChar) {
			default:	Console.WriteLine("Key typed.  Event processed...");	break;
		T.Text = "Key: " + j;


Share this post

Link to post
Share on other sites
You might want to check out Form.KeyPreview:

public bool KeyPreview { set; get; }
Member of System.Windows.Forms.Form

Gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus.

true if the form will receive all key events; false if the currently selected control on the form receives key events. The default is false.

So, if you add "f.KeyPreview = true;" in your Main function, you'll get the functionality you're looking for. The reason you weren't getting this before (as stated in the listing from the docs above) is that the controls had focus, and not the form itself. To see this in action, remove all the controls from the form and then run your program as shown in your post. Note that the events are handled as intended.

If you're writing a game, you might want to look into using DirectX.DirectInput. From what (very little) I have seen, it looks a bit easier to use than the approach you're taking.


Share this post

Link to post
Share on other sites
Thanks for the suggestion - it worked. I suspected that there would be a function that would be able to detect whether or not a control could receive certain events, but I didn't get around to looking for it yet.

As for using DirectInput, I suppose it wouldn't hurt to try finally learning Managed DX. I have the DX7 sdk installed on this (rather weak-8MB video memory) laptop I use, so I could just get the DX9 sdk and uninstall the older one...I'll still have most if not all of the header files and libraries for Win32 C++ stuff anyway.

Share this post

Link to post
Share on other sites
If you are happy using key events, then there really isn't all that much reason to move up to direct input. The advantage is that it should (in theory) work with the likes of Mono or DotGun, whereas direct input will not.

Also, know that there is also System.Windows.Forms.Control.MouseButtons, System.Windows.Forms.Control.MousePosition, etc, which are static so you don't need a form or events to get them. There isn't a static key list however :/

Personally I've found this can be more robust that direct input in some cases, as you don't have to worry about losing the device, etc. Provided that super fast control responce isn't essential, you should be fine.

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!