简体   繁体   English

如何模拟按下自定义键盘键

[英]How to simulate pressing a custom keyboard key

I have an extended keyboard and I want to simulate the key press of a special key that switches the keyboard mode. 我有扩展的键盘,我想模拟切换键盘模式的特殊键的按键操作。 For that I think I need to find out key code, but can't figure out how can I get it. 为此,我认为我需要找出关键代码,但无法弄清楚如何获得它。 It's an extra key on my keyboard, not part of the ConsoleKey enum , and the only functions I've found to catch keys work with this enum . 这是我键盘上的一个额外键,不是ConsoleKey enum一部分,并且我发现捕获键的唯一功能与此enum

If the key does something like change the keyboard color, this will depend on whether or not there is software on the computer which can do so as well. 如果按键执行了诸如更改键盘颜色之类的操作,则将取决于计算机上是否也有可以执行此操作的软件。 If not, this is probably just a local switch to the keyboard and that key isn't on the keymap for the keyboard itself, ether. 如果不是这样,则可能只是本地切换到键盘,并且该键不在键盘本身(以太)的键盘映射上。

If it changes something else, it will depend on if there is driver software for the device or if it uses a default Windows driver. 如果更改了其他内容,则取决于设备是否有驱动程序软件,或者是否使用默认的Windows驱动程序。
If it takes installed software, it's probably a custom event (like some joysticks have) and would only be found if you use a reference to a library distributed by the keyboard manufacturer. 如果使用已安装的软件,则可能是自定义事件(如某些操纵杆一样),并且仅当您使用对键盘制造商分发的库的引用时才能找到。 If it does not, then the switching mode is probably a "remap" mode tied to the keyboard firmware, and just changes which KeyDown event the affected keys send. 如果不是,则切换模式可能是与键盘固件绑定的“重映射”模式,并且仅更改受影响的键发送哪个KeyDown事件。 If this is true, there is no way for Windows to even see what setting the keyboard is on. 如果这是真的,Windows甚至无法查看键盘的打开状态。

I would say the logic for the function key changes is on the keyboard itself. 我会说功能键更改的逻辑在键盘本身上。 For instance, the Fn key on my laptop does not cast any keyboard events by itself, but when combined with certain other keys, a key code is sent to the OS with a key code that triggers certain functionality. 例如,笔记本电脑上的Fn键本身不会引发任何键盘事件,但是当与某些其他键组合使用时,会将键码与触发某些功能的键码一起发送到OS。

For instance, in my keyboard Fn + Left lowers the volume, but the virtual key sent to the OS is VK_VOLUME_DOWN (see this for a list of virtual keys.) 举例来说,在我的键盘Fn + Left降低音量,但发送到OS的虚拟键VK_VOLUME_DOWN (见这个虚拟键的列表。)

Good news is, you can test the keys individually to see which key codes get sent to the OS! 好消息是,您可以单独测试按键,以查看将哪些按键代码发送到操作系统! To accomplish this, you might want to set up a low-level keyboard hook ) using P/Invokes. 为此,您可能想使用P / Invokes设置一个低级键盘hook )。

There's a tutorial on CodeProject that may be useful to you. 在CodeProject上有一个教程可能对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM