栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用JNA(JAVA)的GetAsyncKeyState和VirtualKeys /特殊字符

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用JNA(JAVA)的GetAsyncKeyState和VirtualKeys /特殊字符

我知道了。经过许多小时的搜索,我设法创建了一种方法,可以将组合转换为当前键盘布局上的组合。它不处理死键(例如重音符号),但是捕获了

[SHIFT+Combinations]
我需要捕获的所有内容。

要使用它,请按以下方式调用它:

getCharacter(int vkCode, boolean shiftKeyPressed);

因此,请观看此魔术。如果我想得到

SHIFT+3
键盘上的东西(£),请使用:

getCharacter(KeyEvent.VK_3, true);

这是代码:

public static char getCharacter(int vkCode, boolean shiftKeyPressed){    byte[] keyStates = new byte[256]; //Create a keyboard map of 256 keys    if (shiftKeyPressed)    {        keyStates[16]=-127; //Emulate the shift key being held down        keyStates[160]=-128; //This needs to be set as well    }    IntByReference keyblayoutID = User32.INSTANCE.GetKeyboardLayout(0); //Load local keyboard layout    int ScanCode  = User32.INSTANCE.MapVirtualKeyExW(vkCode, MAPVK_VK_TO_VSC, keyblayoutID); //Get the scanpre    char[] buff = new char[1];    int ret = User32.INSTANCE.ToUnipreEx(vkCode, ScanCode, keyStates, buff, 1, 0, _currentInputLocaleIdentifier);    switch (ret)    {    case -1: //Error        return (char) -1;    case 0:  //No Translation        return (char) 0;    default: //Returning key...        return buff[0];    }}

以下是声明:

final static int MAPVK_VK_TO_VSC = 0;static IntByReference _currentInputLocaleIdentifier;static interface User32 extends Library {    public static User32 INSTANCE = (User32) Native.loadLibrary("User32", User32.class);    IntByReference GetKeyboardLayout(int dwLayout);    int MapVirtualKeyExW (int uCode, int nMapType, IntByReference dwhkl);    boolean GetKeyboardState(byte[] lpKeyState);    int ToUnipreEx(int wVirtKey, int wScanCode, byte[] lpKeyState, char[] pwszBuff, int cchBuff, int wFlags, IntByReference dwhkl);}

非常感谢BrendanMcK,他帮助我获得了此解决方案。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/570355.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号