从java中的任意字符获取VK int
如何从字母字符中获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
对佩斯的回答进行了稍微澄清。 它应该是单引号(代表字符),而不是双引号(代表字符串)。
使用双引号将抛出 java.lang.IllegalArgumentException(字符串格式不正确)。
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).
也许这个丑陋的 hack:
keyTextToCode 将包含从字符串(例如“A”或“PAGE_UP”)到 vk 代码的映射。
Maybe this ugly hack:
keyTextToCode would then contain the mapping from strings (e.g. "A" or "PAGE_UP") to vk codes.
我在为扩展 Robot 而编写的类中使用以下代码来表示大写字母和数字:
基本上,我只是查看了 KeyEvent.VK_whatever 值,并进行了适当的数学运算以在代码中进行补偿。
I am using the following code for upper-case letters and numbers in a class I wrote to extend Robot:
Basically, I just looked at the KeyEvent.VK_whatever values and did the appropriate math to compensate in my code.
我认为这个问题没有一个简单的答案。
首先意识到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
.Adam Paynter 的答案(回答了问题Convert String to KeyEvents)也应该在这里工作。 简单但有效的想法是有一个如下所示的大开关:
请参阅整个列表(包括周围的实用程序类)的原始答案。
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:
See the original answer for the whole listing (including surrounding utility class).
如果您的主要目标是尽快写出字母,您可以像我一样将字符串写入剪贴板,然后只需使用
Robot
类输入 Ctrl+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.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.我发现有两种方法:
A. 使用 JButton.setMnemonic、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:
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