您能否告诉我将移位键暂停一段时间然后发布的代码?我尝试使用Sendkeys.Send(“+”),但它只推了一次,我希望它能保持约3秒然后释放。提前感谢您的帮助。
答案 0 :(得分:1)
我相信这可能已在这里得到解答:
How to simulate held down keys with VB.NET or C#?
他们确实提到DoEvents可能不是最好的循环,这是另一种方法:https://www.experts-exchange.com/questions/22634280/Holding-a-key-down-my-code-not-working-VB-NET.html
如果链接死亡: 如果你想模拟“真正的”键盘睡眠,我会选择系统设置键盘延迟/速度。这可以使用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
不确定这是否有用,似乎只是按键一堆 - 我没有机会测试这个
答案 1 :(得分:0)
试一试。您可能还需要将其格式化为偶数秒。
Private Sub HoldDown()
Dim dt As DateTime = Now
Do Until Now = DateAdd(DateInterval.Second, 3, dt)
SendKeys.Send("+")
Loop
End Sub
当然,我想不出你想要这样做的任何实际原因。