简体   繁体   English

如何使用 C# 发送 Scorlllock 键事件

[英]How to send Scorlllock key event using C#

I have two machines which use a KVM to share one mouse and keyboard.我有两台机器,它们使用 KVM 共享一个鼠标和键盘。 When switch to another machine, I have to press keys ScrollLock + ScrollLock + num .当切换到另一台机器时,我必须按下键ScrollLock + ScrollLock + num I'm trying to create a utility to simulate this process.我正在尝试创建一个实用程序来模拟此过程。
I tired to use SendKeys.Send("{SCROLLLOCK}");我厌倦了使用SendKeys.Send("{SCROLLLOCK}"); , but it seems not work. ,但似乎不起作用。 The state of Scrolllock key didn't change. Scrolllock 键的状态没有改变。

bool ScrollLock = (((ushort)GetKeyState(0x91)) & 0xffff) != 0;  //false
SendKeys.Send("{SCROLLLOCK}");
ScrollLock = (((ushort)GetKeyState(0x91)) & 0xffff) != 0;  // still false

Then I use keybd_event to simulate the key down and up event.然后我使用keybd_event来模拟 key down 和 up 事件。 The state changed but kvm didn't response.状态发生了变化,但 kvm 没有响应。

const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
keybd_event(0x91, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);    // Scrolllock
keybd_event(0x91, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
bool ScrollLock = (((ushort)GetKeyState(0x91)) & 0xffff) != 0;   // true

keybd_event(0x91, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);    // Scrolllock
keybd_event(0x91, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
ScrollLock = (((ushort)GetKeyState(0x91)) & 0xffff) != 0;    // false

keybd_event(0x32, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);   // num 2
keybd_event(0x32, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);

Question:Does that mean KVM does not accept the key events which I simulate?问题:这是否意味着 KVM 不接受我模拟的关键事件? How to solve it?如何解决?

Hardware based KVM receive input from the physical keyboard before they forward it to the current machine.基于硬件的 KVM 在将其转发到当前机器之前从物理键盘接收输入。 If you press a special combination, it's intercepted by the KVM, interpreted and then acted upon.如果你按下一个特殊的组合,它会被 KVM 截获,解释然后采取行动。 Your computers never notice that you have pressed these keys.您的计算机永远不会注意到您按下了这些键。 Since the KVM only monitors the physical input port, it won't respond to any events happening on either machine.由于 KVM 只监控物理输入端口,它不会响应任何一台机器上发生的任何事件。 It's simply too late in the process.在这个过程中为时已晚。

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

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