Is it possible to programmatically set the state of the shift and control keys?
The reason I am asking is that I am thinking of building a foot switch to act as shift and control keys - well two switches, one for each foot.
I'm planning on using the Arduino fo开发者_StackOverflow中文版r this and writing a small C# application to detect when the switch has been pressed that would then set the state of shift or control. I would rather not have to write a keyboard driver for the Arduino as I would like it to do other things as well.
Yes it is possible, but you will have to pInvoke out to Win32. Have a look at the keydb_event call.
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
You can then call:
keybd_event(0x10, 0, 0, 0);
to turn on the shift key (ie. key down), and:
keybd_event(0x10, 0, 2, 0);
to turn it off again (ie. key up).
The first argument is the hex value of the key to press:
- 0x10 = shift
- 0x11 = ctrl
- 0x12 = alt
I've just tried this in a simple C# application using a checkbox to represent the shift key. With it checked, it "held" the shift key in for any application I used.
If you're only interested in controlling the foreground application, the easiest solution is to send WM_KEYDOWN and WM_KEYUP messages to the active application for the press and release of your pedal.
I could be mistaken (and wiser heads, feel free to correct me if I am!) but I believe that to create an actual system-level keypress event -- e.g. to make Windows create the keydown/keyup messages for you and work correctly even if you change applications while the pedal is held down -- you need to write an input driver.
精彩评论