主要是在做触屏的SWING软件时,遇到需要用户输入中文的情况,系统的osk的按钮太多了,而且没有办法定制化,所以就自己写了一个模拟的。以下是代码:
package test.swing;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class KeyboardTest extends JPanel implements ActionListener{
JTextField text ;
Robot robot;
int[] line1 = {192,49,50,51,52,53,54,55,56,57,48,45,61,8}; // 按键的第一排
int[] line2 = {81,87,69,82,84,89,85,73,79,80,91,93,92}; // q到\ 没有tab
int[] line3 = {KeyEvent.VK_CAPS_LOCK,65,83,68,70,71,72,74,75,76,59,222,10}; // 大写到'
int[] line4 = {16,90,88,67,86,66,78,77,44,46,47,38}; // shift到 向上
int[] line5 = {17,18,32,18,17,37,40,39}; // ctrl到 > 不包括 fn、window
Map<Integer, String> uncharMap = new HashMap<Integer, String>(); // 特殊字符
public KeyboardTest() {
// 获取当前大小写
boolean isUpper = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
System.out.println("当前是否大写:"+isUpper);
// 模拟输入
try {
robot = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//
this.setLayout(null);
int x = 20,y = 20 ,width = 60 , height = 40;
text = new JTextField();
text.setBounds(x, y, 800, height);
this.add(text);
text.grabFocus();
// 替换特殊字符
initUnChar();
// 添加从 33 - 126 的ascii
int[][] keyint = new int[5][];
keyint[0] = line1;
keyint[1] = line2;
keyint[2] = line3;
keyint[3] = line4;
keyint[4] = line5;
y = y + height + 20;
// load keys
int startx = 0,cellspace = 5;
loadKeys(line1,startx,cellspace,x, y, width, height);
y = y + height + 20;
// line2
int[] tmpInt = new int[]{line2[0]}; // tab
loadKeys(tmpInt,0,cellspace, x, y, width + width /2 , height);
startx = x + width + width / 2 - cellspace * 2 ;
tmpInt = new int[line2.length - 1];
System.arraycopy(line2, 1, tmpInt, 0, tmpInt.length);
loadKeys(tmpInt,startx ,cellspace, x, y, width, height);
// line3
y = y + height + 20;
tmpInt = new int[]{line3[0]};
loadKeys(tmpInt,0,cellspace, x, y, width * 2 , height);
startx = x + width * 2 - cellspace * 2 ;
tmpInt = new int[line3.length - 1];
System.arraycopy(line3, 1, tmpInt, 0, tmpInt.length);
loadKeys(tmpInt,startx,cellspace, x, y, width, height);
// line4
y = y + height + 20;
tmpInt = new int[]{line4[0]};
loadKeys(tmpInt,0,cellspace, x, y, width * 2 + width / 2 , height);
startx = x + width * 2 + width / 2 - cellspace * 2 ;
tmpInt = new int[line4.length - 1];
System.arraycopy(line4, 1, tmpInt, 0, tmpInt.length);
loadKeys(tmpInt,startx,cellspace, x, y, width, height);
/**
for(int i = 0;i < keyint.length;i++){
for(int j = 0;j < keyint[i].length; j++){
String showStr = uncharMap.get(keyint[i][j]); // 显示的字符
if(showStr == null){
showStr = String.valueOf((char) keyint[i][j]);
}
MyJButton jb = new MyJButton(showStr);
jb.setBounds((x + width )* (j + 1) , y, width, height);
jb.setFocusable(false); // 最关键的一句话
jb.setValue(keyint[i][j]); //
jb.addActionListener(this);
this.add(jb);
}
x = 20;
y = y + height + 20;
}
*/
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1024, 800);
//
KeyboardTest kb = new KeyboardTest();
frame.add(kb);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
MyJButton jb = (MyJButton)e.getSource();
int key = jb.getValue();
System.out.println(key);
robot.keyPress(key);
robot.keyRelease(key);
}
// 初始化特殊字符
public void initUnChar(){
uncharMap.put(192, "`");
uncharMap.put(8, "退格");
uncharMap.put(222, "'");
uncharMap.put(KeyEvent.VK_CAPS_LOCK, "大/小写");
uncharMap.put(10, "回车");
uncharMap.put(16, "SHIFT");
uncharMap.put(17, "CTRL");
uncharMap.put(17, "ALT");
uncharMap.put(38, "↑");
uncharMap.put(37, "←");
uncharMap.put(39, "→");
uncharMap.put(40, "↓");
}
class MyJButton extends JButton{
int value;
public MyJButton(String showStr) {
super(showStr);
}
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
/**
* @title loadKeys
* @date 2018年4月18日
* @param line
* @param x
* @param y
* @param width
* @param height
* @description 加载键盘
*/
public void loadKeys(int[] line,int startx,int cell,int x,int y ,int width,int height){
// line 1
for(int j = 0;j < line.length; j++){
String showStr = uncharMap.get(line[j]); // 显示的字符
if(showStr == null){
showStr = String.valueOf((char) line[j]);
}
MyJButton jb = new MyJButton(showStr);
jb.setBounds(startx + x + (cell + width) * j , y, width, height);
jb.setFocusable(false); // 最关键的一句话
jb.setValue(line[j]); //
jb.addActionListener(this);
this.add(jb);
}
}
}