13

所以我构建了一个 pi 零键盘模拟器,如此处所述:

https://www.rmedgar.com/blog/using-rpi-zero-as-keyboard-setup-and-device-definition

我让它输入从本地文本文件中读取的文本(一切都是用 java 开发的——出于某种原因:))。
我现在的问题是,我的 pi 零连接到的各种计算机上的配置键集差别很大(德语、英语、法语……)。根据计算机的不同,这会导致几个打字错误(例如,用 z 代替 y)。

所以我现在建立了一些“翻译表”,将字符映射到适合计算机的键码。这样的表如下所示:

public scancodes_en_us() {
    //We have (Character, (scancode, modifier))
    table.put("a",Pair.create("4","0"));
    table.put("b",Pair.create("5","0"));
    table.put("c",Pair.create("6","0"));
    table.put("d",Pair.create("7","0"));
    table.put("e",Pair.create("8","0"));
    table.put("f",Pair.create("9","0"));
    table.put("g",Pair.create("10","0"));
    table.put("h",Pair.create("11","0"));
    table.put("i",Pair.create("12","0"));
    table.put("j",Pair.create("13","0"));
    table.put("k",Pair.create("14","0"));
    table.put("l",Pair.create("15","0"));
    table.put("m",Pair.create("16","0"));
    table.put("n",Pair.create("17","0"));
    table.put("o",Pair.create("18","0"));
    table.put("p",Pair.create("19","0"));
    table.put("q",Pair.create("20","0"));
    table.put("r",Pair.create("21","0"));
    table.put("s",Pair.create("22","0"));
    table.put("t",Pair.create("23","0"));
    table.put("u",Pair.create("24","0"));
    table.put("v",Pair.create("25","0"));
    table.put("w",Pair.create("26","0"));
    table.put("x",Pair.create("27","0"));
    table.put("y",Pair.create("28","0"));
    table.put("z",Pair.create("29","0"));
    table.put("A",Pair.create("4","2"));
    table.put("B",Pair.create("5","2"));
    table.put("C",Pair.create("6","2"));
    table.put("D",Pair.create("7","2"));
    table.put("E",Pair.create("8","2"));
    table.put("F",Pair.create("9","2"));
    table.put("G",Pair.create("10","2"));
    table.put("H",Pair.create("11","2"));
    table.put("I",Pair.create("12","2"));
    table.put("J",Pair.create("13","2"));
    table.put("K",Pair.create("14","2"));
    table.put("L",Pair.create("15","2"));
    table.put("M",Pair.create("16","2"));
    table.put("N",Pair.create("17","2"));
    table.put("O",Pair.create("18","2"));
    table.put("P",Pair.create("19","2"));
    table.put("Q",Pair.create("20","2"));
    table.put("R",Pair.create("21","2"));
    table.put("S",Pair.create("22","2"));
    table.put("V",Pair.create("25","2"));
    table.put("W",Pair.create("26","2"));
    table.put("X",Pair.create("27","2"));
    table.put("Y",Pair.create("28","2"));
    table.put("Z",Pair.create("29","2"));
    table.put("1",Pair.create("30","0"));
    table.put("2",Pair.create("31","0"));
    table.put("5",Pair.create("34","0"));
    table.put("6",Pair.create("35","0"));
    table.put("7",Pair.create("36","0"));
    table.put("8",Pair.create("37","0"));
    table.put("9",Pair.create("38","0"));
    table.put("0",Pair.create("39","0"));
    table.put("!",Pair.create("30","2"));
    table.put("@",Pair.create("31","2"));
    table.put("#",Pair.create("32","2"));
    table.put("$",Pair.create("33","2"));
    table.put("%",Pair.create("34","2"));
    table.put("^",Pair.create("35","2"));
    table.put("&",Pair.create("36","2"));
    table.put("*",Pair.create("37","2"));
    table.put("(",Pair.create("38","2"));
    table.put(")",Pair.create("39","2"));
    table.put(" ",Pair.create("44","0"));
    table.put("-",Pair.create("45","0"));
    table.put("=",Pair.create("46","0"));
    table.put("[",Pair.create("47","0"));
    table.put("]",Pair.create("48","0"));
    table.put("\\",Pair.create("49","0"));
    table.put(";",Pair.create("51","0"));
    table.put("'",Pair.create("52","0"));
    table.put("`",Pair.create("53","0"));
    table.put(",",Pair.create("54","0"));
    table.put(".",Pair.create("55","0"));
    table.put("/",Pair.create("56","0"));
    table.put("_",Pair.create("45","2"));
    table.put("+",Pair.create("46","2"));
    table.put("{",Pair.create("47","2"));
    table.put("}",Pair.create("48","2"));
    table.put("|",Pair.create("49","2"));
    table.put(":",Pair.create("51","2"));
    table.put("\"",Pair.create("52","2"));
    table.put("~",Pair.create("53","2"));
    table.put("<",Pair.create("54","2"));
    table.put(">",Pair.create("55","2"));
    table.put("?",Pair.create("56","2"));

为许多不同的键盘布局拥有这样的表格是一件痛苦的事情。是否有一些更聪明的版本可以将字符映射到特定键盘布局的扫描码?

如果没有 - 是否有某种存档,我可以在其中找到这样一个字符到扫描码映射的许多不同键盘布局?

非常感谢

4

4 回答 4

11

看看本地化是如何工作的,它们都共享相同的方法:为每个本地化创建一个特殊版本作为属性文件,然后有一个抽象类来加载基于语言环境的属性。

您将开发一个像这样的加载器类:

public scancodes(Locale locale) {
    // load locale property file or download if missing
    // read the property and store to the table
    ResourceBundle scanCodes = ResourceBundle.getBundle("codes",locale);
}

codes_locale看起来像:

codes_de.properties

a=4,0
b=5,0

通过这样做,您将区域设置特定字符与您的逻辑代码分开,并且您不需要将所有键盘捆绑在您的应用程序中。您可以根据需要下载它们。

您可以在此处访问教程

于 2018-06-20T12:41:09.727 回答
3

如果我理解您要正确执行的操作,那么您根本不需要映射任何内容,只需使用预制格式(例如适用于我所知道的所有语言的 unicode),只需发送一个字符代码并翻译它到它匹配的字符。

示例文件阅读器 - 字符解释器:

JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.showOpenDialog(null);

File textFile = fc.getSelectedFile();

if(textFile.getName().endsWith(".txt")) {
    System.out.println(textFile.getAbsolutePath());
    FileInputStream input = new FileInputStream(textFile);
    BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UNICODE"));
    char[] buffer = new char[input.available() / 2 - 1];
    System.out.println("Bytes left: " + input.available());
    int read = reader.read(buffer);
    System.out.println("Read " + read + " characters");
    for(int i = 0; i < read; i++) {
        System.out.print("The letter is: " + buffer[i]);
        System.out.println(", The key code is: " + (int) buffer[i]);
    }
}

您可以稍后使用密钥代码来模拟计算机上的按键

于 2018-06-24T12:42:33.750 回答
1

对于扫描码映射,您可以访问以下站点:

  1. 键盘扫描码
  2. 扫码揭秘
于 2018-06-20T12:56:01.260 回答
0

我的解决方案是在运行时确定键码列表,它会为您节省大量的咖啡因和头痛

package test;

import java.util.HashMap;
import java.util.Map;

import javax.swing.KeyStroke;

public class Keycode {

    /**
     * List of chars, can be stored in file 
     * @return
     */
    public String getCharsets() {
        return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSVWXYZ12567890!@#$%^&*() -=[]\\;'`,./_+{}|:\\~<>?";
    }

    /**
     * Determines the keycode on runtime
     * @return
     */
    public Map<Character, Integer> getScancode() {
        Map<Character, Integer> table = new HashMap<>();
        String charsets = this.getCharsets();
        for( int index = 0 ; index < charsets.length() ; index++ ) {
            Character currentChar = charsets.charAt(index); 
            KeyStroke keyStroke   = KeyStroke.getKeyStroke(currentChar.charValue(), 0);
            // only for example i've used Map, but you should populate it by your table
            // table.put("a",Pair.create("4","0"));
            table.put(currentChar, keyStroke.getKeyCode());
        }
        return table;
    }

    public static void main(String[] args) {
        System.out.println(new Keycode().getScancode());
    }

}
于 2018-06-20T13:39:09.513 回答