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

问题描述 投票:0回答:1

我想使用键盘键来创建 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

定时器1功能:

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
vb.net winforms
1个回答
0
投票

我没有阅读整个帖子,但我想,您会在这里找到更多信息。

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

  1. 捕获系统或应用程序级别的按键事件
  2. 检查按下了什么键
  3. 如果按下了应该用作快捷键的键,请调用按钮的单击处理子

以 Form Keydown 事件为例,使用快捷键“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
© www.soinside.com 2019 - 2024. All rights reserved.