Author Topic: KeyMapper Class  (Read 2959 times)

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
KeyMapper Class
« on: June 22, 2009, 11:44:50 pm »
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.

Code: [Select]
      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));

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: KeyMapper Class
« Reply #1 on: June 23, 2009, 07:09:00 am »
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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: KeyMapper Class
« Reply #2 on: June 23, 2009, 08:27:26 am »
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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: KeyMapper Class
« Reply #3 on: August 07, 2009, 11:34:47 pm »
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?
« Last Edit: August 08, 2009, 01:06:25 am by AGP »