如何生成Java中没有键代码的键盘事件? [英] How do I generate keyboard events that don't have key code in Java?

查看:120
本文介绍了如何生成Java中没有键代码的键盘事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Robot类和KeyEvent键代码生成所有其他键事件,并且它们可以正常工作,但是我还需要韩文键(切换韩文键盘).显然,KeyEvent没有用于此键的键代码,所以我被困住了:(有没有一种方法可以生成此韩文键事件? 有没有办法使用Windows的VK_HANGUL(0x15)之类的键代码代替KeyEvent键代码?如果可能的话,更改所有键代码就不会有问题...或者以某种方式将键事件一次性存储在一个永久位置并永久使用... ???

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

能够生成韩文键事件会很好,但是如果不可能,是否有关于如何实现此目标的建议? 也许我可以在键盘上将每个韩文字母与相应的字母绑定在一起(例如,在既有英文又有韩文的传统键盘上g是ㅎ),或者然后将其发送给其他应用程序?

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.

推荐答案

我找到了解决该问题的一种方法.我用JNA生成键盘事件.

I found one solution to the problem. I used JNA to generate keyboard event.

这里有一些代码,以防万一有人需要它们.

Here are some codes in case anyone needs them.

使用User32.dll中的JNA和keybd_event方法的基本知识:

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和0xF2是我一直在寻找的韩文/英文切换键的虚拟键代码和键盘扫描代码,但是查找所需代码后再替换它们,就可以生成几乎任何键事件.

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.

您需要jna.jar和platform.jar才能起作用.

You will need jna.jar and platform.jar for this to work.

这篇关于如何生成Java中没有键代码的键盘事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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