Get the VK int from an arbitrary char in java

前端 未结 8 989
孤街浪徒
孤街浪徒 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 18:00

    There are 2 ways I found:

    A. Use a workaround of JButton.setMnemonic, getMnemonic:

    javax.swing.JButton but = new javax.swing.JButton();
    but.setMnemonic(charVkValue);
    int intVkValue = but.getMnemonic());
    

    B. Download open jdk source and see its AbstractButton.setMnemonic(char) method. This code is licensed under GPL2, but these 4 lines do generally the same as the answer with "keyPress(48+(int)c)".

    http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Core/swing/javax/swing/AbstractButton.java.htm

提交回复
热议问题