Skip to main content
gamedev.net
🔒 Locked

keyboarding

Started by ehmdjii 4 replies 600+ views
Original Post
ehmdjii
ehmdjii
hello, what''s the best way to handle keys when programming a game? so far i always did it this way in the main message loop: ( saw it at nehe ) if(keys[''C''] && !c_pressed) { c_pressed=true; //...do something here } if(!keys[''C'']) { c_pressed=false; } but this is getting kinda annoying cause i need one extra variable for each key and this is only processed once every frame. so if the framerate is very low, chances are good the keypressing isnt realised at all. also, i dont know if this is the usual way to do it at all. how do you do it? thanks!
Ekim_Gram
Ekim_Gram
I usually like to do a switch-case block when checking keystrokes. It''s much cleaner and takes less to code.



R.I.P. Mark Osback
Solo Pa Mi Gente
VG-Force
ehmdjii
ehmdjii
ok, but where do you do it?
and how do you check if a key is held?
Rick Scott
Rick Scott
I have an input class that has many methods, 2 of which are GetInputState() and GetInputEvents(). State retrieves the state of keys at that time, events retrieves what has changed since the last time I called the function. So in a main game-loop you can do state(), but for menus and such you can use events(). I do this by keeping a seperate copy of the keyboard state as a static variable in events() and comparing it to the actual kb state, then returning the result.
alnite
alnite
quote:
Original post by ehmdjii
but this is getting kinda annoying cause i need one extra variable for each key and this is only processed once every frame. so if the framerate is very low, chances are good the keypressing isnt realised at all.
Isn''t that what happening to most games? When everything goes really slow, you can hardly control the game.

Anyway, if you use DirectInput, there is a method called buffered reading. DirectInput will send the messages sent by input devices to a buffer, and your job is to read the buffer. So, in any case the frame rate drops low, if the player presses ''a'' 5 times, there will be 5 a''s in the buffer.

But of course, if the buffer is full, the messages are lost.
psykr
psykr
Personally, I''m trying to keep the various subsystems of my engine very encapsulated. I''m planning to have my input class update the state of the key buffer, then pass the information to an input-translating object, which will handle key bindings and whatnot, then turn keypresses into game events.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.