nightrobin at October 29th, 2012 08:20 — #1
I just want to ask if can I check for "KEY"(keyboard) that is HOLD/PRESSED for a long time, because I am to create a clone of breakout with air hockey for 2 different human players.
Here's the list of my concern:
Do I need other/ 3rd party library for KEY HOLDS?
Is multi-threading needed? I don't know anything about this multi-threading stuff and
I don't think about using one(I'm just a NEWBIE).
One more thing, what if the two players pressed their respective key at the same time,
how can I program to avoid error or worse one player's key is prioritized first before
the the key of the other.
Player 1 = W for UP & S for DOWN
Player 2 = O for UP & L for DOWN
(example: W & L is pressed at the same time)
Great thanks for help.
PS: I use GLUT for the visuals of the game.
thenut at October 29th, 2012 11:21 — #2
I don't remember the specifics of how GLUT manages keyboard input, but in general:
No, you shouldn't need a 3rd party library. GLUT already supports this. See this.
No, you don't need to thread this. Nothing you do in the keyboard event callback should delay the processor. AI and physics are examples of things you would offload to a separate thread. Window management and user input should be handled in the same thread (typically the main thread).
Keys are sent in the order they were pressed on the keyboard. All you have to do is create the necessary logical states for each of your player objects and update them as keyboard input is received. You only have to check for one key-down and one key-up event. I'm not sure which events GLUT is hooked into, but it's possible you may receive multiple key-down events on the last keyboard key that is held down by the user. The speed of this is actually controlled by your OS (check your keyboard control panel). Nevertheless, you should ignore this behaviour. Game logic should not depend on repetitive key events sent from the OS. On the first press, update a boolean flag or logical state. When that key is released, once again update your flag or state.
nightrobin at October 31st, 2012 09:44 — #3
thank you very much