Note that there are some explanatory texts on larger screens.

plurals
  1. POJFrame - reading two keystrokes (ESC and F2) and executing different actions
    text
    copied!<p>I'm looking to recognise two keystrokes from the ESC and F2 key. Below is how I recognise when the escape key is pressed:</p> <pre><code>// Enable escape to close the window by first returning the rootPane object for this frame, then the InputMap that is usedduring condition i.e. JComponent.WHEN_IN_FOCUSED_WINDOW // and then use function put to add a binding for keyStroke to actionMapKey frame.getRootPane().getInputMap(JComponent. WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Cancel"); // Kill the AuotIt script when closing the window by first returning the rootPane object for this frame then the ActionMap used to determine what Action // to fire for particular KeyStroke binding. The returned ActionMap, unless otherwise set, will have the ActionMap from the UI set as the parent. frame.getRootPane().getActionMap().put("Cancel", new AbstractAction(){ public void actionPerformed(ActionEvent x) { // Undertake some action here } }); </code></pre> <p>I attempted the exact same thing but changing the keyevent to F2 but nothing happens when I press the F2 button. Below is how I attempted recognising the F2:</p> <pre><code>frame.getRootPane().getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), "pressed"); // frame.getRootPane().getActionMap().put("pressed", new AbstractAction(){ public void actionPerformed(ActionEvent x) { // Change the hasusersetcomport flag in hasusersetcomport.txt to 0 notifyScriptThatUserNeedsCOMPorts("0"); // Notify Script That User Pressed Allow Pairing (which is the default setting for wasallowpairingpressed.txt) notifyScriptThatUserPressedAllowPairing("1"); // Write menu Item Event Control ID RFC95.writeMenuItemEventControlID("8"); // Write empty string into config file writeUserSelectedCOMportIntoConfigFile(""); // Notify user that current pairing has been deleted notifMssgLabel.setText("&lt;html&gt;&lt;p&gt;CURRENT PAIRING SETTING DELETED. WINDOW CLOSING&lt;/p&gt;&lt;/html&gt;"); notifMssgLabel.setForeground(officialSuccessFontColour); notifMssgLabel.setFont(officialFontBold); // Change the hasusersetcomport flag in hasusersetcomport.txt to 0 notifyScriptThatUserNeedsCOMPorts("0"); panelTimer.start(); } }); </code></pre> <p>Any ideas as to why I'm encountering the problem.</p> <p>Thanks.</p>
 

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