用户登录  |  用户注册
首 页源码下载网络学院最新源码源码排行屏蔽广告
当前位置:新兴网络 > 网络学院 > 编程开发 > Visual Basic

VB 两种方法实现热键

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2010-06-21 11:43:30

方法一:

VBScript code复制代码
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

'添加个Timer控件 名字为:Timer1
'Interval属性设为100
Private Sub Timer1_Timer()
    If GetAsyncKeyState(vbKeyQ) Then
        Print "你按了Q"
    End If
End Sub

方法二:

VBScript code复制代码
'模块代码

Private Declare Function CallNextHookEx Lib "user32" _
    (ByVal hHook As Long, _
    ByVal nCode As Long, _
    ByVal wParam As Long, _
    lParam As Any) As Long

Private Declare Function SetWindowsHookEx Lib "user32" _
    Alias "SetWindowsHookExA" _
    (ByVal idHook As Long, _
    ByVal lpfn As Long, _
    ByVal hmod As Long, _
    ByVal dwThreadId As Long) As Long

Private Declare Function UnhookWindowsHookEx Lib "user32" _
    (ByVal hHook As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" _
    Alias "RtlMoveMemory" _
    (Destination As Any, _
    Source As Any, _
    ByVal Length As Long)

Private Type PKBDLLHOOKSTRUCT
    vkCode As Long
    scanCode As Long
    flags As Long
    Time As Long
    dwExtraInfo As Long
End Type

Private Const WM_KEYDOWN = &H100
Private Const WM_SYSKEYDOWN = &H104
Private Const WM_KEYUP = &H101
Private Const WM_SYSKEYUP = &H105

Private Const HC_ACTION = 0
Private Const WH_KEYBOARD_LL = 13
Private lngHook As Long

Public Function HotKey(ByVal nCode As Long, _
                       ByVal wParam As Long, _
                       ByVal lParam As Long) As Long
    Dim p As PKBDLLHOOKSTRUCT
    If nCode = HC_ACTION Then
        Select Case wParam
            Case WM_KEYDOWN, WM_SYSKEYDOWN
                Call CopyMemory(p, ByVal lParam, Len(p))
                If p.vkCode = vbKeyQ Then
                    Form1.Print "你按了Q"
                End If
            Case Else
               
        End Select
    End If
    Call CallNextHookEx(WH_KEYBOARD_LL, nCode, wParam, lParam)
End Function

Public Sub HooK()
    lngHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf HotKey, App.hInstance, 0)
End Sub

Public Sub UnHooK()
    Call UnhookWindowsHookEx(lngHook)
End Sub
'调用
Private Sub Form_Load()
    HooK
End Sub

Private Sub Form_Unload(Cancel As Integer)
    UnHooK
End Sub

Tags:VB Hook GetAsyncKeyState 热键

作者:佚名
  • 好的评价 如果您觉得此文章好,就请您
      0%(0)
  • 差的评价 如果您觉得此文章差,就请您
      0%(0)

网络学院评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论