从java中的任意字符获取VK int

发布于 2024-07-15 12:49:45 字数 683 浏览 8 评论 0原文

如何从字母字符中获取 VK 代码? 看起来您应该能够执行类似 javax.swing.KeyStroke.getKeyStroke('c').getKeyCode() 的操作,但这不起作用(结果为零)。 如果你已经有一个KeyEvent,每个人都知道如何获取关键代码,但是如果你只想将字符转换为VK整数怎么办? 我对获取奇怪字符的 FK 代码不感兴趣,只有 [AZ]、[az]、[0-9]。

这个问题的背景-------- 我见过的所有机器人教程都假设程序员喜欢通过发送带有 VK 代码的按键来拼写单词:

int keyInput[] = { 
        KeyEvent.VK_D, 
        KeyEvent.VK_O, 
        KeyEvent.VK_N, 
        按键事件.VK_E 
    };//结束keyInput数组 
  

你可以说我很懒,但即使使用 Eclipse,这也无法在 GUI 上使用 TDD。 如果有人碰巧知道一个类似机器人的类,它接受字符串,然后模拟这些字符串的用户输入(我正在使用 FEST),我很想知道。

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(), but that doesn't work (the result is zero). Everyone knows how to get the key code if you already have a KeyEvent, but what if you just want to turn chars into VK ints? I'm not interested in getting the FK code for strange characters, only [A-Z],[a-z],[0-9].

Context of this problem --------
All of the Robot tutorials I've seen assume programmers love to spell out words by sending keypresses with VK codes:

int keyInput[] = {
      KeyEvent.VK_D,
      KeyEvent.VK_O,
      KeyEvent.VK_N,
      KeyEvent.VK_E
  };//end keyInput array

Call me lazy, but even with Eclipse this is no way to go about using TDD on GUIs. If anyone happens to know of a Robot-like class that takes strings and then simulates user input for those strings (I'm using FEST), I'd love to know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

财迷小姐 2024-07-22 12:49:45
AWTKeyStroke.getAWTKeyStroke('c').getKeyCode();

佩斯的回答进行了稍微澄清。 它应该是单引号(代表字符),而不是双引号(代表字符串)。

使用双引号将抛出 java.lang.IllegalArgumentException(字符串格式不正确)。

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

想挽留 2024-07-22 12:49:45

也许这个丑陋的 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 将包含从字符串(例如“A”或“PAGE_UP”)到 vk 代码的映射。

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.

随梦而飞# 2024-07-22 12:49:45
AWTKeyStroke.getAWTKeyStroke("C").getKeyCode();
AWTKeyStroke.getAWTKeyStroke("C").getKeyCode();
浪菊怪哟 2024-07-22 12:49:45

我在为扩展 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);
    }
}

基本上,我只是查看了 KeyEvent.VK_whatever 值,并进行了适当的数学运算以在代码中进行补偿。

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.

标点 2024-07-22 12:49:45

我认为这个问题没有一个简单的答案。

首先意识到java有两个字节字符并且没​​有定义近2^16个KeyEvent.VK_。 因此,有些字符无法生成 KeyEvent 来获取该输出。

此外,C 和 c 以及 Ç 和 ç 都具有相同的 KeyEvent.getKeyCode() == KeyEvent.VK_C

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.

小猫一只 2024-07-22 12:49:45

Adam Paynter 的答案(回答了问题Convert String to KeyEvents)也应该在这里工作。 简单但有效的想法是有一个如下所示的大开关:

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;
    // ...
    }
}

请参阅整个列表(包括周围的实用程序类)的原始答案。

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

太阳男子 2024-07-22 12:49:45

如果您的主要目标是尽快写出字母,您可以像我一样将字符串写入剪贴板,然后只需使用 Robot 类输入 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);
}

系统剪贴板中还有一些其他功能,可以让您在需要时保存和恢复剪贴板上的任何数据。 同样,如果你想控制每个字符之间的速度,你可以一次将每个字母放在剪贴板上,并在它们之间使用 robot.delay(n); ,使用foreach 循环。

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.

熊抱啵儿 2024-07-22 12:49:45

我发现有两种方法:

A. 使用 JButton.setMnemonic、getMnemonic 的解决方法:

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

B. 下载打开的 jdk 源并查看其 AbstractButton.setMnemonic(char) 方法。 该代码已获得 GPL2 许可,但这 4 行的作用通常与“keyPress(48+(int)c)”的答案相同。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文