keypress - Java while ( keyispressed ) -


how write code loops while left or right arrow key pressed?

add keylistener swing component (assuming you're using swing), , mark keydown , keyup event. specifically, on keydown set boolean movingleft, , on keyup unset boolean.

a better solution might use map of enumerations of directions booleans, make code cleaner.

example:

map<movedirection, boolean> movemap = new hashmap<movedirection,boolean>(); movemap.put( movedirection.left, false ); movemap.put( movedirection.right, false ); movemap.put( movedirection.up, false ); movemap.put( movedirection.down, false ); 

then put , get necessary.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -