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. 4 13020
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]
"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!)
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]
"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! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Bilal |
last post by:
Hi
I have an application with numerous pages. A couple of these pages
include a functionality that allows it to be refreshed every 15 secs.
My problem is that this "refreshing" works too well: THe...
|
by: hpi |
last post by:
Hi,
I have 2 listboxes (and some other stuff) on a form
The first listbox has an onclick event which, when fired, adds an item to
the second list box and sets the selected item to the last item...
|
by: CLH |
last post by:
I've written a project that uses a notify icon. The program
periodically checks my SQL server, and when certain criteria are met, it
pops up a reminder that a task needs to be completed.
So far,...
|
by: Sameh Ahmed |
last post by:
Hello there
I there a way to use ScrollToCaret without the control in focus?
Any suggestions?
Regards
Sameh
|
by: learner404 |
last post by:
Hello,
I have a Windows Pyhon-Tk app which need to capture mouse end keyboard
events even when the app is not in focus.
On Windows so far I've used the excellent PyHook which does the job...
|
by: eliss.carmine |
last post by:
Is it possible to simulate a mouse click in the window I made (it's a
Form), but not give it focus?
I tried using WinAPI's mouseevent and SendMessage of
WM_LBUTTONDOWN/WM_LBUTTONUP as suggested...
|
by: Luke R |
last post by:
How do i do this?
I have this at the top of the class for my form, but it doesnt work.
Protected Overrides ReadOnly Property ShowWithoutActivation() As Boolean
Get
Return True
End Get
End...
|
by: noon |
last post by:
Can't get this to work, tried every bit of code I could think of and
every snippet I could find out there.
I have a form with the js code in the onsubmit action. I know, I
shoved it all in...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |