5个回答
展开全部
模块代码:
Option Explicit
Public zhixing As Boolean
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 Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, ByVal lpvSource As Long, ByVal cbCopy As Long)
Public Type KEYMSGS
vKey As Long '虚拟码 (and &HFF)
sKey As Long '扫描码
flag As Long '键按下:128 抬起:0
time As Long 'Window运行时间
End Type
Public Const WH_KEYBOARD_LL = 13
'-----------------------------------------
'消息
Public Const HC_ACTION = 0
'键盘消息
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const WM_SYSKEYDOWN = &H104
Public Const WM_SYSKEYUP = &H105
Public Declare Function GetKeyNameText Lib "user32" Alias "GetKeyNameTextA" (ByVal lParam As Long, ByVal lpBuffer As String, ByVal nSize As Long) As Long
Public strKeyName As String * 255
Public Declare Function GetActiveWindow Lib "user32" () As Long
Public keyMsg As KEYMSGS
Public lHook(1) As Long
'键盘钩子
Public Function CallKeyHookProc(ByVal code As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim lKey As Long
Dim strKeyName As String * 255
Dim strLen As Long
If code = HC_ACTION Then
CopyMemory keyMsg, lParam, LenB(keyMsg)
Select Case wParam
Case WM_SYSKEYDOWN, WM_KEYDOWN, WM_SYSKEYUP, WM_KEYUP:
lKey = keyMsg.sKey And &HFF '扫描码
lKey = lKey * 65536
strLen = GetKeyNameText(lKey, strKeyName, 250)
If (keyMsg.vKey And &HFF) = 162 And zhixing = True Then '把左Ctrl键屏蔽
CallKeyHookProc = 1 '屏蔽按键
End If
If (keyMsg.vKey And &HFF) = 163 And zhixing = True Then '把右Ctrl键屏蔽
CallKeyHookProc = 1 '屏蔽按键
End If
If (keyMsg.vKey And &HFF) = 160 And zhixing = True Then '把左shift键屏蔽
CallKeyHookProc = 1 '屏蔽按键
End If
If (keyMsg.vKey And &HFF) = 161 And zhixing = True Then '把右shift键屏蔽
CallKeyHookProc = 1 '屏蔽按键
End If
If (keyMsg.vKey And &HFF) = 164 And zhixing = True Then '把左alt键屏蔽
CallKeyHookProc = 1 '屏蔽按键
End If
If (keyMsg.vKey And &HFF) = 165 And zhixing = True Then '把右alt键屏蔽
CallKeyHookProc = 1 '屏蔽按键
End If
End Select
End If
If code <> 0 Then
CallKeyHookProc = CallNextHookEx(0, code, wParam, lParam)
End If
End Function
窗口代码:
Option Explicit
Private Sub AddHook()
'键盘钩子
lHook(0) = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf CallKeyHookProc, App.hInstance, 0)
End Sub
'卸钩子
Private Sub DelHook()
UnhookWindowsHookEx lHook(0)
End Sub
Private Sub Command1_Click()
zhixing = True
End Sub
Private Sub Command2_Click()
zhixing = False
End Sub
Private Sub Form_Load()
Call Command1_Click
AddHook
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call Command2_Click
UnhookWindowsHookEx lHook(0)
End Sub
2个按钮的Visible为false
Option Explicit
Public zhixing As Boolean
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 Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, ByVal lpvSource As Long, ByVal cbCopy As Long)
Public Type KEYMSGS
vKey As Long '虚拟码 (and &HFF)
sKey As Long '扫描码
flag As Long '键按下:128 抬起:0
time As Long 'Window运行时间
End Type
Public Const WH_KEYBOARD_LL = 13
'-----------------------------------------
'消息
Public Const HC_ACTION = 0
'键盘消息
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const WM_SYSKEYDOWN = &H104
Public Const WM_SYSKEYUP = &H105
Public Declare Function GetKeyNameText Lib "user32" Alias "GetKeyNameTextA" (ByVal lParam As Long, ByVal lpBuffer As String, ByVal nSize As Long) As Long
Public strKeyName As String * 255
Public Declare Function GetActiveWindow Lib "user32" () As Long
Public keyMsg As KEYMSGS
Public lHook(1) As Long
'键盘钩子
Public Function CallKeyHookProc(ByVal code As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim lKey As Long
Dim strKeyName As String * 255
Dim strLen As Long
If code = HC_ACTION Then
CopyMemory keyMsg, lParam, LenB(keyMsg)
Select Case wParam
Case WM_SYSKEYDOWN, WM_KEYDOWN, WM_SYSKEYUP, WM_KEYUP:
lKey = keyMsg.sKey And &HFF '扫描码
lKey = lKey * 65536
strLen = GetKeyNameText(lKey, strKeyName, 250)
If (keyMsg.vKey And &HFF) = 162 And zhixing = True Then '把左Ctrl键屏蔽
CallKeyHookProc = 1 '屏蔽按键
End If
If (keyMsg.vKey And &HFF) = 163 And zhixing = True Then '把右Ctrl键屏蔽
CallKeyHookProc = 1 '屏蔽按键
End If
If (keyMsg.vKey And &HFF) = 160 And zhixing = True Then '把左shift键屏蔽
CallKeyHookProc = 1 '屏蔽按键
End If
If (keyMsg.vKey And &HFF) = 161 And zhixing = True Then '把右shift键屏蔽
CallKeyHookProc = 1 '屏蔽按键
End If
If (keyMsg.vKey And &HFF) = 164 And zhixing = True Then '把左alt键屏蔽
CallKeyHookProc = 1 '屏蔽按键
End If
If (keyMsg.vKey And &HFF) = 165 And zhixing = True Then '把右alt键屏蔽
CallKeyHookProc = 1 '屏蔽按键
End If
End Select
End If
If code <> 0 Then
CallKeyHookProc = CallNextHookEx(0, code, wParam, lParam)
End If
End Function
窗口代码:
Option Explicit
Private Sub AddHook()
'键盘钩子
lHook(0) = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf CallKeyHookProc, App.hInstance, 0)
End Sub
'卸钩子
Private Sub DelHook()
UnhookWindowsHookEx lHook(0)
End Sub
Private Sub Command1_Click()
zhixing = True
End Sub
Private Sub Command2_Click()
zhixing = False
End Sub
Private Sub Form_Load()
Call Command1_Click
AddHook
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call Command2_Click
UnhookWindowsHookEx lHook(0)
End Sub
2个按钮的Visible为false
展开全部
将窗体的KeyPreview属性设置为True
在窗体的KeyPress事件(即Form_KeyPress事件过程)中加入语句:
If KeyAscii = 48 Then KeyAscii = 0
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
貌似这是不被允许的,屏蔽键盘的按钮得对系统进行操作,貌似VB没有这种功能。
追问
貌似是可以的,以前好像见过
追答
那坐等高手来解答
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用个键盘设置软件,弄成别的就成了
追问
我要用vb实现。。。。
追答
在百度里查“vb屏蔽 ctrl键” 或者上bbs.csdn.net 上查,都有相关的内容,你可以看看。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-05-13
展开全部
用螺丝刀 扣下去
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询