如何使用GetAsyncKeyState创建全局热键?

时间:2013-12-15 02:14:55

标签: vb.net key

我目前正在使用Visual Basic 2010:

我想创建一个带热键的Auto Typer。即使在最小化的情况下,这些热键的目的也是如此,所以当玩视频游戏或打入网站聊天时,我可以输入“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

1 个答案:

答案 0 :(得分:0)

添加另一个计时器(timer2)以检查是否按下了F12键并且如果按下了timer1将启动并检查任何文本它是timer1中的textbox1而不是当你按下按钮时如果你将继续包括Button1.PerformClick () 或者您需要将文本发送代码移动到按钮,但我建议添加另一个计时器

这就是2个计时器的代码,(在表单加载事件或视觉工作室中将enable选项设置为true)

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