Gre*_*jan 10 java unicode automation
我们有一个用户提供的字符串,可能包含unicode字符,我们希望机器人键入该字符串.
如何将字符串转换为机器人将使用的keyCodes?
你是如何做到这一点它也是java版本独立(1.3 - > 1.6)?
我们为"ascii"字符工作的是
//char c = nextChar();
//char c = 'a'; // this works, and so does 'A'
char c = 'á'; // this doesn't, and neither does '?'
Robot robot = new Robot();
KeyStroke key = KeyStroke.getKeyStroke("pressed " + Character.toUpperCase(c) );
if( null != key ) {
// should only have to worry about case with standard characters
if (Character.isUpperCase(c))
{
robot.keyPress(KeyEvent.VK_SHIFT);
}
robot.keyPress(key.getKeyCode());
robot.keyRelease(key.getKeyCode());
if (Character.isUpperCase(c))
{
robot.keyRelease(KeyEvent.VK_SHIFT);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 10
基于javamonkey79的代码,我创建了以下代码片段,它应该适用于所有Unicode值...
public static void pressUnicode(Robot r, int key_code)
{
r.keyPress(KeyEvent.VK_ALT);
for(int i = 3; i >= 0; --i)
{
// extracts a single decade of the key-code and adds
// an offset to get the required VK_NUMPAD key-code
int numpad_kc = key_code / (int) (Math.pow(10, i)) % 10 + KeyEvent.VK_NUMPAD0;
r.keyPress(numpad_kc);
r.keyRelease(numpad_kc);
}
r.keyRelease(KeyEvent.VK_ALT);
}
Run Code Online (Sandbox Code Playgroud)
这将自动遍历unicode键代码的每个十年,将其映射到相应的VK_NUMPAD等效项并相应地按下/释放键.
归档时间: |
|
查看次数: |
13718 次 |
最近记录: |