Get the VK int from an arbitrary char in java

前端 未结 8 963
孤街浪徒
孤街浪徒 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:59

    The answer of Adam Paynter (answered to the question Convert String to KeyEvents) should also work here. The simple but working idea is to have a big switch like the following:

    public void type(char character) {
        switch (character) {
        case 'a': doType(VK_A); break;
        case 'b': doType(VK_B); break;
        case 'c': doType(VK_C); break;
        case 'd': doType(VK_D); break;
        case 'e': doType(VK_E); break;
        // ...
        case 'A': doType(VK_SHIFT, VK_A); break;
        case 'B': doType(VK_SHIFT, VK_B); break;
        case 'C': doType(VK_SHIFT, VK_C); break;
        // ...
        }
    }
    

    See the original answer for the whole listing (including surrounding utility class).

提交回复
热议问题