I have some code that I need to modify. In the code, the original author uses KeyStroke.getKeyStroke
to take user input. In this code, for example, he uses a
instead of left arrow.
I want to change this, but I don't know how.
Here is the original code:
registerKeyboardAction(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
tick(RIGHT);
}
}, "right", KeyStroke.getKeyStroke('d'), WHEN_IN_FOCUSED_WINDOW
);
I have to change it to something like this, but when run, it doesn't work:
KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT);
KeyStroke.getKeyStroke("RIGHT");
You should be able to use
KeyStroke.getKeyStroke("DOWN");
,"UP"
,"LEFT"
,"RIGHT"
, to do what you want.See the javadoc for more detail.
Do start the program by pressing the DOWN
ARROW KEY
, to watch the string first. Here have a look at this example program :