2013-12-15 96 views
1

我目前使用的是Visual Basic 2010:如何使用GetAsyncKeyState創建全局熱鍵?

我想用熱鍵創建一個自動打字機。這些熱鍵的目的即使在最小化時也是如此,所以當玩視頻遊戲或打字進入網站聊天時,我可以鍵入「F12」,它會發送我已經預設的任何消息,而不必取消最小化我的程序製作!

下面是我所擁有的,到目前爲止,我會很感激一些幫助。

Public Class Form1 
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Int32) _ 
                     As Short 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 
     If GetAsyncKeyState(Keys.F12) Then Button1.PerformClick() 
     SendKeys.Send(TextBox1.Text) 
     SendKeys.Send("{Enter}") 
    End Sub 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     If TextBox1.Text = "" Then 
      MsgBox("The message box was left blank!", MsgBoxStyle.Exclamation) 
      Timer1.Stop() 
     Else 
      Timer1.Interval = TextBox2.Text * 1000 
      Timer1.Start() 
      Me.WindowState = FormWindowState.Minimized 
     End If 
    End Sub 

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
     Timer1.Stop() 
    End Sub 
End Class 
+0

你將需要考慮使用這樣的Windows API函數[SO問題](http://stackoverflow.com/questions/14168258/registering-3-hotkeys-possible/14168506#14168506)問或您需要使用像這樣的圖書館[GlobalMouseKeyboardHook](http://globalmousekeyhook.codeplex.com/) –

回答

0

添加另一個定時器(定時器2)檢查F12鍵按下與否,如果按下定時器1將啓動並檢查是否有任何文本它TIMER1的TextBox1中而不是當你按下按鈕,這就是,如果你繼續包括Button1.PerformClick() 或者您將需要將文本發送代碼移動到按鈕,但我建議添加另一個計時器

這是2個定時器的代碼,(將啓用選項設置爲真表單加載事件或從Visual Studio)

Public Class Form1 
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Int32) 
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 
    If GetAsyncKeyState(Keys.F12) Then 
     Timer2.Start() 
    End If 
End Sub 

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick 
    If TextBox1.Text <> "" Then 
     SendKeys.Send(TextBox1.Text) 
     SendKeys.Send("{Enter}") 
    Else 
     MsgBox("Error", MsgBoxStyle.Exclamation) 
     Timer1.Stop() 
    End If 
End Sub 
End Class