I have this problem with my code, I'm trying to learn how to use keystrokes in Java, and i want to be able to track which keystrokes I'm pressing. I'm trying to use KeyEvent.VK_UP to track what I'm pressing.
import java.awt.event.*;
import javax.swing.*;
public class TrackArrows
{
protected static InputMap inputMap;
public static void main(String[] arg){
JPanel panel = new JPanel();
inputMap = panel.getInputMap();
panel.getActionMap().put("keys", new AbstractAction() {
public void actionPerformed(ActionEvent e){
if(inputMap.get(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), true)){//My problem lies here
System.out.println("Key pressed up");
}
if(inputMap.get(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), true)){//And here
System.out.println("Key pressed down");
}
}
});
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "keys");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "keys");
JFrame frame = new JFrame();
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(20,20);
frame.setVisible(true);
}
}
Am i wrong in doing this, or is there another way to do this?