Java软键盘

主要是在做触屏的SWING软件时,遇到需要用户输入中文的情况,系统的osk的按钮太多了,而且没有办法定制化,所以就自己写了一个模拟的。以下是代码:



    
    
  1. package test.swing;
  2. import java.awt.AWTException;
  3. import java.awt.Robot;
  4. import java.awt.Toolkit;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.KeyEvent;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13. import javax.swing.JTextField;
  14. public class KeyboardTest extends JPanel implements ActionListener{
  15. JTextField text ;
  16. Robot robot;
  17. int[] line1 = { 192, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 45, 61, 8}; // 按键的第一排
  18. int[] line2 = { 81, 87, 69, 82, 84, 89, 85, 73, 79, 80, 91, 93, 92}; // q到\ 没有tab
  19. int[] line3 = {KeyEvent.VK_CAPS_LOCK, 65, 83, 68, 70, 71, 72, 74, 75, 76, 59, 222, 10}; // 大写到'
  20. int[] line4 = { 16, 90, 88, 67, 86, 66, 78, 77, 44, 46, 47, 38}; // shift到 向上
  21. int[] line5 = { 17, 18, 32, 18, 17, 37, 40, 39}; // ctrl到 > 不包括 fn、window
  22. Map<Integer, String> uncharMap = new HashMap<Integer, String>(); // 特殊字符
  23. public KeyboardTest() {
  24. // 获取当前大小写
  25. boolean isUpper = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
  26. System.out.println( "当前是否大写:"+isUpper);
  27. // 模拟输入
  28. try {
  29. robot = new Robot();
  30. } catch (AWTException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. //
  35. this.setLayout( null);
  36. int x = 20,y = 20 ,width = 60 , height = 40;
  37. text = new JTextField();
  38. text.setBounds(x, y, 800, height);
  39. this.add(text);
  40. text.grabFocus();
  41. // 替换特殊字符
  42. initUnChar();
  43. // 添加从 33 - 126 的ascii
  44. int[][] keyint = new int[ 5][];
  45. keyint[ 0] = line1;
  46. keyint[ 1] = line2;
  47. keyint[ 2] = line3;
  48. keyint[ 3] = line4;
  49. keyint[ 4] = line5;
  50. y = y + height + 20;
  51. // load keys
  52. int startx = 0,cellspace = 5;
  53. loadKeys(line1,startx,cellspace,x, y, width, height);
  54. y = y + height + 20;
  55. // line2
  56. int[] tmpInt = new int[]{line2[ 0]}; // tab
  57. loadKeys(tmpInt, 0,cellspace, x, y, width + width / 2 , height);
  58. startx = x + width + width / 2 - cellspace * 2 ;
  59. tmpInt = new int[line2.length - 1];
  60. System.arraycopy(line2, 1, tmpInt, 0, tmpInt.length);
  61. loadKeys(tmpInt,startx ,cellspace, x, y, width, height);
  62. // line3
  63. y = y + height + 20;
  64. tmpInt = new int[]{line3[ 0]};
  65. loadKeys(tmpInt, 0,cellspace, x, y, width * 2 , height);
  66. startx = x + width * 2 - cellspace * 2 ;
  67. tmpInt = new int[line3.length - 1];
  68. System.arraycopy(line3, 1, tmpInt, 0, tmpInt.length);
  69. loadKeys(tmpInt,startx,cellspace, x, y, width, height);
  70. // line4
  71. y = y + height + 20;
  72. tmpInt = new int[]{line4[ 0]};
  73. loadKeys(tmpInt, 0,cellspace, x, y, width * 2 + width / 2 , height);
  74. startx = x + width * 2 + width / 2 - cellspace * 2 ;
  75. tmpInt = new int[line4.length - 1];
  76. System.arraycopy(line4, 1, tmpInt, 0, tmpInt.length);
  77. loadKeys(tmpInt,startx,cellspace, x, y, width, height);
  78. /**
  79. for(int i = 0;i < keyint.length;i++){
  80. for(int j = 0;j < keyint[i].length; j++){
  81. String showStr = uncharMap.get(keyint[i][j]); // 显示的字符
  82. if(showStr == null){
  83. showStr = String.valueOf((char) keyint[i][j]);
  84. }
  85. MyJButton jb = new MyJButton(showStr);
  86. jb.setBounds((x + width )* (j + 1) , y, width, height);
  87. jb.setFocusable(false); // 最关键的一句话
  88. jb.setValue(keyint[i][j]); //
  89. jb.addActionListener(this);
  90. this.add(jb);
  91. }
  92. x = 20;
  93. y = y + height + 20;
  94. }
  95. */
  96. }
  97. public static void main(String[] args) {
  98. JFrame frame = new JFrame();
  99. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  100. frame.setSize( 1024, 800);
  101. //
  102. KeyboardTest kb = new KeyboardTest();
  103. frame.add(kb);
  104. frame.setVisible( true);
  105. }
  106. @Override
  107. public void actionPerformed(ActionEvent e) {
  108. MyJButton jb = (MyJButton)e.getSource();
  109. int key = jb.getValue();
  110. System.out.println(key);
  111. robot.keyPress(key);
  112. robot.keyRelease(key);
  113. }
  114. // 初始化特殊字符
  115. public void initUnChar(){
  116. uncharMap.put( 192, "`");
  117. uncharMap.put( 8, "退格");
  118. uncharMap.put( 222, "'");
  119. uncharMap.put(KeyEvent.VK_CAPS_LOCK, "大/小写");
  120. uncharMap.put( 10, "回车");
  121. uncharMap.put( 16, "SHIFT");
  122. uncharMap.put( 17, "CTRL");
  123. uncharMap.put( 17, "ALT");
  124. uncharMap.put( 38, "↑");
  125. uncharMap.put( 37, "←");
  126. uncharMap.put( 39, "→");
  127. uncharMap.put( 40, "↓");
  128. }
  129. class MyJButton extends JButton{
  130. int value;
  131. public MyJButton(String showStr) {
  132. super(showStr);
  133. }
  134. public void setValue(int value) {
  135. this.value = value;
  136. }
  137. public int getValue() {
  138. return value;
  139. }
  140. }
  141. /**
  142. * @title loadKeys
  143. * @date 2018年4月18日
  144. * @param line
  145. * @param x
  146. * @param y
  147. * @param width
  148. * @param height
  149. * @description 加载键盘
  150. */
  151. public void loadKeys(int[] line,int startx,int cell,int x,int y ,int width,int height){
  152. // line 1
  153. for( int j = 0;j < line.length; j++){
  154. String showStr = uncharMap.get(line[j]); // 显示的字符
  155. if(showStr == null){
  156. showStr = String.valueOf(( char) line[j]);
  157. }
  158. MyJButton jb = new MyJButton(showStr);
  159. jb.setBounds(startx + x + (cell + width) * j , y, width, height);
  160. jb.setFocusable( false); // 最关键的一句话
  161. jb.setValue(line[j]); //
  162. jb.addActionListener( this);
  163. this.add(jb);
  164. }
  165. }
  166. }

运行结果如下:
在这里插入图片描述

主要是在做触屏的SWING软件时,遇到需要用户输入中文的情况,系统的osk的按钮太多了,而且没有办法定制化,所以就自己写了一个模拟的。以下是代码:



  
  
  1. package test.swing;
  2. import java.awt.AWTException;
  3. import java.awt.Robot;
  4. import java.awt.Toolkit;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.KeyEvent;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13. import javax.swing.JTextField;
  14. public class KeyboardTest extends JPanel implements ActionListener{
  15. JTextField text ;
  16. Robot robot;
  17. int[] line1 = { 192, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 45, 61, 8}; // 按键的第一排
  18. int[] line2 = { 81, 87, 69, 82, 84, 89, 85, 73, 79, 80, 91, 93, 92}; // q到\ 没有tab
  19. int[] line3 = {KeyEvent.VK_CAPS_LOCK, 65, 83, 68, 70, 71, 72, 74, 75, 76, 59, 222, 10}; // 大写到'
  20. int[] line4 = { 16, 90, 88, 67, 86, 66, 78, 77, 44, 46, 47, 38}; // shift到 向上
  21. int[] line5 = { 17, 18, 32, 18, 17, 37, 40, 39}; // ctrl到 > 不包括 fn、window
  22. Map<Integer, String> uncharMap = new HashMap<Integer, String>(); // 特殊字符
  23. public KeyboardTest() {
  24. // 获取当前大小写
  25. boolean isUpper = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
  26. System.out.println( "当前是否大写:"+isUpper);
  27. // 模拟输入
  28. try {
  29. robot = new Robot();
  30. } catch (AWTException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. //
  35. this.setLayout( null);
  36. int x = 20,y = 20 ,width = 60 , height = 40;
  37. text = new JTextField();
  38. text.setBounds(x, y, 800, height);
  39. this.add(text);
  40. text.grabFocus();
  41. // 替换特殊字符
  42. initUnChar();
  43. // 添加从 33 - 126 的ascii
  44. int[][] keyint = new int[ 5][];
  45. keyint[ 0] = line1;
  46. keyint[ 1] = line2;
  47. keyint[ 2] = line3;
  48. keyint[ 3] = line4;
  49. keyint[ 4] = line5;
  50. y = y + height + 20;
  51. // load keys
  52. int startx = 0,cellspace = 5;
  53. loadKeys(line1,startx,cellspace,x, y, width, height);
  54. y = y + height + 20;
  55. // line2
  56. int[] tmpInt = new int[]{line2[ 0]}; // tab
  57. loadKeys(tmpInt, 0,cellspace, x, y, width + width / 2 , height);
  58. startx = x + width + width / 2 - cellspace * 2 ;
  59. tmpInt = new int[line2.length - 1];
  60. System.arraycopy(line2, 1, tmpInt, 0, tmpInt.length);
  61. loadKeys(tmpInt,startx ,cellspace, x, y, width, height);
  62. // line3
  63. y = y + height + 20;
  64. tmpInt = new int[]{line3[ 0]};
  65. loadKeys(tmpInt, 0,cellspace, x, y, width * 2 , height);
  66. startx = x + width * 2 - cellspace * 2 ;
  67. tmpInt = new int[line3.length - 1];
  68. System.arraycopy(line3, 1, tmpInt, 0, tmpInt.length);
  69. loadKeys(tmpInt,startx,cellspace, x, y, width, height);
  70. // line4
  71. y = y + height + 20;
  72. tmpInt = new int[]{line4[ 0]};
  73. loadKeys(tmpInt, 0,cellspace, x, y, width * 2 + width / 2 , height);
  74. startx = x + width * 2 + width / 2 - cellspace * 2 ;
  75. tmpInt = new int[line4.length - 1];
  76. System.arraycopy(line4, 1, tmpInt, 0, tmpInt.length);
  77. loadKeys(tmpInt,startx,cellspace, x, y, width, height);
  78. /**
  79. for(int i = 0;i < keyint.length;i++){
  80. for(int j = 0;j < keyint[i].length; j++){
  81. String showStr = uncharMap.get(keyint[i][j]); // 显示的字符
  82. if(showStr == null){
  83. showStr = String.valueOf((char) keyint[i][j]);
  84. }
  85. MyJButton jb = new MyJButton(showStr);
  86. jb.setBounds((x + width )* (j + 1) , y, width, height);
  87. jb.setFocusable(false); // 最关键的一句话
  88. jb.setValue(keyint[i][j]); //
  89. jb.addActionListener(this);
  90. this.add(jb);
  91. }
  92. x = 20;
  93. y = y + height + 20;
  94. }
  95. */
  96. }
  97. public static void main(String[] args) {
  98. JFrame frame = new JFrame();
  99. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  100. frame.setSize( 1024, 800);
  101. //
  102. KeyboardTest kb = new KeyboardTest();
  103. frame.add(kb);
  104. frame.setVisible( true);
  105. }
  106. @Override
  107. public void actionPerformed(ActionEvent e) {
  108. MyJButton jb = (MyJButton)e.getSource();
  109. int key = jb.getValue();
  110. System.out.println(key);
  111. robot.keyPress(key);
  112. robot.keyRelease(key);
  113. }
  114. // 初始化特殊字符
  115. public void initUnChar(){
  116. uncharMap.put( 192, "`");
  117. uncharMap.put( 8, "退格");
  118. uncharMap.put( 222, "'");
  119. uncharMap.put(KeyEvent.VK_CAPS_LOCK, "大/小写");
  120. uncharMap.put( 10, "回车");
  121. uncharMap.put( 16, "SHIFT");
  122. uncharMap.put( 17, "CTRL");
  123. uncharMap.put( 17, "ALT");
  124. uncharMap.put( 38, "↑");
  125. uncharMap.put( 37, "←");
  126. uncharMap.put( 39, "→");
  127. uncharMap.put( 40, "↓");
  128. }
  129. class MyJButton extends JButton{
  130. int value;
  131. public MyJButton(String showStr) {
  132. super(showStr);
  133. }
  134. public void setValue(int value) {
  135. this.value = value;
  136. }
  137. public int getValue() {
  138. return value;
  139. }
  140. }
  141. /**
  142. * @title loadKeys
  143. * @date 2018年4月18日
  144. * @param line
  145. * @param x
  146. * @param y
  147. * @param width
  148. * @param height
  149. * @description 加载键盘
  150. */
  151. public void loadKeys(int[] line,int startx,int cell,int x,int y ,int width,int height){
  152. // line 1
  153. for( int j = 0;j < line.length; j++){
  154. String showStr = uncharMap.get(line[j]); // 显示的字符
  155. if(showStr == null){
  156. showStr = String.valueOf(( char) line[j]);
  157. }
  158. MyJButton jb = new MyJButton(showStr);
  159. jb.setBounds(startx + x + (cell + width) * j , y, width, height);
  160. jb.setFocusable( false); // 最关键的一句话
  161. jb.setValue(line[j]); //
  162. jb.addActionListener( this);
  163. this.add(jb);
  164. }
  165. }
  166. }

猜你喜欢

转载自blog.csdn.net/weixin_41792162/article/details/89216086