View Full Version : Multiple Key Presses

12-30-2013, 03:38 AM

I need help with handling multiple key presses for my platformer. I've looked in the example 'Input' project, and maybe I've missed it because I have had this problem for a while now, but haven't quite gotten it yet so I decided to ask for help.

My situation is that I want to press the 'A' or 'D' key to move left or right respectively and then press 'W' to jump or 'Shift' to increase running speed. I only have one slot connected by the following:

slots.connect(window.get_ic().get_keyboard().sig_k ey_down(), this, &AnimateImage::KeyInput);

The function 'KeyInput' of course checks by if conditions and go from there. But if holding down 'D' and then I press 'W', it cancels my 'D' movement so then I only jump while loosing speed. Is there another way to go about this, should I set bools to true if key down and false on key release, or is there something I am missing?

Thanks everyone

12-30-2013, 04:13 PM
ClanLib's input manager executes the callback for every key you push down and release. When you press down two keys at the same time, the callback will be executed twice for each of the keys. What I would do is make a class that registers what inputs is being pressed down (through ClanLib calling a function in that class), and then have the rest of the game code read from that class to decide what to do. The class would have something like std::map<KeyCode, KeyStatus> that maps a keycode to it's status (pressed or not pressed).

12-30-2013, 05:20 PM
You can also use polling:

if(keyboard.get_keycode(clan::KEY_D)) ...

More info at http://clanlib.org/documentation/3.0/UserInput.html

01-02-2014, 06:04 AM
Alrighty, I will consider which approach would be the best and design my code from there.

Thanks for the help and ideas! :)