自定义每个热键

时间:2015-10-08 16:37:44

标签: 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

    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

1 个答案:

答案 0 :(得分:0)

您需要挂钩User32 Windows库以捕获按键。

将其放在代码的顶部,位于Import下面,但在模块中:

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

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