C# 多个键盘和低级钩子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/91234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:32:11  来源:igfitidea点击:

Multiple keyboards and low-level hooks

提问by Ray Hayes

I have a system where I have multiple keyboards and really need to know which keyboard the key stroke is coming from. To explain the set up:

我有一个系统,我有多个键盘,真的需要知道击键来自哪个键盘。解释设置:

  1. I have a normal PC and USB keyboard
  2. I have an external VGA screen with some hard-keys
  3. The hard keys are mapped as a standard USB keyboard, sending a limited number of key-codes (F1, F2, Return, + and -)
  1. 我有一台普通的 PC 和 USB 键盘
  2. 我有一个带有一些硬键的外置 VGA 屏幕
  3. 硬键映射为标准 USB 键盘,发送有限数量的键码(F1、F2、Return、+ 和 -)

I have a low-level hook (in C# but actually calling upon Win32 functionality) which is able to deal with the input even when my application is not focused.

我有一个低级挂钩(在 C# 中,但实际上调用了 Win32 功能),即使我的应用程序没有获得焦点,它也能够处理输入。

The problem is that when using the normal keyboard, some of the mapped key-codes at picked up by the application being driven on the external screen. One of the key-presses sent by the external screen and used for confirmation is VK_RETURN. Unless I can identify the "device" and filter upon it, the user could be performing actions and confirming them on a screen their not even looking at.

问题是,当使用普通键盘时,一些映射的键码会被外部屏幕上的应用程序所驱动。外部屏幕发送的用于确认的按键之一是 VK_RETURN。除非我可以识别“设备”并对其进行过滤,否则用户可能正在执行操作并在他们甚至不看的屏幕上确认它们。

How do I know which keyboard was responsible for the key-press?

我怎么知道哪个键盘负责按键?

采纳答案by Roel

Yes I stand corrected, my bad, learning something new every day.

是的,我站得更正,我的坏,每天都在学习新东西。

Here's my attempt at making up for it :) :

这是我弥补它的尝试:):

  • Register the devices you want to use for raw input (the two keyboards) with ::RegisterRawInputDevices().

  • You can get these devices from GetRawInputDeviceList()

  • After you've registered your devices, you will start getting WM_INPUT messages.

  • The lParam of the WM_INPUT message contains a RAWKEYBOARD structure that you can use to determine the keyboard where the input came from, plus the virtual keycode and the type of message (WM_KEYDOWN, WM_KEYUP, ...)

  • So you can set a flag of where the last message came from and then dispatch it to the regular keyboard input handlers.

  • 使用 ::RegisterRawInputDevices() 注册要用于原始输入的设备(两个键盘)。

  • 您可以从 GetRawInputDeviceList() 获取这些设备

  • 注册设备后,您将开始收到 WM_INPUT 消息。

  • WM_INPUT 消息的 lParam 包含一个 RAWKEYBOARD 结构,您可以使用它来确定输入来自哪个键盘,以及虚拟键码和消息类型(WM_KEYDOWN、WM_KEYUP,...)

  • 因此,您可以设置最后一条消息来自何处的标志,然后将其分派到常规键盘输入处理程序。

回答by Roel

No way to do this. Windows abstracts this for you. As mentioned, you need to write/modify a device driver.

没有办法做到这一点。Windows 为您抽象了这一点。如前所述,您需要编写/修改设备驱动程序。