想对JTextField做键盘监听
但是是两个键同时按得时候生效
下面是我的代码, 对两个键的测试无效,求解:
public void keyPressed(KeyEvent e) {
boolean ctrl = false;
boolean c = false;
boolean v = false;
boolean enter = false;
int i = e.getKeyCode();
switch(i) {
case KeyEvent.VK_CONTROL : ctrl=true;break;
case KeyEvent.VK_C : c= true; break;
case KeyEvent.VK_V : v= true; break;
case KeyEvent.VK_ENTER : enter= true; break;
}
if(ctrl) {
System.out.println("------------ctrl");
}
if(ctrl && c) {
System.out.println("------------copy event");
}
if(c) {
System.out.println("------------c");
}
但是是两个键同时按得时候生效
下面是我的代码, 对两个键的测试无效,求解:
public void keyPressed(KeyEvent e) {
boolean ctrl = false;
boolean c = false;
boolean v = false;
boolean enter = false;
int i = e.getKeyCode();
switch(i) {
case KeyEvent.VK_CONTROL : ctrl=true;break;
case KeyEvent.VK_C : c= true; break;
case KeyEvent.VK_V : v= true; break;
case KeyEvent.VK_ENTER : enter= true; break;
}
if(ctrl) {
System.out.println("------------ctrl");
}
if(ctrl && c) {
System.out.println("------------copy event");
}
if(c) {
System.out.println("------------c");
}
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;@SuppressWarnings("serial")
public class TextAreaInputMapTest extends JTextArea {
private static final String AUTO_COMPLETE_ACTION = "AutoComplete"; public TextAreaInputMapTest() {
this.setFont(new Font("monaco", Font.PLAIN, 14));
InputMap im = this.getInputMap();
ActionMap am = this.getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SEMICOLON, InputEvent.META_DOWN_MASK),
AUTO_COMPLETE_ACTION);
am.put(AUTO_COMPLETE_ACTION, new AutoCompleteAction());
am.put(AUTO_COMPLETE_ACTION, new PrintAction()); // 前一个绑定的Action被删除 KeyStroke[] kss = im.allKeys();
for (KeyStroke ks : kss) {
this.append(String.format("%30s : %s\n", ks, im.get(ks)));
}
} public class AutoCompleteAction extends AbstractAction {
public void actionPerformed(ActionEvent e) {
System.out.println("Auto complete");
}
} public class PrintAction extends AbstractAction {
public void actionPerformed(ActionEvent e) {
System.out.println("Print........");
}
} private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("TextDemo"); // Add contents to the window.
TextAreaInputMapTest textArea = new TextAreaInputMapTest();
JScrollPane scroller = new JScrollPane(textArea);
scroller.setBorder(null);
frame.getContentPane().add(scroller); // Display the window.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int width = 600;
int height = 700;
int x, y;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
x = (screenSize.width - width) / 2;
y = (screenSize.height - height) / 2 - 600;
x = x > 0 ? x : 0;
y = y > 0 ? y : 0;
frame.setLocation(x, y);
frame.setSize(width, height);
frame.setVisible(true);
} public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TextAreaInputMapTest.createAndShowGUI();
}
});
}
}或者使用前不久我写的swing应用程序级的快捷键管理工具类也方便处理这个问题。