How do I translate an android keyEvent's keycode to something that Robot can use on a desktop machine?
As the question states, I wish to translate android keycodes to java awt keycodes th开发者_StackOverflowat java.awt.Robot
can use in its keyPress()
and keyRelease()
methods.
The mapping is different in the two, is there a library that provides a translator? Or do I have to research the mapping charts and hardcode them in myself >
Me too, I have been wondering why Android does not follow ASCII.
I made a hardcode too, but it's a bit concise, hope that helps:
public static int toAscii(int key) {
if (key >= KeyEvent.KEYCODE_A && key <= KeyEvent.KEYCODE_Z)
return key - KeyEvent.KEYCODE_A + 'A';
else ...
}
I am starting to just hardcode it:
private int translateKeyCode(int keyCode)
{
switch (keyCode)
{
case AndroidKeyCode.KEYCODE_0:
return KeyEvent.VK_0;
case AndroidKeyCode.KEYCODE_1:
return KeyEvent.VK_1;
case AndroidKeyCode.KEYCODE_2:
return KeyEvent.VK_2;
case AndroidKeyCode.KEYCODE_3:
return KeyEvent.VK_3;
// 300 miles more of this stuff
}
}
You can use event.getUnicodeChar()
method. event
is an object of KeyEvent
class.
int asciiKey = event.getUnicodeChar(event.getMetaState());
精彩评论