Vb.net 自定义每个热键

Vb.net 自定义每个热键,vb.net,Vb.net,一点帮助,我想做一个自定义每个热键。 但我有一个问题,如果我的应用程序没有重点热键不工作 有什么想法吗 Public Class Form1 Private KeySelections As New Dictionary(Of String, Keys) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load KeySelecti

一点帮助,我想做一个自定义每个热键。 但我有一个问题,如果我的应用程序没有重点热键不工作 有什么想法吗

Public Class Form1


Private KeySelections As New Dictionary(Of String, Keys)


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    KeySelections.Add("Home", Keys.Home)
    KeySelections.Add("Insert", Keys.Insert)

    ComboBox1.Items.AddRange(KeySelections.Keys.ToArray)
End Sub

Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean

    If ComboBox1.SelectedIndex > -1 Then 


        Dim hotkey As Keys = KeySelections.Item(ComboBox1.SelectedItem.ToString)

        If keyData = hotkey Then 
            Me.Timer1.Enabled = Not Me.Timer1.Enabled    
            If Timer1.Enabled Then
                Label1.Text = "Timer 1 On"
            Else
                Label1.Text = "Timer 1 Off"
            End If
        End If

    End If

    Return MyBase.ProcessCmdKey(msg, keyData)
End Function
End Class

您需要挂接到
User32
Windows库以捕获按键

将其放在代码的顶部,导入的下方,但在模块内:

Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Int32) As Int16

然后可以使用
GetAsyncKeyState(Keys.[key])
获取特定密钥的状态。通常,如果
True
,则按键被按下。

您需要连接到
User32
Windows库以捕获按键

将其放在代码的顶部,导入的下方,但在模块内:

Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Int32) As Int16
然后可以使用
GetAsyncKeyState(Keys.[key])
获取特定密钥的状态。通常,如果
True
,则按下该键