- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在开发可在全屏游戏中运行的双向私有(private)聊天。
这是让用户在屏幕顶部的半透明文本框中输入内容所必需的即使它没有焦点。
使用以下代码,我可以检测到所有物理键,但很难检测到虚拟键。
检测到SHIFT
。
2
被检测到。
但是 Shift + 2
被检测为两个单独的键(即使 [SHIFT+2]
在我的键盘上给出了 @
)。 IE:程序同时输出 SHIFT 和 2,但不输出它们产生的结果:@
。
问题是,如何根据键盘转换为字符?例如:
"
(引号)。@
。如何根据键盘转换为特定字符?
这是目前的代码:
static interface User32 extends Library {
public static User32 INSTANCE = (User32) Native.loadLibrary("User32", User32.class);
short GetAsyncKeyState(int key);
short GetKeyState(int key);
IntByReference GetKeyboardLayout(int dwLayout);
int MapVirtualKeyExW (int uCode, int nMapType, IntByReference dwhkl);
boolean GetKeyboardState(byte[] lpKeyState);
int ToUnicodeEx(int wVirtKey, int wScanCode, byte[] lpKeyState, char[] pwszBuff, int cchBuff, int wFlags, IntByReference dwhkl);
}
public static void main(String[] args) {
long currTime = System.currentTimeMillis();
while (System.currentTimeMillis() < currTime + 20000)
{
for (int key = 1; key < 256; key++)
{
if (isKeyPressed(key))
getKeyType(key);
}
}
}
private static boolean isKeyPressed(int key)
{
return User32.INSTANCE.GetAsyncKeyState(key) == -32767;
}
private static void getKeyType(int key)
{
boolean isDownShift = (User32.INSTANCE.GetKeyState(VK_SHIFT) & 0x80) == 0x80;
boolean isDownCapsLock = (User32.INSTANCE.GetKeyState(VK_CAPS)) != 0;
byte[] keystate = new byte[256];
User32.INSTANCE.GetKeyboardState(keystate);
IntByReference keyblayoutID = User32.INSTANCE.GetKeyboardLayout(0);
int ScanCode = User32.INSTANCE.MapVirtualKeyExW(key, MAPVK_VK_TO_VSC, keyblayoutID);
char[] buff = new char[10];
int bufflen = buff.length;
int ret = User32.INSTANCE.ToUnicodeEx(key, ScanCode, keystate, buff, bufflen, 0, keyblayoutID);
switch (ret)
{
case -1:
System.out.println("Error");
break;
case 0: // no translation
break;
default:
System.out.println("output=" + String.valueOf(buff).substring(0, ret));
}
}
它工作正常并输出按下的键,但不适用于 Shift + 组合。我意识到我可以执行“切换”并将 Shift+3 更改为“£”,但这不适用于不同的键盘。
最佳答案
尝试使用JIntelliType图书馆代替。它比 JNA 使用起来简单得多,它应该能够执行 SHIFT + key (MOD_SHIFT)。您可能遇到的唯一问题是检测 3
,但这很容易解决(例如通过 KeyListener 打印键的代码)。
关于java - 使用 JNA (JAVA) 的 GetAsyncKeyState 和 VirtualKeys/特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6237250/
我想将虚拟键转换为 WideString。这就是我到目前为止所拥有的...... function VKeytoWideString (Key : Word) : WideString; var W
我正在为 Windows 8 编写一个 Windows 应用商店应用程序。我在屏幕上有两个按钮,我想用空格键切换:当按下开始按钮时,我禁用它并启用停止按钮,并且反之亦然。 我可以开始和停止使用空格键,
我想本地化 VirtualKey for Control。目前有这样的代码: using Windows.System; var message = "Press " + VirtualKey.Con
我正在尝试使用 user32.dll 中的 ToUnicodeEx 函数将 virtualKey 转换为 unicode。根据文档,如果成功,此方法应返回 1 个或多个。在我的例子中,它总是返回 0。
我正在设置一个在单个页面上工作的全局键盘快捷键,比如 Ctrl + someKey 来做一些事情,Ctrl + Alt + otherKey 来激活一些其他的事情等等在。我或多或少有 Ctrl + V
在标准 .NET 中,存在 ToAscii/ToUnicode 和 MapVirtualKey 函数来处理此功能,但似乎没有将等效函数或库引入 Metro/WinRT。如果 Metro/WinRT 中
我使用 C# 扩展了 Windows.UI.Xaml.Controls.TextBox 类,以在用户键入时处理输入的自定义格式。我的逻辑是从两个事件处理程序调用的,一个用于 TextChanged 事
我正在使用 C# 为 HoloLens 编写 VNC 客户端,但我很难弄清楚如何处理键盘输入。 KeyUp/KeyDown 给了我一个 Windows.System.VirtualKey 对象,但似乎
我正在开发可在全屏游戏中运行的双向私有(private)聊天。 这是让用户在屏幕顶部的半透明文本框中输入内容所必需的即使它没有焦点。 使用以下代码,我可以检测到所有物理键,但很难检测到虚拟键。 检测到
我知道如何处理关键事件,即 private void Page_KeyUp(object sender, KeyRoutedEventArgs e) { switch (e.Key) {
我是一名优秀的程序员,十分优秀!