vb 中怎样拦截键盘
展开全部
窗体上方两个按钮,一个Timer控件。
当按下command1后,如果10秒内键盘没有操作,键盘自动锁定。按command2开锁,并重新开始计时。
如果要修改离开时间,可将倒数第5行和倒数第6行代码改为:
If kbdllhs.vkCode <> 0 And n < 设定的时间(单位为秒)Then n = 0
If n >= 设定的时间(单位为秒)Then
窗体代码:
Option Explicit
Private Sub Command1_Click()
m_hDllKbdHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, App.hInstance, 0&)
If m_hDllKbdHook = 0 Then
MsgBox "安装钩子失败."
End If
Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
Call UnhookWindowsHookEx(m_hDllKbdHook)
n = 0
Command1_Click
End Sub
Private Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 1000
Command1.Caption = "锁定"
Command2.Enabled = "开锁"
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call UnhookWindowsHookEx(m_hDllKbdHook)
End Sub
Private Sub Timer1_Timer()
n = n + 1
End Sub
模块代码:
Option Explicit
Public m_hDllKbdHook As Long
Public Const WH_KEYBOARD_LL As Long = 13
Private Type KBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
Public kbdllhs As KBDLLHOOKSTRUCT
Public n As Integer
Public 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
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal cb As Long)
Public Function LowLevelKeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Call CopyMemory(kbdllhs, ByVal lParam, Len(kbdllhs))
If kbdllhs.vkCode <> 0 And n < 10 Then n = 0
If n >= 10 Then
LowLevelKeyboardProc = 1
Form1.Timer1.Enabled = False
End If
End Function
当按下command1后,如果10秒内键盘没有操作,键盘自动锁定。按command2开锁,并重新开始计时。
如果要修改离开时间,可将倒数第5行和倒数第6行代码改为:
If kbdllhs.vkCode <> 0 And n < 设定的时间(单位为秒)Then n = 0
If n >= 设定的时间(单位为秒)Then
窗体代码:
Option Explicit
Private Sub Command1_Click()
m_hDllKbdHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, App.hInstance, 0&)
If m_hDllKbdHook = 0 Then
MsgBox "安装钩子失败."
End If
Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
Call UnhookWindowsHookEx(m_hDllKbdHook)
n = 0
Command1_Click
End Sub
Private Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 1000
Command1.Caption = "锁定"
Command2.Enabled = "开锁"
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call UnhookWindowsHookEx(m_hDllKbdHook)
End Sub
Private Sub Timer1_Timer()
n = n + 1
End Sub
模块代码:
Option Explicit
Public m_hDllKbdHook As Long
Public Const WH_KEYBOARD_LL As Long = 13
Private Type KBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
Public kbdllhs As KBDLLHOOKSTRUCT
Public n As Integer
Public 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
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal cb As Long)
Public Function LowLevelKeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Call CopyMemory(kbdllhs, ByVal lParam, Len(kbdllhs))
If kbdllhs.vkCode <> 0 And n < 10 Then n = 0
If n >= 10 Then
LowLevelKeyboardProc = 1
Form1.Timer1.Enabled = False
End If
End Function
展开全部
1.做个键盘钩子,捕捉键盘事件
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
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
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Long) As Long
2.定义一个全局的变量记录时间,在键盘钩子处理函数中初始化这个变量。就可以实现记录时间了。
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
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
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Long) As Long
2.定义一个全局的变量记录时间,在键盘钩子处理函数中初始化这个变量。就可以实现记录时间了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询