问题
I'm using Robot class and KeyEvent key codes to generate all the other key events and they work fine, but I also need Hangul key(toggle Korean keyboard). Apparently KeyEvent does not have a key code for this key, so I'm stuck :( Is there a way to generate this Hangul key event? Is there a way to use the Windows' key code like VK_HANGUL (0x15) instead of the KeyEvent key codes? If that's possible changing all the key codes wouldn't be a problem... Or somehow take the key event once and store it permanently somewhere and use that forever...???
What I am trying to do is creating an onscreen keyboard that has numbers, alphabets and Korean. Click on an icon and it will generate the key event of the corresponding letter so the letter is typed. (Everything except switching to Korean is working properly.)
Being able to generate the Hangul key event would be nice but if that's not possible, is there any suggestions on how I could achieve this? Maybe I could bind each Korean letter with corresponding alphabet on keyboard(for example g is ㅎ on conventional keyboards that have both Eng and Korean) or something but then how do I send it to other applications?
Sorry if this question is so all over the place. I'm just really lost.
回答1:
I found one solution to the problem. I used JNA to generate keyboard event.
Here are some codes in case anyone needs them.
Basic stuff for using JNA and keybd_event method from User32.dll:
import com.sun.jna.*;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.win32.StdCallLibrary;
public interface User32jna extends User32 {
User32jna INSTANCE = (User32jna) Native.loadLibrary("user32.dll",User32jna.class);
public void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
}
User32jna u32 = User32jna.INSTANCE;
And then insert this where you need to generate key event:
u32.keybd_event((byte) 0x15,(byte)0xF2,0,0);
0x15 and 0xF2 are the virtual key code and keyboard scan code for Hangul/English toggle key that I was looking for, but look up the codes for whatever key you need and then replace those, and you can generate pretty much any key event.
You will need jna.jar and platform.jar for this to work.
来源:https://stackoverflow.com/questions/27407920/how-do-i-generate-keyboard-events-that-dont-have-key-code-in-java