I’m trying to do a very simple thing : trigger an action when the user presses on a key of the keyboard
the keys I would like to map are :
- key +
- key –
- key delete
- ctrl+c
-
ctrl+v
public class keytestmain extends Applet{
//Called when this applet is loaded into the browser. public void init() { //Execute a job on the event-dispatching thread; creating this applet's GUI. try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { JLabel lbl = new JLabel("Hello World"); add(lbl); JPanel p = new JPanel(); p.setBackground(Color.green); p.setMinimumSize(new Dimension(100,100)); p.setPreferredSize(new Dimension(100,100)); p.setMaximumSize(new Dimension(100,100)); InputMap inputMap = new InputMap(); // Add a KeyStroke inputMap.put(KeyStroke.getKeyStroke("SPACE"), "actionName"); inputMap.setParent(p.getInputMap(JComponent.WHEN_FOCUSED)); p.setInputMap(JComponent.WHEN_FOCUSED, inputMap); add(p); } }); } catch (Exception e) { System.err.println("createGUI didn't complete successfully"); } }}
but nothing works
Any ideas?
I would use Key Bindings, instead of KeyListeners. This is more reliable as it doesn’t have many focus issues. Also KeyListeners is a relatively old AWT solution, so depending on your JDE, the command may have been depracated.