' Module1.bas
Option Explicit
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Const WM_SYSKEYDOWN = &H104
Private Const WM_SYSKEYUP = &H105
Public Type KBDLLHOOKSTRUCT
vkCode As Long ' 虚拟按键码(1--254)
scanCode As Long ' 硬件按键扫描码
flags As Long ' flags
time As Long ' 消息时间戳
dwExtraInfo As Long ' 额外信息
End Type
Public Enum KEYACTION
ACTION_KEYDOWN = WM_KEYDOWN
ACTION_KEYUP = WM_KEYUP
ACTION_SYSKEYDOWN = WM_SYSKEYDOWN
ACTION_SYSKEYUP = WM_SYSKEYUP
End Enum
' 全局键盘钩子回调函数
' 参数: action 标识键盘消息(按下,弹起), keyStruct 包含按键信息
Public Function KeyboardProc(ByVal action As KEYACTION, keyStruct As KBDLLHOOKSTRUCT) As Boolean
Select Case action
Case ACTION_KEYDOWN
Debug.Print keyStruct.vkCode, "按下键盘按键"
Case ACTION_KEYUP
Debug.Print keyStruct.vkCode, "弹起键盘按键"
Case ACTION_SYSKEYDOWN
Case ACTION_SYSKEYUP
End Select
' 返回 True 表示继续传递按键消息
' 返回 False 表示结束按键消息传递
KeyboardProc = False
End Function
' Form1.frm
' 窗体上添加1个CommandButton,改名为cmdStartMask
Option Explicit
Private Declare Function StartMaskKey Lib "MaskKey" (lpdwVirtualKey As Long, ByVal nLength As Long, Optional ByVal bDisableKeyboard As Boolean = False) As Long
Private Declare Function StopMaskKey Lib "MaskKey" () As Long
Private Sub cmdStartMask_Click()
Dim key(7) As Long
key(0) = vbKeyPageUp
key(1) = vbKeyPageDown
key(2) = vbKeyReturn
key(3) = vbKeyLeft
key(4) = vbKeyRight
key(5) = vbKeyUp
key(6) = vbKeyDown
key(7) = vbKeySpace
If cmdStartMask.Caption = "开始屏蔽" Then
StartMaskKey key(0), UBound(key) + 1
cmdStartMask.Caption = "停止屏蔽"
Else
StopMaskKey
cmdStartMask.Caption = "开始屏蔽"
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
If cmdStartMask.Caption = "停止屏蔽" Then
StopMaskKey
End If
End Sub
成品DLL及VB demo下载地址:
https://download.csdn.net/download/hangvane123/10701404
MaskKey.dll源码及VC++调用请查看原文(原文下载链接已失效)