hi, im creating a simple game here...its a shooting game...so how can I make my program read the keyboard to control the player's movement around?tnx in advance

8 Years
Discussion Span
Last Post by Duoas

I think I understand what u mean.
I remember the old days when I was doing a simple "snake" game in pascal.
Back then I was able to do something like

.......move snake in one direction.....
until keypressed();
..... process input key and change dirrection....
until forever

or something like it... The thing was that the snake was moving ( implemented in the loop ) and when an arrow key was pressed the direction would heve been changed. Ok, the only problem is that i don't know any standard C function that would do the samething thing as keypressed in pascal. The only alternative I can think about is having another thread that listens to user input, push the event in a global queue and have the "game running" thread process the event queue with user input. This is a more general approach, but I'm still wondering if C has any function like keypressed() :) . So if anybody can enlighten me... :)


Yes, you really are asking an OS and SDK-dependent question.

If you are using something like SDL, then respond to the key events that SDL gives you. Most GUI/graphics toolkits will give you a way to hook into keyboard events.

If you are using straight Win32, look for WM_KEYDOWN events in your message loop.

If you are using X, make sure you have selected an appropriate KeyPressMask and watch for KeyPress events in your main event loop.

If you are using something else, please let us know.

Votes + Comments
I knew it - thanks for the thorough explanation =)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.