Get the VK int from an arbitrary char in java

我是研究僧i 提交于 2019-11-30 09:15:42

Maybe this ugly hack:

Map<String, Integer> keyTextToCode = new HashMap<String, Integer>(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.

pR0Ps
AWTKeyStroke.getAWTKeyStroke('c').getKeyCode();

Slight clarification of Pace's answer. It should be single quotes (representing a character), not double quotes (representing a string).

Using double quotes will throw a java.lang.IllegalArgumentException (String formatted incorrectly).

AWTKeyStroke.getAWTKeyStroke("C").getKeyCode();

I am using the following code for upper-case letters and numbers in a class I wrote to extend Robot:


public void typeLetterOrNumber(char c) {
    if(Character.isLetter(c)) {
        keyPress((int)c);
        keyRelease((int)c);
    }
    else if(Character.isDigit(c)) {
        keyPress(48+(int)c);
        keyRelease(48+(int)c);
    }
}

Basically, I just looked at the KeyEvent.VK_whatever values and did the appropriate math to compensate in my code.

I don't think there is an easy answer for this.

First realize that java has two byte chars and not nearly 2^16 KeyEvent.VK_'s defined. So there are going to be chars for which no KeyEvent can be generated to get that output.

Also, C and c and Ç and ç all have the same KeyEvent.getKeyCode() == KeyEvent.VK_C.

Touko

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).

If your primary goal is to write out the letters as fast as possible, you can do what I did which was to write a string to the clipboard, then just use the Robot class to enter Ctrl+V.

static void type(String s) {
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(s), null);
    robot.setAutoDelay(30);
    robot.keyPress(VK_CONTROL);
    robot.keyPress(VK_V);
    robot.keyRelease(VK_CONTROL);
    robot.keyRelease(VK_V);
}

There are some other features in the system clipboard that could allow you to save and restore any data on the clipboard, in case you needed to. As well, you if you wanted to control the speed between each character, you could put each letter on the clipboard one at a time, with a robot.delay(n); in between them, using a foreach loop.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!