Could you please tell me the code to make the shift key be held for a while and then released? I tried to use Sendkeys.Send ("+"), but it only pushes it once, and I'd like it to hold for about 3 seconds and then release. Thank you in advance for your help.
-
Never post an image of code. It will attract down votes. – LarsTech Nov 21 '17 at 17:57
-
1See https://stackoverflow.com/a/16343101/832052 – djv Nov 21 '17 at 18:15
-
Based on your other comment below, it sounds like you only need to check the Shift key when you are mousing down. That's a different question. – LarsTech Nov 21 '17 at 20:04
-
If you un-accepted Poat's answer because it doesn't fit your needs then you should add a comment explaining why not, so that he can improve it. – Visual Vincent Nov 21 '17 at 22:51
2 Answers
I believe this may have already been answered here:
How to simulate held down keys with VB.NET or C#?
They do mentioned that DoEvents may not be the best thing to do in a loop like so, here is an alternative approach: https://www.experts-exchange.com/questions/22634280/Holding-a-key-down-my-code-not-working-VB-NET.html
and in case the link dies: If you want to simulate a "real" keyboard sleep, I would then go with system settings for keyboard delay/speed. THis can be done with SystemParametersInfo API
Const SPI_GETKEYBOARDDELAY = 22
Const SPI_GETKEYBOARDSPEED = 10
Private Declare Function SystemParametersInfo Lib "user32" Alias _
"SystemParametersInfoA" (ByVal uAction As Integer, ByVal uParam As Integer, _
ByRef lpvParam As Integer, ByVal fuWinIni As Integer) As Integer
Private Sub HoldKeyDown(ByVal key As Byte, ByVal durationInSeconds As Integer)
Dim targetTime As DateTime = DateTime.Now().AddSeconds(durationInSeconds)
Dim kb_delay As Integer
Dim kb_speed As Integer
SystemParametersInfo(SPI_GETKEYBOARDDELAY, 0, kb_delay, 0)
SystemParametersInfo(SPI_GETKEYBOARDSPEED, 0, kb_speed, 0)
While targetTime.Subtract(DateTime.Now()).TotalSeconds > 0
keybd_event(key, MapVirtualKey(key, 0), 0, 0) ' Up key pressed
keybd_event(key, MapVirtualKey(key, 0), 2, 0) ' Up key released
System.Threading.Thread.Sleep(kb_delay + kb_speed)
End While
End Sub
Not sure if this'll work, seems it's just pressing the key a bunch - I've not had a chance to test this

- 327
- 1
- 11
-
Neat solution, however be advised that [**`keybd_event()`**](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646304(v=vs.85).aspx) is officially deprecated and replaced by [**`SendInput()`**](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx), so the former might get removed some time in the future. – Visual Vincent Nov 21 '17 at 20:33
-
I have made an object-oriented library (called `InputHelper`) that uses the latest functions to simulate input (`InputHelper.Keyboard` utilizes `SendInput`). I haven't taken the time to make a proper documentation for it yet, but the code and release files includes XML comments that are automatically shown by Visual Studio's IntelliSense while you're coding. -- **Link** (with a short explanation): https://stackoverflow.com/a/45420637/3740093 – Visual Vincent Nov 21 '17 at 20:42
Give this a try. You may have to format it to even seconds as well.
Private Sub HoldDown()
Dim dt As DateTime = Now
Do Until Now = DateAdd(DateInterval.Second, 3, dt)
SendKeys.Send("+")
Loop
End Sub
Of course, I cannot think of any practical reason why you would want to do this.

- 11,506
- 5
- 20
- 33
-
I'm doing a program that clicks on specific places, like an autoclicker with coordinates. But at any given time, I need it to click holding the shift key in a location. And I do not know how to do that. – Happy Nov 21 '17 at 18:08
-
Google "Visual Studio SendKeys". First Link. Bottom of the page. (https://msdn.microsoft.com/it-it/library/system.windows.forms.sendkeys.send(v=vs.110).aspx) – Jimi Nov 21 '17 at 18:32