multiple key detection for KeyListener (java)

后端 未结 2 1511
时光取名叫无心
时光取名叫无心 2021-01-26 23:39

How would one implement KeyListener so that I can create a two-player system where one person uses \'.\' and \'/\' to control a character, and the other person can use the arrow

2条回答
  •  广开言路
    2021-01-27 00:08

    From the sounds of things you're listening to a keyPressed event. Basically, you need to maintain stateful information about what keys are currently "down" and only stop the appropriate action when the keyReleased event occurs.

    This will require to have two seperate lines action handler, one for when the key is pressed and one when the key is released.

    One of the other things you might need to do is maintain some kind of cache of the active keys...which just got mentioned by Ethan as I was typing :P

提交回复
热议问题