vb 如何 屏蔽 键盘指定按键 10

我想在某个情况下屏蔽ctrl键,使ctrl无效怎么实现?... 我想在某个情况下屏蔽 ctrl键, 使ctrl无效 怎么实现? 展开
 我来答
百度网友4c113e1
2014-02-27
知道答主
回答量:36
采纳率:0%
帮助的人:20万
展开全部
模块代码:

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
笃侠6A
推荐于2017-09-29 · TA获得超过3734个赞
知道大有可为答主
回答量:3205
采纳率:75%
帮助的人:3253万
展开全部
  1. 将窗体的KeyPreview属性设置为True

  2. 在窗体的KeyPress事件(即Form_KeyPress事件过程)中加入语句:

    If KeyAscii = 48 Then KeyAscii = 0


已赞过 已踩过<
你对这个回答的评价是?
评论 收起
hai3zei3wang3
2013-05-11 · TA获得超过173个赞
知道小有建树答主
回答量:168
采纳率:0%
帮助的人:92.4万
展开全部
貌似这是不被允许的,屏蔽键盘的按钮得对系统进行操作,貌似VB没有这种功能。
追问
貌似是可以的,以前好像见过
追答
那坐等高手来解答
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
传道者
2013-05-13 · TA获得超过195个赞
知道小有建树答主
回答量:616
采纳率:0%
帮助的人:266万
展开全部
用个键盘设置软件,弄成别的就成了
追问
我要用vb实现。。。。
追答
在百度里查“vb屏蔽 ctrl键” 或者上bbs.csdn.net 上查,都有相关的内容,你可以看看。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-05-13
展开全部
用螺丝刀 扣下去
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式