vb 中怎样拦截键盘

我用VB做了个桌面锁,怎么能在我离开后几分钟内自动锁定,就是怎样知道一段时间内键盘没有操作。... 我用VB做了个桌面锁,怎么能在我离开后几分钟内自动锁定,就是怎样知道一段时间内键盘没有操作。 展开
 我来答
yhbo
2010-09-05 · TA获得超过180个赞
知道小有建树答主
回答量:90
采纳率:0%
帮助的人:100万
展开全部
窗体上方两个按钮,一个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
wu_xiaolin2009
2010-08-30 · TA获得超过632个赞
知道小有建树答主
回答量:378
采纳率:0%
帮助的人:300万
展开全部
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.定义一个全局的变量记录时间,在键盘钩子处理函数中初始化这个变量。就可以实现记录时间了。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式