Problems with Keyboard input (KeyDown, KeyPress, KeyUp)
-
Hi, I'm a rather new C# user and I'm still figuring out the ropes of forms programming and such. For the part of the program I'm working on, I basically want to move an image around on the screen, in a region (advanced, I know ). I achieved this just fine with mouse click and drag, but I'm unable to get any sort of keyboard input. Here's the part of the code I'm using:
protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; using(Region boardRegion = new Region(new Rectangle(ClientRectangle.X, ClientRectangle.Y, (int)(ClientRectangle.Width), (int)(ClientRectangle.Height \* boardRegionSize)))) { g.Clip = boardRegion; using(Bitmap boardImage = new Bitmap(board.ImagePath)){ g.DrawImage(boardImage, board.X, board.Y); } } base.OnPaint(e); } private void CMGame\_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Up) board.Y -= 30; }
And for part of InitializeComponent(), I have
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.CMGame_KeyDown);
(automatically done by windows) Basically, nothing happens when I press a key (the up arrow or anything else, since I've tried different keys in testing) I read somewhere that I could try overriding WndProc, but I don't know how I would do this (even after I wrote the override method header and and found the code for KeyDown, 0x0100) For reference, I also have a button in the form with a click event. Help would be much appreciated.
-
Hi, I'm a rather new C# user and I'm still figuring out the ropes of forms programming and such. For the part of the program I'm working on, I basically want to move an image around on the screen, in a region (advanced, I know ). I achieved this just fine with mouse click and drag, but I'm unable to get any sort of keyboard input. Here's the part of the code I'm using:
protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; using(Region boardRegion = new Region(new Rectangle(ClientRectangle.X, ClientRectangle.Y, (int)(ClientRectangle.Width), (int)(ClientRectangle.Height \* boardRegionSize)))) { g.Clip = boardRegion; using(Bitmap boardImage = new Bitmap(board.ImagePath)){ g.DrawImage(boardImage, board.X, board.Y); } } base.OnPaint(e); } private void CMGame\_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Up) board.Y -= 30; }
And for part of InitializeComponent(), I have
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.CMGame_KeyDown);
(automatically done by windows) Basically, nothing happens when I press a key (the up arrow or anything else, since I've tried different keys in testing) I read somewhere that I could try overriding WndProc, but I don't know how I would do this (even after I wrote the override method header and and found the code for KeyDown, 0x0100) For reference, I also have a button in the form with a click event. Help would be much appreciated.
You need to tell the control that's holding the image that is needs to be redrawn either by calling it's invalidated method or calling onpaint directly depending on the situation. You could hook this up to be done automatically by putting it in a locationchanged event which you can fire whenever X or Y is changed
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog) -
You need to tell the control that's holding the image that is needs to be redrawn either by calling it's invalidated method or calling onpaint directly depending on the situation. You could hook this up to be done automatically by putting it in a locationchanged event which you can fire whenever X or Y is changed
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog)Right, I actually had that earlier before I started messing with things. Even with this.Invalidate() after it, nothing happens. I think that the event isn't being fired at all, since, for parts of my test, I just told the keydown, keypress, and keyup to change the text of the form. Nothing happened anytime I pressed a button.
-
Right, I actually had that earlier before I started messing with things. Even with this.Invalidate() after it, nothing happens. I think that the event isn't being fired at all, since, for parts of my test, I just told the keydown, keypress, and keyup to change the text of the form. Nothing happened anytime I pressed a button.
Ok, I found my problem. Once I removed my button, the key input worked again. Can anyone tell me what else would have worked?
modified on Sunday, August 17, 2008 7:36 PM
-
Ok, I found my problem. Once I removed my button, the key input worked again. Can anyone tell me what else would have worked?
modified on Sunday, August 17, 2008 7:36 PM
Is it a focus problem? Was the button getting the keyboard input? If so, setting the form's KeyPreview property to true might be the solution.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog) -
Is it a focus problem? Was the button getting the keyboard input? If so, setting the form's KeyPreview property to true might be the solution.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog)Thanks, setting the KeyPreview to true worked perfectly for me.