KeyMapper Class

Started by AGP, June 22, 2009, 11:44:50 PM

Previous topic - Next topic

AGP

Egon suggested I used the KeyMapper class to improve keyboard-input-reading for the racing game, and so it is that I am. Problem is, the input is at least as bad as it was before with the following code. The idea is that the car speeds up while you're holding down the UP key and slows down when you're holding DOWN, but in practice it only speeds up and slows down when I press, release, and press again. keyPressed at VK_UP sets speedingUp to true, and at VK_DOWN sets slowingDown to true. Shouldn't the car speed up with every loop iteration? It turns just fine. And I did remove the awt KeyListener, by the way, I'm just using its keyPressed because I'm being lazy.


      KeyState state = keyMapper.poll();
      if (state.getState() == KeyState.PRESSED || (speedingUp && state.getKeyCode() == KeyEvent.VK_UP) || (slowingDown && state.getKeyCode() == KeyEvent.VK_DOWN))
keyPressed(new KeyEvent(this, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, state.getKeyCode(), KeyEvent.CHAR_UNDEFINED));

EgonOlsen

Use flags instead, i.e. in a kind of poll method, you set a flag for "up" if up gets a keypressed-event and clear the flag if it gets a keyreleased event. In your main loop, simply evaluate the state of that flag, not of the keyevent itself. That should give you smooth movement.

AGP

That did it, thank you.

And man, Mizuki just showed me that jPCT off-road car game. Wow, I'm going to have to to try to step up to dmouse's level. Very, very, cool, if you read this, dmouse.

AGP

#3
Is there a way to get SHIFT and CONTROL from KeyState that I'm missing? I need them, and KeyEvent provides methods such as isShiftDown() to test parallel to the keyCode, but KeyState doesn't.

I figure SHIFT AND CONTROL are queued along with everything else, but wouldn't it make more sense to have two parallel queues, one just for the modifiers, to ease the input-reading as well as the porting of the code between KeyListener and the KeyMapper?