【问题标题】:How to make a global Hot Key using GetAsyncKeyState?如何使用 GetAsyncKeyState 制作全局热键?
【发布时间】:2014-01-02 15:20:19
【问题描述】:

我目前使用的是 Visual Basic 2010:

我想用热键创建一个自动打字机。这些热键的用途即使在最小化时也是如此,因此在玩视频游戏或输入网站聊天时,我可以输入“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

【问题讨论】:

标签: vb.net key


【解决方案1】:

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

这是 2 个计时器的代码,,(在表单加载事件或从 Visual Studio 中将启用选项设置为 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-22
    • 2010-11-19
    • 2014-05-27
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    相关资源
    最近更新 更多