比如说一个页面上有一个按扭,当CLICK时,弹出一个警告框吧.
 我现在想用键盘来实现这个功能,比如说把这个按扭CLICK事件用一个"快捷字母T"来实现,就是说我一按"T",也能弹出一个警告框,请问用什么函数来捕捉这个事件(T输入呢),刚接触VB,别见笑

解决方案 »

  1.   

    把command1的caption 设置为&T
      sub command1_click()
       msgbox "Hello"
      end sub
      

  2.   

    简单点的就这样:
    Option ExplicitPrivate Sub Command1_Click()
        MsgBox "hello"
    End SubPrivate Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        If KeyCode = vbKeyT Then
            Command1_Click
        End If
    End SubPrivate Sub Form_Load()
        Me.KeyPreview = True
    End Sub
      

  3.   

    这样也行:
    Option ExplicitPrivate Const MOD_ALT = &H1
    Private Const MOD_CONTROL = &H2
    Private Const MOD_SHIFT = &H4
    Private Const PM_REMOVE = &H1
    Private Const WM_HOTKEY = &H312
    Private Type POINTAPI
        x As Long
        y As Long
    End Type
    Private Type Msg
        hWnd As Long
        Message As Long
        wParam As Long
        lParam As Long
        time As Long
        pt As POINTAPI
    End Type
    Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
    Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long
    Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
    Private Declare Function WaitMessage Lib "user32" () As Long
    Private bCancel As Boolean
    Private Sub ProcessMessages()
        Dim Message As Msg
        'loop until bCancel is set to True
        Do While Not bCancel
            'wait for a message
            WaitMessage
            'check if it's a HOTKEY-message
            If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
                Command1_Click
            End If
            'let the operating system process other events
            DoEvents
        Loop
    End SubPrivate Sub Command1_Click()
        MsgBox "hello"
    End SubPrivate Sub Form_Load()    '
        Dim ret As Long
        bCancel = False
        ret = RegisterHotKey(Me.hWnd, &HBFFF&, 0, vbKeyT)
        MsgBox ret
        Show
        'process the Hotkey messages
        ProcessMessages
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
        bCancel = True
        'unregister hotkey
        Call UnregisterHotKey(Me.hWnd, &HBFFF&)
    End Sub