如何创建按钮的键盘快捷键?

时间:2019-09-20 22:38:06

标签: vb.net winforms

我要使用键盘键来创建Button1的键盘快捷键:

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Toggle = Toggle + 1
    If Toggle = 1 Then
        Timer1.Start()
        Button1.Text = "Toggle Off"
    Else
        Timer1.Stop()
        Toggle = 0
        Button1.Text = "Toggle On"
    End If

Timer1功能:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Randomize()
    Dim rnd As New Random
    Dim minval As Integer
    Dim maxval As Integer

    minval = 1000 / TrackBar1.Value
    maxval = 1000 / TrackBar2.Value

    Timer1.Interval = rnd.Next(maxval, minval)

    If MouseButtons = MouseButtons.Left Then
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

I didn't read the whole thread, but I think, you will find some more information here.

基本上,您需要执行以下步骤:

  1. 捕获系统或应用程序级别的按键事件
  2. 检查按下了什么键
  3. 如果按下了应用作快捷键的键,请调用您的Button的Click Handling Sub

例如,以“表单按键事件”为例,使用快捷方式“ a”调用按钮“ LeButton”(当按下按键“ a”时) (此示例不起作用,因为MyBase.KeyDown并非应用程序范围的事件,但可能会对您有所帮助)

Public Sub form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown   
   If e.KeyCode = Keys.A Then
      LeButton_Click()
   End If
End Sub

Public Sub LeButton_Click(sender As Object, e As EventArgs) Handles LeButton.Click
     MsgBox("This is a Message Box")
End Sub

对不起,我的语法不好,我很年轻,还很年轻...