虚拟键盘的Java输入法
我在实现虚拟键盘输入法时遇到问题。目前我正在使用机器人类从虚拟键盘将输入发送到任何应用程序。但为此,我需要创建键码和 unicode 的映射,这在不同的键盘布局上不一致,我可以使用输入法直接将 UNICODE 传递给任何应用程序,而不必担心键码和 unicode 之间的映射吗?
任何有用的链接或示例代码都会有用。
它是一个简单的 Java 程序,始终位于任何应用程序之上,并充当屏幕键盘。使用鼠标同时按下键盘上的任意按钮(键),相应的字符将在下面运行的应用程序中键入。这对于英文字母来说非常有效。我在处理 unicode 时遇到问题。
找到下面的代码片段
public static void simulateKeyEvent(char key){
try{
AWTKeyStroke awtKS = AWTKeyStroke.getAWTKeyStroke(key);
int key_code = awtKS.getKeyCode();
System.out.println("key = "+key+" keyCode = "+key_code);
robot.keyPress(key_code);
robot.keyRelease(key_code);
}catch(Exception e){
e.printStackTrace();
}
}
I am facing a problem in implementing Input method for Virtual Keyboard. Currently I am using robot class for sending input to any application from virtual keyboard. But for that I need to create mapping of key-code and unicode, which is not consistent on different keyboard layout, can I directly pass the UNICODE to any application using Input method without worry about mapping between keycode and unicode.
Any useful link or sample code will be useful.
It is a simple Java program which is always on top of any application and work as onscreen keyboard. Using a mouse while you press any button (key) of the keyboard, the corresponding character will be typed in the application running below. This is working perfectly for English Alphabets. I am facing problem while I am doing for unicode.
find the code snippet below
public static void simulateKeyEvent(char key){
try{
AWTKeyStroke awtKS = AWTKeyStroke.getAWTKeyStroke(key);
int key_code = awtKS.getKeyCode();
System.out.println("key = "+key+" keyCode = "+key_code);
robot.keyPress(key_code);
robot.keyRelease(key_code);
}catch(Exception e){
e.printStackTrace();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的虚拟键盘是否被操作系统用作设备?
或者,换句话说,您是否尝试过将其视为“真正的”键盘?
根据 Java 硬件抽象,如果您的虚拟键盘被视为驱动程序,它应该像真实键盘一样工作。
编辑:根据评论,这不是一个虚拟设备,而是一个Java应用程序,因此,问题是不同的。
根据 Javadoc,Robot 可以发送 键击以 int 形式给出。要从字符创建这些击键,我建议您使用
getKeyStroke(char)
之前使用getKeycode()
。这样,您将拥有与您的 unicode 字符关联的整数值,无论它们是什么。编辑2:再次修改;-)
看起来像
getKeyStroke(String)
“应该”处理 unicode 字符。Is your virtual keyboard used as a device by your OS ?
Or, in other words, have you tried considering it as a "real" keyboard ?
According to Java hardware abstraction, were your virtual keyboard to be considered as a driver, it should simply work like a real keyboard.
EDIT : according to comment, this is not a virtual device, but a Java application, as a consequence, probleme is different.
According to Javadoc, Robot can send key strokes given as int. To create those key strokes from characters, I would recommand you create them using
getKeystroke(char)
before to transform them into integer values usinggetKeycode()
. This way, you would have integer values associatesd your unicode characters, whichever they are.EDIT 2 : once again, a modification ;-)
it seems like
getKeyStroke(String)
"should" handle unicode characters.我是如何解决的:
How I sovled it: