从java中的任意char获取VK int [英] Get the VK int from an arbitrary char in java

查看:142
本文介绍了从java中的任意char获取VK int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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


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

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.

推荐答案

也许这个丑陋的黑客:

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将包含字符串的映射(例如一个或PAGE_UP)到vk代码。

keyTextToCode would then contain the mapping from strings (e.g. "A" or "PAGE_UP") to vk codes.

这篇关于从java中的任意char获取VK int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆