简体   繁体   English

Java键绑定到相同的动作

[英]Java Key Binding to same Action

I'm trying to reduce the amount of code I need to handle keyboard presses/releases by using a common Action, while still using the InputMap/ActionMap paradigm (Need separate Press/Released information). 我试图通过使用通用Action来减少处理键盘按下/释放的代码量,同时仍使用InputMap / ActionMap范例(需要单独的按下/发布信息)。

The problem I'm trying to solve is how to get the keyPressed/keyReleased information from the ActionEvent. 我要解决的问题是如何从ActionEvent中获取keyPressed / keyReleased信息。

    KeyStroke rKeyStrokeReleased = KeyStroke.getKeyStroke(KeyEvent.VK_R,
            0, true);
    KeyStroke rKeyStrokePressed = KeyStroke.getKeyStroke(KeyEvent.VK_R,
            0, false);

    Action rAction = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            // How do I get the pressed/release information here?
            System.out.println(e);
        }
    };

    getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
            rKeyStrokeReleased, "R_RELEASED");
    getRootPane().getActionMap().put("R_RELEASED", rAction );

    getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
            rKeyStrokePressed, "R_PRESSED");
    getRootPane().getActionMap().put("R_PRESSED", rAction);

how to get the keyPressed/keyReleased information from the ActionEvent. 如何从ActionEvent中获取keyPressed / keyReleased信息。

You can't get the information from the ActionEvent . 您无法从ActionEvent获取信息。

You can get the information from the EventQueue : 您可以从EventQueue获取信息:

EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
AWTEvent event = queue.getCurrentEvent();
System.out.println(event.getID());

One way -- use the same class that extends AbstractAction, but use separate instances for pressed and released, and pass a boolean into the constructor, telling it which state it's in. For example: 一种方法-使用扩展AbstractAction的同一类,但使用单独的实例进行按下和释放,然后将布尔值传递给构造函数,以告知其处于哪个状态。例如:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class KeyBindingEg extends JPanel {
   private static final String UP_KEY_PRESSED = "up key pressed";
   private static final String UP_KEY_RELEASED = "up key released";
   private static final int UP_TIMER_DELAY = 200;
   private static final Color FLASH_COLOR = Color.red;

   private Timer upTimer;
   private JLabel label = new JLabel();

   public KeyBindingEg() {
      label.setFont(label.getFont().deriveFont(Font.BOLD, 32));
      label.setOpaque(true);
      add(label);

      setPreferredSize(new Dimension(400, 300));

      int condition = WHEN_IN_FOCUSED_WINDOW;
      InputMap inputMap = getInputMap(condition);
      ActionMap actionMap = getActionMap();
      KeyStroke upKeyPressed = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false);
      KeyStroke upKeyReleased = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true);

      inputMap.put(upKeyPressed, UP_KEY_PRESSED);
      inputMap.put(upKeyReleased, UP_KEY_RELEASED);

      actionMap.put(UP_KEY_PRESSED, new UpAction(false));
      actionMap.put(UP_KEY_RELEASED, new UpAction(true));

   }

   private class UpAction extends AbstractAction {
      private boolean onKeyRelease;

      public UpAction(boolean onKeyRelease) {
         this.onKeyRelease = onKeyRelease;
      }

      @Override
      public void actionPerformed(ActionEvent evt) {
         if (!onKeyRelease) {
            if (upTimer != null && upTimer.isRunning()) {
               return;
            }
            System.out.println("key pressed");
            label.setText(UP_KEY_PRESSED);

            upTimer = new Timer(UP_TIMER_DELAY, new ActionListener() {

               @Override
               public void actionPerformed(ActionEvent e) {
                  Color c = label.getBackground();
                  if (FLASH_COLOR.equals(c)) {
                     label.setBackground(null);
                     label.setForeground(Color.black);
                  } else {
                     label.setBackground(FLASH_COLOR);
                     label.setForeground(Color.white);
                  }
               }
            });
            upTimer.start();
         } else {
            System.out.println("Key released");
            if (upTimer != null && upTimer.isRunning()) {
               upTimer.stop();
               upTimer = null;
            }
            label.setText("");
         }
      }

   }

   private static void createAndShowGui() {
      KeyBindingEg mainPanel = new KeyBindingEg();

      JFrame frame = new JFrame("KeyBindingEg");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM