5

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.

Jade
  • 51
  • 2
  • This may help you. Not marking as duplicate as it does not appear to be a duplicate (actual key vs Unicode interpretation). http://stackoverflow.com/questions/397113/how-to-make-the-java-awt-robot-type-unicode-characters-is-it-possible I'm not 100% familiar with Robot or Hangul so I'm not sure if there's interpreted unicode. – Compass Dec 10 '14 at 21:20
  • Thanks for commenting! Using the alt code like in your link is actually a good idea. However, you need to Hold 'Alt' and then go to numpad and press '+' and other digits of Unicode hex, right? How do I get the robot to press NUMPAD key '+'? I tried SHIFT= but that does not work... – Jade Dec 11 '14 at 05:00
  • http://stackoverflow.com/questions/15605109/java-keybinding-plus-key I'm not really good with key events, but this sounds like it'll work – Compass Dec 11 '14 at 05:08
  • Also, due to the way Korean language is, (one letter is usually a combination of 2-3 characters if that makes sense, like 한 is ㅎ+ㅏ+ㄴ, there are thousands of combination) mapping every letter using unicode is really hard. – Jade Dec 11 '14 at 05:19
  • Do you have a delay? http://stackoverflow.com/questions/14595483/using-java-to-send-key-combinations – Compass Dec 11 '14 at 05:45
  • Giving the delay doesn't seem to change anything... – Jade Dec 11 '14 at 05:56
  • I'm stumped, then :( – Compass Dec 11 '14 at 06:02

1 Answers1

0

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.

Jade
  • 51
  • 2
  • 1
    using JNA for this type of task will most likely break your functionality at some point int the future. The WINAPI changes rapidly nowadays, hard-linking your bytecode to machinecode requires constant testing on all of your target platforms - and at some point even rewriting it, with constant primitives (like the ones you are using) this may work a little bit longer since microsoft doesnt change constants often - but it WILL happen, thats why they introduced native macros for these, they will always map to the correct primitive. – specializt Dec 12 '14 at 15:48
  • Yes that is along the lines with what I was concerned about at first which is why I wrote 'one' solution instead of 'the' solution. Eventually I figured it wouldn't matter in my case since this is just a semester long project. However would you please tell me more about the native macros? I didn't know they existed. What keywords should I search for to get information about those? – Jade Dec 13 '14 at 03:08
  • 1
    short overview : http://msdn.microsoft.com/en-us/library/b0084kay.aspx. Most of the tens of thousands of macros are function-specific, they are always mentioned and explained in each function which uses them - some people might refer to them as parameter constants, in your case : http://msdn.microsoft.com/en-us/library/windows/desktop/ms646304%28v=vs.85%29.aspx and especially http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx -- its usually much better to write a native app for this kind of task, adhering to all of the microsoft standards while doing so. – specializt Dec 13 '14 at 11:35