我正在尝试用Java创建自己的虚拟键盘。我正在使用Robot的keyPress()事件来触发KeyEvent到文本字段。关键事件正在发挥作用,但它继续打印我按下的键。这是我的代码请检查我做错了什么?
创建键盘的代码
package virtualkeyboard;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JTextField;
public class Keyboard extends JDialog {
private static final long serialVersionUID = 1L;
private JTextField txtField;
public Keyboard(JTextField txt) {
this.txtField = txt;
setModalityType(ModalityType.APPLICATION_MODAL);
setLayout(new GridLayout(3, 3));
for (int i = 0; i <= 9; i++){
createButton(Integer.toString(i));
}
pack();
}
private void createButton(String label) {
JButton btn = new JButton(label);
btn.setFocusPainted(false);
btn.setPreferredSize(new Dimension(100, 100));
Font font = btn.getFont();
float size = font.getSize() + 15.0f;
btn.setFont(font.deriveFont(size));
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
txtField.requestFocus();
Robot robot;
try {
robot = new Robot();
if(label.equals("0"))
robot.keyPress(KeyEvent.VK_0);
else if(label.equals("1"))
robot.keyPress(KeyEvent.VK_1);
else if(label.equals("2"))
robot.keyPress(KeyEvent.VK_2);
else if(label.equals("3"))
robot.keyPress(KeyEvent.VK_3);
else if(label.equals("4"))
robot.keyPress(KeyEvent.VK_4);
else if(label.equals("5"))
robot.keyPress(KeyEvent.VK_5);
else if(label.equals("6"))
robot.keyPress(KeyEvent.VK_6);
else if(label.equals("7"))
robot.keyPress(KeyEvent.VK_7);
else if(label.equals("8"))
robot.keyPress(KeyEvent.VK_8);
else if(label.equals("9"))
robot.keyPress(KeyEvent.VK_9);
} catch (AWTException e1) {
e1.printStackTrace();
}
}
});
add(btn);
}
}
带文字字段的窗口
package virtualkeyboard;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JDialog;
import javax.swing.JTextField;
public class WindowOne extends JDialog {
private JTextField txtField = new JTextField();
private Keyboard keyboard = new Keyboard(txtField);
public WindowOne(){
setSize(new Dimension(250,30));
txtField.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(isEnabled()){
Point p = getLocationOnScreen();
p.y += 30;
keyboard.setLocation(p);
keyboard.setVisible(true);
}
}
});
add(txtField);
}
public static void main(String[] args) {
new WindowOne().setVisible(true);
}
}
编辑:
我在代码中进行了以下更改,并在按下按钮的同时使JTextfield可对焦。
private void createButton(String label,JTextField txt) {
JButton btn = new JButton(label);
btn.setFocusPainted(false);
btn.setPreferredSize(new Dimension(100, 100));
Font font = btn.getFont();
float size = font.getSize() + 15.0f;
btn.setFont(font.deriveFont(size));
btn.setFocusable(false);
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
txt.getParent().requestFocus();
txt.requestFocus();
robot.delay(100);
if(label.equals("0"))
robot.keyPress(KeyEvent.VK_0);
else if(label.equals("1"))
robot.keyPress(KeyEvent.VK_1);
else if(label.equals("2"))
robot.keyPress(KeyEvent.VK_2);
else if(label.equals("3"))
robot.keyPress(KeyEvent.VK_3);
else if(label.equals("4"))
robot.keyPress(KeyEvent.VK_4);
else if(label.equals("5"))
robot.keyPress(KeyEvent.VK_5);
else if(label.equals("6"))
robot.keyPress(KeyEvent.VK_6);
else if(label.equals("7"))
robot.keyPress(KeyEvent.VK_7);
else if(label.equals("8"))
robot.keyPress(KeyEvent.VK_8);
else if(label.equals("9"))
robot.keyPress(KeyEvent.VK_9);
requestFocusInWindow();
}
});
add(btn);
}
如果我按下按钮,它会继续打印文本字段中的数字。但事件只被触发一次。
请检查。
答案 0 :(得分:0)
我找到了解决问题的方法,
对于每个keyPress事件,我们需要触发相应的keyRelease事件,然后才会停止keyPress事件,否则它将被视为长按。
更改了此行
if(keyCode.equals("VK_0")){
robot.keyPress(KeyEvent.VK_0);
}
到
if(keyCode.equals("VK_0")){
robot.keyPress(KeyEvent.VK_0);
robot.keyRelease(KeyEvent.VK_0);
}