0

我正在创建一个带有标签的计算器,以显示用户从键盘输入的内容。

当用户按下键盘上的“+”按钮时,问题就出现了,因为键盘上的“+”是(Shift + '=')。KeyEventgetKeyChar()getKeyCode()方法解释为两个单独的 ASCII(一个是 16,另一个是 61)

下面的屏幕截图显示了getKeyCode()按下 Shift + '=' 以生成加号时的输出:

在此处输入图像描述

4

2 回答 2

0

这与您的疑问非常相似,希望这对您有所帮助,

class DispatcherListener implements KeyEventDispatcher{
    private int level=0;

    public boolean dispatchKeyEvent(KeyEvent e) {
        if(e.getID() == KeyEvent.KEY_RELEASED){
            if(e.isControlDown() && e.isShiftDown()){
                if(this.level==0 && e.getKeyCode()==KeyEvent.VK_S){
                    level++;
                }else if(this.level==1 && e.getKeyCode()==KeyEvent.VK_H){
                    level++;
                }else if(this.level==2 && e.getKeyCode()==KeyEvent.VK_O){
                    level++;
                }else if(this.level==3 && e.getKeyCode()==KeyEvent.VK_W){
                    level=0;
                    this.showHiddenWindow((JFrame)SwingUtilities.getRoot(e.getComponent()));
                }else{
                    level=0;
                }

                //System.out.println( "level: " +  level );
            }
        }
        return false;
    }

并将其用作:

KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.addKeyEventDispatcher( new DispatcherListener());
于 2013-10-24T13:13:37.953 回答
0

为什么不通过测试 KEY_PAD 事件来捕捉输入的字符,所以“+”符号可能来自数字 pa,而不仅仅是来自“Shift”和“+”组合?

例如,类似:

public boolean dispatchKeyEvent(KeyEvent event) {
    int eventID = event.getID();
    if (eventID == KeyEvent.KEY_TYPED) {
      char key = event.getKeyChar();
      if (key == '+') {
         /// Continue here...
         System.out.println("++++");
      }
    }
}

-- 编辑:这是一个完整的例子(从http://www.java2s.com/Code/Java/Event/KeyEventDemo.htm简化)

如果您尝试键入“Shift”+“+”,显示的字符应该是“+”

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class KeyEventTest extends JPanel implements KeyListener, ActionListener {
  JTextArea displayArea;

  JTextField typingArea;

  static final String newline = "\n";

  public KeyEventTest() {
    super(new BorderLayout());

    JButton button = new JButton("Clear");
    button.addActionListener(this);

    typingArea = new JTextField();
    typingArea.addKeyListener(this);

    displayArea = new JTextArea();
    displayArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(displayArea);
    scrollPane.setPreferredSize(new Dimension(375, 125));

    add(typingArea, BorderLayout.PAGE_START);
    add(scrollPane, BorderLayout.CENTER);
    add(button, BorderLayout.PAGE_END);
  }

  /** Handle the key typed event from the text field. */
  public void keyTyped(KeyEvent e) {
    displayInfo(e, "KEY TYPED: ");
  }

  /** Handle the key pressed event from the text field. */
  public void keyPressed(KeyEvent e) {
  }

  /** Handle the key released event from the text field. */
  public void keyReleased(KeyEvent e) {
  }

  /** Handle the button click. */
  public void actionPerformed(ActionEvent e) {
    displayArea.setText("");
    typingArea.setText("");
    typingArea.requestFocusInWindow();
  }

  protected void displayInfo(KeyEvent e, String s) {
    String typedKey = null;
    int id = e.getID();
    if (id == KeyEvent.KEY_TYPED) {
      char c = e.getKeyChar();
      typedKey = "key character = '" + c + "'";
    } 
    displayArea.append(s + newline + "    " + typedKey + newline);
    displayArea.setCaretPosition(displayArea.getDocument().getLength());
  }

  /**
   * Create the GUI and show it. For thread safety, this method should be
   * invoked from the event-dispatching thread.
   */
  private static void createAndShowGUI() {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("KeyEventDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JComponent newContentPane = new KeyEventTest();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);
    frame.pack();
    frame.setVisible(true);
  }

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

问候

于 2013-10-24T13:23:51.993 回答