'How to send Scorlllock key event using C#
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?
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Christof Wollenhaupt |