Jump to content
  • Advertisement
Sign in to follow this  
EasyGino

DirectInput Multi-Key

This topic is 5195 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey boys and girls, stupid question, but how do you do multi-key movements and stuff, so i can have 2 keys pressed and do a certain task. Im using directinput from dx8.

Share this post


Link to post
Share on other sites
Advertisement
When I've used DirectX keyboard stuff, I've tied two event handlers to the keyboard - one for keys being pressed, another for keys being released. Let's say you have an (x,y) coordinate for something. I'd have two extra variables - dx and dy, which are added to the x and y once a "game loop". When I trap a key pressed down, I then check the key- if it's left, dx=-1, if it's right, dx=1, id it's up, dy=-1 and if it's down dy=1.
In the key release event, I'll check which key again - if it's left or right, dx=0 and if it's up or down dy=0. This is the easiest way, but doesn't check for press-hold-up-then-tap-and-release-down-whilst-still-holding-up type situations (you'd expect the object to keep moving up, it'd actually just stop). I don't know if DX has this built-in, but you could always create a 256-long array of booleans and store whether a key is pressed or not into that, then check those as you run your game loop.

Share this post


Link to post
Share on other sites
A word of caution on keyboard input: If you're having trouble getting your keyboard to register multiple keypresses simultaneously, it may be a hardware issue with your keyboard. Various keyboards have difficulties with certain keypress combinations. For example, my keyboard cannot handle the LEFT-ARROW and UP-ARROW at the same time as the spacebar.

You should always allow your users to re-define your game's keyboard mapping, to ensure that strange harware issues such as this won't prevent them from playing the game!


Ryan

Share this post


Link to post
Share on other sites
Quote:
Original post by Ryan Clark
For example, my keyboard cannot handle the LEFT-ARROW and UP-ARROW at the same time as the spacebar.


I'm glad that's a hardware thing as I was about to set off trying to fix that 'bug' in my code :)

Share this post


Link to post
Share on other sites
Hehe, I'm glad you didn't have to pull your hair out over that "bug" :)

There are certain key combinations that seem to be ideal for games. I've never seen a keyboard where the CTRL, ALT or SHIFT keys conflicted with any of the arrow keys, making these ideal for "weapons" keys. Using those keys as the default setup for your game will likely result in fewer frustrated users!


Ryan

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!