Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OnKeyDown problem....

Status
Not open for further replies.

lobodo44

Technical User
Sep 8, 2003
27
CA
Hi there,
I'm trying to make something move with the keyboard but i can't get that OnKeyDown to work. I tried to put the function to move on the OnKeyDown of teh form but it doesn't execute at all.

Thanks for your help



Laurent
 
> I tried to put the function to move on the OnKeyDown of teh form but it doesn't execute at all.

The OnKeyDown event of the form will only fire if there is no other component on the form that has focus. If there is another component that has focus, the OnKeyDown event of that component fires, not the OnKeyDown event of the form. I suspect that this is what is happening.

Good Luck.
 
thanks for your response
I'm trying to make a little game whit an image in background so how can I catch the keyboard or remove the focus thing on my object
 
You can just use the OnKeyDown event of the object that has focus.
 
Set KeyPreview to true in the forms object inspector.
 
If you want to make a game, you must use a loop where you can call the win api function GetAsyncKeyState.
Or you can use a timer to call GetAsyncKeyState.

The GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState.

SHORT GetAsyncKeyState(
int vKey // virtual-key code
);

VK_LEFT 25 LEFT ARROW key
VK_UP 26 UP ARROW key
VK_RIGHT 27 RIGHT ARROW key
VK_DOWN 28 DOWN ARROW key

For more details read Windows SDK.

normal c++ programmer
 
Thank you guyz now it worked with the keypreview. But now ihave a problem : the game slow down when i keep the button pressed. I have a background that is running with a timer to move it. I don<t if the thing you speaking of (ICounter) can remove that.
 
If you don't try, you don't know !!
I'm using GetAsyncKeyState at 50 miliseconds in a timer, and it doesn't slow down my application!


normal c++ programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top