Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.ActionEvent;
- import java.awt.event.KeyEvent;
- import javax.swing.*;
- public class KeyBindingTest extends JPanel {
- public KeyBindingTest() {
- initializeKeyBinds();
- }
- void initializeKeyBinds() {
- Action timerAction = new DoAction();
- System.out.println("Here at initializeKeyBinds");
- InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
- ActionMap am = getActionMap();
- im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "SPACE");
- am.put("SPACE", timerAction);
- }
- static class DoAction extends AbstractAction {
- @Override
- public void actionPerformed(ActionEvent tf) {
- System.out.println("The Enter key has been pressed.");
- // scrambleButton.doClick();
- }
- }
- private static void createAndShowGui() {
- KeyBindingTest mainPanel = new KeyBindingTest();
- JFrame frame = new JFrame("KeyBindingTest");
- frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- frame.getContentPane().add(mainPanel);
- frame.pack();
- frame.setLocationByPlatform(true);
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(() -> createAndShowGui());
- }
- }
Add Comment
Please, Sign In to add comment