DirectInput Multi-Key

Started by
3 comments, last by Ryan Clark 19 years, 7 months ago
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.
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.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

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
--Visit the Game Programming Wiki!
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 :)
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
--Visit the Game Programming Wiki!

This topic is closed to new replies.

Advertisement