900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > SWING中模拟键盘输入例子

SWING中模拟键盘输入例子

时间:2023-11-10 03:48:24

相关推荐

SWING中模拟键盘输入例子

主要是在做触屏的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到\ 没有tabint[] 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、windowMap<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 blocke.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 的asciiint[][] keyint = new int[5][];keyint[0] = line1;keyint[1] = line2;keyint[2] = line3;keyint[3] = line4;keyint[4] = line5;y = y + height + 20;// load keysint startx = 0,cellspace = 5;loadKeys(line1,startx,cellspace,x, y, width, height);y = y + height + 20;// line2int[] tmpInt = new int[]{line2[0]}; // tabloadKeys(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);// line3y = 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);// line4y = 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);}@Overridepublic 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 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 1for(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);}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。