Sign in to follow this  
CuppoJava

Keyboard Input Parsing

Recommended Posts

Hey guys, I'm programming a 2D fighter-esque game, and I'm running into a little difficulty parsing the keyboard controls. Would anyone volunteer to explain how they are doing it? I have a function called wasKeyPressed(), which returns whether a key has been pressed since the input was last polled. I do this by checking whether the key is DOWN in the current frame, and UP in the previous frame. Now this worked until I tried to do double-clicks and stuff. Often, the user is able to press, release, then press again the key during the same frame, in which case, I can't detect the double-press. Just wondering what everyone else is using in terms of keyboard control. Do you guys use polling methods such as wasKeyPressed() or isKeyDown()? or do you use a Event Listener interface? Thanks a lot for sharing your knowledge -Cuppo

Share this post


Link to post
Share on other sites
Yeah, I'm about to switch to using buffered inputs. I'm just wondering what's a nice clean way of doing it.

Do games ever use an event listener scheme for Keyboard controls?

eg. Attach a listener to the controller class, then have the controller class call the keyPressed(KeyEvent k), keyReleased(KeyEvent k) methods inside the listener?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I'm not developing a game, although what i'm developing could be used to create games. My system is event driven, so I do use observer/listeners to raise events that can be handled by user created script.

However, I don't rely on Windows messages because of the lag with keyboard repeat. My main loop polls the keyboard and then places events in an event queue to be processed by my core.

Share this post


Link to post
Share on other sites
Quote:
Original post by CuppoJava
Yeah, I'm about to switch to using buffered inputs. I'm just wondering what's a nice clean way of doing it.

Do games ever use an event listener scheme for Keyboard controls?

eg. Attach a listener to the controller class, then have the controller class call the keyPressed(KeyEvent k), keyReleased(KeyEvent k) methods inside the listener?


With the project I'm currently working on that is almost exactly what I am doing. I also use the OIS library so I can seamlessly move between Linux and Windows without having to alter the code (I don't think it has Mac support as of yet)

My project is driven by game states, and cGameManager and cGameState inherit from OIS::MouseListener, OIS::KeyboardListener and OIS::JoystickListener. This allows the current game state to handle all input messages seamlessly. If I need unbuffered input, its just a case of reading from the input device directly (admittedly my cInputManager class is a singleton =/)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this