Performing a specific task after pressing "ctrl" + "alt" + "backspace"

How to do it when I want to do something. Assume that the focus on the JTextField and message box appears when the user press ctrl + alt + backspace at the same time.

+4
source share
2 answers

Key binding example:

import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import javax.swing.*; public class JTextFieldMagic { public static final String CTRL_ALT_BACK_SPACE = "ctrlAltBackspace"; public static void main(String[] args) { JTextField field = new JTextField(10); int condition = JComponent.WHEN_FOCUSED; InputMap inputmap = field.getInputMap(condition); ActionMap actionMap = field.getActionMap(); KeyStroke ctrlAltBackSpaceKeyStroke = KeyStroke.getKeyStroke( KeyEvent.VK_BACK_SPACE, KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK); inputmap.put(ctrlAltBackSpaceKeyStroke, CTRL_ALT_BACK_SPACE); actionMap.put(CTRL_ALT_BACK_SPACE, new CtrlAltBackspaceAction()); JOptionPane.showMessageDialog(null, field); } } class CtrlAltBackspaceAction extends AbstractAction { @Override public void actionPerformed(ActionEvent e) { System.out.println(JTextFieldMagic.CTRL_ALT_BACK_SPACE); } } 
+4
source

Alternatively, if you are after a "global global application listener", you can use either KeyboardFocusManager.addKeyEventDispatcher or Toolkit.addAWTEventListener .

KeyboardFocusManager.addKeyEventDispatcher

 public class GloablKeyListener { public static void main(String[] args) { new GloablKeyListener(); } public GloablKeyListener() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Testing"); frame.setLayout(new BorderLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new TestPane()); frame.setSize(200, 200); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } protected class TestPane extends JPanel { private JLabel label; public TestPane() { setLayout(new BorderLayout()); label = new JLabel("Nothing happening here"); label.setHorizontalAlignment(JLabel.CENTER); add(label); KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() { @Override public boolean dispatchKeyEvent(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE && e.isControlDown() && e.isAltDown()) { label.setText("Hit me"); } else { label.setText("Nothing to see here..."); } return false; } }); } } } 

Toolkit.addAWTEventListener

 public class GloablKeyListener { public static void main(String[] args) { new GloablKeyListener(); } public GloablKeyListener() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Testing"); frame.setLayout(new BorderLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new TestPane()); frame.setSize(200, 200); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } protected class TestPane extends JPanel { private JLabel label; public TestPane() { setLayout(new BorderLayout()); label = new JLabel("Nothing happening here"); label.setHorizontalAlignment(JLabel.CENTER); add(label); Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() { @Override public void eventDispatched(AWTEvent event) { if (event instanceof KeyEvent) { KeyEvent ke = (KeyEvent) event; if (ke.getID() == KeyEvent.KEY_TYPED) { if (ke.getKeyCode() == KeyEvent.VK_BACK_SPACE && ke.isControlDown() && ke.isAltDown()) { label.setText("Hit me"); } else { label.setText("Nothing to see here..."); } } } } }, AWTEvent.KEY_EVENT_MASK); } } } 

Personally, I prefer KeyboardFocusManager.addKeyEventDispatcher . It is simpler and easier to use.

You can set up your own singlton manager, where you can assign KeyStroke to Action s, as well as key bindings.

+2
source

Source: https://habr.com/ru/post/1438159/


All Articles