| | |
2 keys down
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2004
Posts: 80
Reputation:
Solved Threads: 5
0
#2 25 Days Ago
Could you post the code you're using to track key up/down status? That would help us find what's going wrong.
--smg
•
•
Join Date: Oct 2009
Posts: 62
Reputation:
Solved Threads: 15
0
#3 25 Days Ago
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!
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!
•
•
Join Date: Jan 2009
Posts: 8
Reputation:
Solved Threads: 0
0
#4 25 Days Ago
•
•
•
•
Could you post the code you're using to track key up/down status? That would help us find what's going wrong.
i use speed to accelerate
C# Syntax (Toggle Plain Text)
void nagy_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Left) { leftspeed += 5; rightspeed = 0; } if (e.KeyCode == Keys.Right) { rightspeed += 5; leftspeed = 0; } if (e.KeyCode == Keys.Space) { leaser = true; } }
•
•
Join Date: May 2004
Posts: 80
Reputation:
Solved Threads: 5
0
#5 25 Days Ago
•
•
•
•
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.
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)
private bool spaceKeyDown = false;
Then, handle the "key down" event:
C# Syntax (Toggle Plain Text)
private void MainFormKeyDown(object sender, KeyEventArgs e) { if(e.KeyCode == Keys.Space) { spaceKeyDown = true; } }
Handle the corresponding "key up" event:
C# Syntax (Toggle Plain Text)
private void MainFormKeyUp(object sender, KeyEventArgs e) { if(e.KeyCode == Keys.Space) { spaceKeyDown = false; } }
Then the trick is to run your game logic in a separate thread, for example, with a Timer:
C# Syntax (Toggle Plain Text)
private void KeyMonitorTick(object sender, EventArgs e) { if(spaceKeyDown) { // do something useful here } }
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.
--smg
•
•
Join Date: Oct 2009
Posts: 62
Reputation:
Solved Threads: 15
0
#7 24 Days Ago
•
•
•
•
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.
![]() |
Similar Threads
- How to Quickly Lock Your Computer and Use Other Windows Logo Shortcut Keys (Windows tips 'n' tweaks)
- Turn On the Sticky Keys Feature (Windows tips 'n' tweaks)
- special keys as inputs (Game Development)
- Florida Keys/Swap Links=) (Relevant Link Exchanges)
- Acer laptop keys not working (Troubleshooting Dead Machines)
- Can't get multimedia keys to work on mac keyboard (Apple Hardware)
Other Threads in the C# Forum
- Previous Thread: Know when sql server data has changed
- Next Thread: Listview item display
| Thread Tools | Search this Thread |
.net access algorithm array asp.net barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom database databaseconnection datagrid datagridview dataset datetime dbconnection degrees design development draganddrop drawing encryption enum event eventhandlers excel file firefox form format forms function gdi+ grantorrevokepermissionthroughc#.net httpwebrequest image index input install java label libraries list listbox loop mandelbrot marshalbyrefobject math mouseclick movingimage mysql mysql.data.client operator path photoshop picturebox pixelinversion platform post programming radians regex remote remoting resourcefile richtextbox server sleep socket sql statistics stream string system.servicemodel table tcpclientchannel text textbox thread time timer update usercontrol validation visualstudio webbrowser windows winforms wpf wpfc# xml






