944,148 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 2478
  • C# RSS
Nov 2nd, 2009
0

2 keys down

Expand Post »
hi...i'm making a game that uses the direction arrows and fires with space...the problem is that when i keep holding 2 keys it makes only the behavior of one....can any1 tell me what i shall do
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kadamora is offline Offline
10 posts
since Jan 2009
Nov 2nd, 2009
0
Re: 2 keys down
Click to Expand / Collapse  Quote originally posted by kadamora ...
hi...i'm making a game that uses the direction arrows and fires with space...the problem is that when i keep holding 2 keys it makes only the behavior of one....can any1 tell me what i shall do
Could you post the code you're using to track key up/down status? That would help us find what's going wrong.
Reputation Points: 182
Solved Threads: 72
Posting Pro in Training
gusano79 is offline Offline
476 posts
since May 2004
Nov 2nd, 2009
0
Re: 2 keys down
AFAIK Normal Windows development (e.g. what you get with normal Visual Studio installations) does not handle multiple key presses, not including modifier keys (ctrl, alt, and shift.) The problem is Windows supports two types of key press events: KeyDown/Up and Repeat. Key up and down is great, but if you hold a key down, it fires Repeat until that key is let go. Not handy if you are trying to hold 2 keys down at once.

In order to handle multiple simultaneous non-modifier keys, you need to use the DirectInput portion of the DirectX SDK (google directx sdk directinput), and specifically the GetCurrentKeyboardState function.

There are definitely other ways to do this (I am sure there are a lot of existing game engines out there) but the Microsoft Way (TM) is DirectX. Other alternatives will probably appear if you search "windows game engine" , "windows game sdk" or "windows alternative keyboard handler." Good luck!
Reputation Points: 27
Solved Threads: 17
Junior Poster in Training
mikiurban is offline Offline
63 posts
since Oct 2009
Nov 2nd, 2009
0
Re: 2 keys down
Click to Expand / Collapse  Quote originally posted by gusano79 ...
Could you post the code you're using to track key up/down status? That would help us find what's going wrong.
sure...here it is...
i use speed to accelerate
C# Syntax (Toggle Plain Text)
  1. void nagy_KeyDown(object sender, KeyEventArgs e)
  2. {
  3.  
  4. if (e.KeyCode == Keys.Left)
  5. {
  6. leftspeed += 5;
  7. rightspeed = 0;
  8.  
  9.  
  10. }
  11.  
  12. if (e.KeyCode == Keys.Right)
  13. {
  14. rightspeed += 5;
  15. leftspeed = 0;
  16. }
  17. if (e.KeyCode == Keys.Space)
  18. {
  19. leaser = true;
  20. }
  21.  
  22.  
  23. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kadamora is offline Offline
10 posts
since Jan 2009
Nov 2nd, 2009
1
Re: 2 keys down
Click to Expand / Collapse  Quote originally posted by mikiurban ...
AFAIK Normal Windows development (e.g. what you get with normal Visual Studio installations) does not handle multiple key presses, not including modifier keys (ctrl, alt, and shift.)
To clarify, the .NET Framework doesn't provide automatic handling of multiple key presses. You can still handle them yourself.

Click to Expand / Collapse  Quote originally posted by mikiurban ...
The problem is Windows supports two types of key press events: KeyDown/Up and Repeat. Key up and down is great, but if you hold a key down, it fires Repeat until that key is let go. Not handy if you are trying to hold 2 keys down at once.
This is what's keeping your code from working right--when you hold one key down and press another at the same time, you stop getting repeats from the first key, and start getting them from the second.

As long as your game logic is coupled to keyboard event handling, you'll always need a new key event to do something in game logic. Because you never get repeats for more than one key at a time, you won't be able to do two actions at once.

The solution? Decouple keyboard event handling from game logic.

First, you need a place to keep track of which keys are currently down. If you only have a few keys you're interested in, you might have a couple of lines like this:

C# Syntax (Toggle Plain Text)
  1. private bool spaceKeyDown = false;

Then, handle the "key down" event:

C# Syntax (Toggle Plain Text)
  1. private void MainFormKeyDown(object sender, KeyEventArgs e)
  2. {
  3. if(e.KeyCode == Keys.Space)
  4. {
  5. spaceKeyDown = true;
  6. }
  7. }

Handle the corresponding "key up" event:

C# Syntax (Toggle Plain Text)
  1. private void MainFormKeyUp(object sender, KeyEventArgs e)
  2. {
  3. if(e.KeyCode == Keys.Space)
  4. {
  5. spaceKeyDown = false;
  6. }
  7. }

Then the trick is to run your game logic in a separate thread, for example, with a Timer:

C# Syntax (Toggle Plain Text)
  1. private void KeyMonitorTick(object sender, EventArgs e)
  2. {
  3. if(spaceKeyDown)
  4. {
  5. // do something useful here
  6. }
  7. }

This way, the timer tick method is able to check the "down" status of any key you've tracked using the key handlers, and you can handle more than one key being down at a time.

There are a few ways to skin this particular cat--some are easier to code, some are more general-purpose, others are more thread-safe... attached is a sample Windows Forms application that tracks multiple keypresses in a slightly different way than the above code.

Final note: There will be certain combinations of keys you won't ever be able to handle simultaneously, simply because keyboards won't send key events for them. It may differ from keyboard to keyboard, and usually they're reasonable. For example, I can hold any two (and sometimes three) arrow keys down on the keyboard I'm using right now and get proper events for those keys, but I never get "key down" events from all four simultaneously.
Attached Files
File Type: zip MultipleKeys.zip (20.5 KB, 186 views)
Reputation Points: 182
Solved Threads: 72
Posting Pro in Training
gusano79 is offline Offline
476 posts
since May 2004
Nov 3rd, 2009
0
Re: 2 keys down
this is so great......i'm very grateful to you, and the program
thnx alot
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kadamora is offline Offline
10 posts
since Jan 2009
Nov 3rd, 2009
0
Re: 2 keys down
Click to Expand / Collapse  Quote originally posted by gusano79 ...
To clarify, the .NET Framework doesn't provide automatic handling of multiple key presses. You can still handle them yourself.

<snip>

This is what's keeping your code from working right--when you hold one key down and press another at the same time, you stop getting repeats from the first key, and start getting them from the second.

<snip>

The solution? Decouple keyboard event handling from game logic.
Thanks Gusano, for clarifying, and giving an excellent example.
Reputation Points: 27
Solved Threads: 17
Junior Poster in Training
mikiurban is offline Offline
63 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Know when sql server data has changed
Next Thread in C# Forum Timeline: Listview item display





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC