Java仮想キーボード



Java Virtual Keyboard



タッチスクリーンキーボードの設計上の問題を含むソフトウェア開発の過程で、Java 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 /* Set the Java keyboard to essentially use the swing graphical interface to control the relevant knowledge, to build a keyboard interface in the JFrame, to achieve control input using the keyboard, * The main panel of the computer keyboard contains twenty-six letters and poetry numbers and other related characters. If you use the button time to implement one by one, it will cost * Too much time and experience, so in this example, the array is well utilized, that is, the keys in the keyboard are divided into five arrays by row, each array contains one * Line keyboard characters, using arrays to achieve keyboard settings, */ public class tEST1 extends JPanel implements ActionListener{ JTextField text Robot robot //line1 to line5 represent the first to fifth rows of the keyboard respectively. In Java, each key has its own keycode value. // For example: the keycode of 0~9 is 48~57, A~Z is 65~90 Int[] line1 = {192,49,50,51,52,53,54,55,56,57,48,45,61,8} // first row of buttons Int[] line2 = {81,87,69,82,84,89,85,73,79,80,91,93,92} //q to no tab Int[] line3 = {KeyEvent.VK_CAPS_LOCK,65,83,68,70,71,72,74,75,76,59,222,10} //Capital to ' Int[] line4 = {16,90,88,67,86,66,78,77,44,46,47,38} // shift to up Int[] line5 = {17,18,32,18,17,37,40,39} // ctrl to > does not include fn, window Map uncharMap = new HashMap() // special characters public tEST1() { / / Get the current case //boolean isUpper = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK) //System.out.println('Currently capitalized: '+isUpper) // analog input 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 / / Define a text box class for displaying the characters in the keyboard input text box text = new JTextField() text.setBounds(x, y, 800, height) this.add(text) text.grabFocus() // replace special characters initUnChar() // Add ascii from 33 - 126 Int[][] keyint = new int[5][]//Define a two-dimensional array of five rows 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 = 7 loadKeys(line1,startx,cellspace,x, y, width, height) y = y + height + 20 // line2 int[] tmpInt = new int[]{line2[0]} // tab / / In the second line, the first key is relatively large, so it is listed separately loadKeys(tmpInt,0,cellspace, x, y, width + width /2 , height) startx = x + width + width / 2 - cellspace * 2 tmpInt = new int[line2.length - 1]//The length of the new array is the length of the original array minus one System.arraycopy(line2, 1, tmpInt, 0, tmpInt.length)//Add the corresponding character to each key in the line 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 = 0i

結果は次のとおりです。
画像