Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I created my own SSCCE and on testing, it seems that regardless of what technique is used, the applet still must request and gain focus for them to work. I've gotten this to successfully work on my windows system using either of two kludges, including a javax.swing.Timer that requests focus after xxx ms, or an override of the JApplet's <code>paint(...)</code> method that requests focus the first time paint is called (and when the applet is just being rendered). For example, showing both kludges (only one would be required) and an example of key bindings and of an SSCCE:</p> <pre><code>import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.lang.reflect.InvocationTargetException; import javax.swing.*; @SuppressWarnings("serial") public class KeyBindingEg extends JApplet { protected static final int TIMER_DELAY = 100; private boolean firstPane = true; @Override public void init() { createAndShowGui(); } private void createAndShowGui() { try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { TestClass test = new TestClass(); getContentPane().add(test); // a kludge to get focus on the GUI some time after it has been created new Timer(TIMER_DELAY, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { requestFocusInWindow(); ((Timer)e.getSource()).stop(); } }).start(); } }); } catch (InterruptedException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } @Override public void paint(Graphics g) { super.paint(g); // another kludge to get focus on the GUI after it is // first rendered if (firstPane) { requestFocusInWindow(); firstPane = false; } } } @SuppressWarnings("serial") class TestClass extends JPanel { public TestClass() { setLayout(new BorderLayout()); add(new JLabel("TestClass", SwingUtilities.CENTER)); int condition = WHEN_IN_FOCUSED_WINDOW; InputMap inputMap = getInputMap(condition); ActionMap actionMap = getActionMap(); for (KeyInfo keyInfo : KeyInfo.values()) { KeyStroke keyStroke = KeyStroke.getKeyStroke(keyInfo.getKeyCode(), 0); inputMap.put(keyStroke , keyInfo.toString()); actionMap.put(keyInfo.toString(), new AbstractAction() { @Override public void actionPerformed(ActionEvent evt) { System.out.println("key press: " + evt.getActionCommand()); } }); } } } enum KeyInfo { UP(KeyEvent.VK_W), DOWN(KeyEvent.VK_S), LEFT(KeyEvent.VK_A), RIGHT(KeyEvent.VK_D); private int keyCode; private KeyInfo(int keyCode) { this.keyCode = keyCode; } public int getKeyCode() { return keyCode; } } </code></pre> <p>The above code using an AncestorListener:</p> <pre><code>import java.awt.BorderLayout; import java.awt.Component; import java.awt.event.*; import java.lang.reflect.InvocationTargetException; import javax.swing.*; import javax.swing.event.AncestorEvent; import javax.swing.event.AncestorListener; @SuppressWarnings("serial") public class KeyBindingEg extends JApplet { protected static final int TIMER_DELAY = 100; private boolean firstPane = true; @Override public void init() { createAndShowGui(); } private void createAndShowGui() { try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { TestClass test = new TestClass(); JPanel contentPane = (JPanel) getContentPane(); contentPane.add(test); contentPane.addAncestorListener(new RequestFocusListener()); } }); } catch (InterruptedException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } class RequestFocusListener implements AncestorListener { public void ancestorRemoved(AncestorEvent arg0) {} public void ancestorMoved(AncestorEvent arg0) {} @Override public void ancestorAdded(AncestorEvent aEvt) { Component comp = (Component) aEvt.getSource(); comp.requestFocusInWindow(); } } @SuppressWarnings("serial") class TestClass extends JPanel { public TestClass() { setLayout(new BorderLayout()); add(new JLabel("TestClass", SwingUtilities.CENTER)); int condition = WHEN_IN_FOCUSED_WINDOW; InputMap inputMap = getInputMap(condition); ActionMap actionMap = getActionMap(); for (KeyInfo keyInfo : KeyInfo.values()) { KeyStroke keyStroke = KeyStroke.getKeyStroke(keyInfo.getKeyCode(), 0); inputMap.put(keyStroke, keyInfo.toString()); actionMap.put(keyInfo.toString(), new AbstractAction() { @Override public void actionPerformed(ActionEvent evt) { System.out.println("key press: " + evt.getActionCommand()); } }); } } } enum KeyInfo { UP(KeyEvent.VK_W), DOWN(KeyEvent.VK_S), LEFT(KeyEvent.VK_A), RIGHT( KeyEvent.VK_D); private int keyCode; private KeyInfo(int keyCode) { this.keyCode = keyCode; } public int getKeyCode() { return keyCode; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload