I know how to use SendKeys() but how do i go about it if i would like to simulate holding ESCAPE key for like 5 seconds?
Asked
Active
Viewed 4,393 times
2 Answers
14
You can PInvoke keybd_event
and hold down Escape key for 5 seconds and then release it:
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
keybd_event(VK_ESCAPE, 0, 0, 0) // KEY_DOWN
System.Threading.Thread.Sleep(5000);
keybd_event(VK_ESCAPE, 0, KEYEVENTF_KEYUP, 0) // KEY_UP

fardjad
- 20,031
- 6
- 53
- 68
-
-
-
ummm... take look at this: http://api.farmanager.com/en/winapi/virtualkeycodes.html – fardjad Dec 03 '10 at 10:57
-
Im testing a abit, if i replace VK_ESCAPE with VK_A i should get "aaaaaaaaaa" in a textfield if i sleep it X seconds shouldnt i? im just getting one at the time (testing with a Timer loop) U are using KEYEVENTF_KEYUP = 0x02 right? – Jason94 Dec 03 '10 at 13:42
-
`KEYEVENTF_KEYUP` should be `2` or `0x02`. I think As you sleep your application thread after holding down A, it won't respond to keybd_event (Thus just one `A` written in the TextBox in your application) . Use a timer or make another thread instead. – fardjad Dec 03 '10 at 14:40
-2
try using a timer... use System.Forms.Timer... for 5000ms... then if the 5000ms is finished, shut the timer off..

Gian Santillan
- 753
- 1
- 7
- 16
-
Read the question again, this isn't about reading a long keypress, but about sending one. – Gerrie Schenck Dec 03 '10 at 10:32
-
1I guess he want to programmatically generate something like `KeyDown` and after 5 secs a `KeyUp` event, not sending a KeyPress and wait 5 seconds... – digEmAll Dec 03 '10 at 10:32