如何使用C#发送Scorlllock密钥事件
我有两台使用KVM共享一只鼠标和键盘的机器。切换到另一台计算机时,我必须按键scrolllock + scrolllock + num
。我正在尝试创建一个实用程序来模拟此过程。
我厌倦了使用sendkeys.send(“ {scrolllock}”);
,但似乎不起作用。 Scrolllock键的状态没有改变。
bool ScrollLock = (((ushort)GetKeyState(0x91)) & 0xffff) != 0; //false
SendKeys.Send("{SCROLLLOCK}");
ScrollLock = (((ushort)GetKeyState(0x91)) & 0xffff) != 0; // still false
然后,我使用keybd_event
来模拟键向外事件。国家发生了变化,但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);
问题:这是否意味着KVM不接受我模拟的关键事件?如何解决?
I have two machines which use a KVM to share one mouse and keyboard. When switch to another machine, I have to press keys ScrollLock + ScrollLock + num
. I'm trying to create a utility to simulate this process.
I tired to use SendKeys.Send("{SCROLLLOCK}");
, but it seems not work. The state of Scrolllock key didn't change.
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. The state changed but kvm didn't response.
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? How to solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基于硬件的KVM将其转发到当前机器之前,请从物理键盘将其转发。如果您按特殊的组合,它将被KVM拦截,然后解释然后采取行动。您的计算机永远不会注意到您已经按下这些密钥。由于KVM仅监视物理输入端口,因此它不会响应两台机器上发生的任何事件。在此过程中为时已晚。
Hardware based KVM receive input from the physical keyboard before they forward it to the current machine. If you press a special combination, it's intercepted by the KVM, interpreted and then acted upon. 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. It's simply too late in the process.