Get the VK int from an arbitrary char in java

前端 未结 8 894
孤街浪徒
孤街浪徒 2020-12-31 17:17

How do you get the VK code from a char that is a letter? It seems like you should be able to do something like javax.swing.KeyStroke.getKeyStroke(\'c\').getKeyCode()<

8条回答
  •  醉梦人生
    2020-12-31 17:52

    Maybe this ugly hack:

    Map keyTextToCode = new HashMap(256);
    Field[] fields = KeyEvent.class.getDeclaredFields();
    for (Field field : fields) {
        String name = field.getName();
        if (name.startsWith("VK_")) {
            keyTextToCode.put(name.substring("VK_".length()).toUpperCase(),
                              field.getInt(null));
        }
    }
    

    keyTextToCode would then contain the mapping from strings (e.g. "A" or "PAGE_UP") to vk codes.

提交回复
热议问题