我正在創建一個程序,使用我的鼠標和鍵盤自動與另一個應用程序進行交互。我發現代碼會立即點擊該鍵並立即釋放它(據我所知);這是SendKeys.Send(「{up}」)。請不要過於複雜的事情,如果你可以,即時在這裏學習。如何按住按鍵?
我的問題是如何發送按鍵,並保持一段時間由變量決定。我大致想要:
Press and hold arrow key "up"
wait(Random.next(1,10))
Release arrow key "up"
我在Visual Studio 17社區中使用Visual Basic編寫它。
謝謝!
看着Valyrean組代碼後,我已經編輯它歸結爲這樣:
Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Integer, ByVal wMapType As Integer) As Integer
Private Sub HoldKeyDown(ByVal key As Byte, ByVal durationInSeconds As Integer)
keybd_event(key, MapVirtualKey(key, 0), 0, 0) ' Down
Threading.Thread.Sleep((RandNum.Next(1000, 5000)))
keybd_event(key, MapVirtualKey(key, 0), 2, 0) ' Up
End Sub
我的問題是現在我該怎樣改變箭頭鍵被按下並保持?
忽略此編輯 – Norman
FYI'keybd_event()'已過時,被替換爲[**'SendInput()'**](HTTPS ://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v = vs.85).aspx) –