By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,098 Members | 1,918 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,098 IT Pros & Developers. It's quick & easy.

KeyDown without focus?

P: n/a
I'm building a simple "breakout" style video-game, and I want keyboard
control of the paddle.

What I need is for an event handler to respond whenever a key is pressed,
regardless of what the focus is set to. So far, the best I've been able to
accomplish is to create a button and have that button handle the KeyDown
event. I've tried using KeyDown on the form, but it simply doesn't work.

I did a search on this, and found on site that suggested overriding the
system "OnKeyDown" event handlers - but I haven't gotten this to work,
either. It could be because I'm putting it in the wrong place, or perhaps
it's just not the way to do it.

I'm using Visual C# Studio - the new Beta edition, if that makes a
difference. Any suggestions would be greatly appreciated.
Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi Tony,

Did you set the Form.KeyPreview property to true? The parent form should then be notified of key events, even though a child control has focus.

To be able to use OnKeyDown you need to inherit from some Control and override the base KeyDown event. You can do that in the parent form

//Inside Form1
protected override OnKeyDown(KeyEventArgs e)
{
}

However, the effect is very much like

Form1.KeyDown += new KeyEventHandler(Form1_KeyDown);
....
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
}

On Fri, 27 May 2005 23:30:33 +0200, Tony <so*****@somewhere.not> wrote:
I'm building a simple "breakout" style video-game, and I want keyboard
control of the paddle.

What I need is for an event handler to respond whenever a key is pressed,
regardless of what the focus is set to. So far, the best I've been able to
accomplish is to create a button and have that button handle the KeyDown
event. I've tried using KeyDown on the form, but it simply doesn't work.

I did a search on this, and found on site that suggested overriding the
system "OnKeyDown" event handlers - but I haven't gotten this to work,
either. It could be because I'm putting it in the wrong place, or perhaps
it's just not the way to do it.

I'm using Visual C# Studio - the new Beta edition, if that makes a
difference. Any suggestions would be greatly appreciated.


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #2

P: n/a
"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op.srgnuztdklbvpo@stone...
Hi Tony,

Did you set the Form.KeyPreview property to true? The parent form should
then be notified of key events, even though a child control has focus.
No - none of the samples I found mentioned that at all. Would the default
for that be either false or null, then?
To be able to use OnKeyDown you need to inherit from some Control and
override the base KeyDown event. You can do that in the parent form

//Inside Form1
protected override OnKeyDown(KeyEventArgs e)
{
}


I already had that - but I needed the Form.KeyPreview to be changed.

That seems to have done the trick for the immediate problem - thanks!
Now, I have another problem - I can't read the arrow keys, or Enter or Tab.

Right now, I just have a label showing the text - the event handler is just:

whichkey.Text = keyArgs.KeyData.ToString();

(keyArgs is set earlier).

This gives me "Space" for the space bar, "Escape" for the escape key, etc -
but nothing for Tab, Enter, or the four arrow keys.

I'm guessing I'm missing something on those, too, but I have no idea what it
could be... (in the meantime, I can at least use the numeric keypad for
direction control now - thanks again!)
Nov 17 '05 #3

P: n/a
On Sat, 28 May 2005 05:14:59 +0200, Tony <so*****@somewhere.not> wrote:
"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op.srgnuztdklbvpo@stone...
Hi Tony,

Did you set the Form.KeyPreview property to true? The parent form should
then be notified of key events, even though a child control has focus.


No - none of the samples I found mentioned that at all. Would the default
for that be either false or null, then?


The default would be false.
To be able to use OnKeyDown you need to inherit from some Control and
override the base KeyDown event. You can do that in the parent form

//Inside Form1
protected override OnKeyDown(KeyEventArgs e)
{
}


I already had that - but I needed the Form.KeyPreview to be changed.

That seems to have done the trick for the immediate problem - thanks!
Now, I have another problem - I can't read the arrow keys, or Enter or Tab.

Right now, I just have a label showing the text - the event handler is just:

whichkey.Text = keyArgs.KeyData.ToString();

(keyArgs is set earlier).

This gives me "Space" for the space bar, "Escape" for the escape key, etc -
but nothing for Tab, Enter, or the four arrow keys.

I'm guessing I'm missing something on those, too, but I have no idea what it
could be... (in the meantime, I can at least use the numeric keypad for
direction control now - thanks again!)


Hm, for me it traps all keys except TAB.
However, there is also ProcessCmdKey

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
return base.ProcessCmdKey (ref msg, keyData);
}

This gave me TAB as well.

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #4

P: n/a
"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op.srgzc7apklbvpo@stone...


Hm, for me it traps all keys except TAB.
However, there is also ProcessCmdKey

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
return base.ProcessCmdKey (ref msg, keyData);
}

This gave me TAB as well.


Thanks again - that did everything I needed!
Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.