用于比较+和其他SHIFT字符的VK代码

时间:2013-10-24 13:07:58

标签: java swing keylistener

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

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

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

enter image description here

2 个答案:

答案 0 :(得分: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());

答案 1 :(得分: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();
      }
    });
  }
}

此致