| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1314 人关注过本帖
标题:[求助]钩子截获消息后
只看楼主 加入收藏
winnip1
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-4-16
收藏
 问题点数:0 回复次数:6 
[求助]钩子截获消息后
事情是这样的。在我截获按键消息后,也就是 win(窗口)键,我想给屏蔽掉。
不知道有没有办法。
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 CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Public Hook1 As Long
Public Const WH_KEYBOARD = 2
Public Const VK_LWIN = &H5B
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Function MyHook(ByVal ncode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If ncode >= 0 Then
MyHook = 0
If wParam = &H5B <> 0 Then
If (lParam And &HC000000) = 0 Then '截获按下窗口键。。。
MyHook = 1
End If
End If
End If
Call CallNextHookEx(Hook1, ncode, wParam, lParam)
End Function
请指点。谢谢。
搜索更多相关主题的帖子: Long ByVal 钩子 Public 截获 
2007-04-16 03:05
winnip1
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-4-16
收藏
得分:0 
大家就帮帮我这个新来的吧。,。。。。。
我熬了好几个夜晚了。。。哭.....
2007-04-16 09:21
winnip1
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-4-16
收藏
得分:0 
2007-04-16 09:46
Kendy123456
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:62
帖 子:2720
专家分:0
注 册:2007-1-3
收藏
得分:0 
似乎这里有个对hook很熟的 不过好久不来了

2007-04-16 17:22
huangwy
Rank: 1
等 级:新手上路
威 望:2
帖 子:297
专家分:0
注 册:2007-3-23
收藏
得分:0 

你不要往下传就行了

2007-04-16 17:25
Joforn
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1242
专家分:122
注 册:2007-1-2
收藏
得分:0 
我试过钩住按键消息,但是好像系统在传消息给VB窗体的同时会自己传递消息给系统,所以就是不执行消息也会自动打开开始菜单。

VB QQ群:47715789
2007-04-16 22:50
cuiwei12
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-3-6
收藏
得分:0 

一个form,一个模块,form上有四个文本框(txtMsg(0),txtMsg(1),txtHwnd(0),txtHwnd(1)),一个按钮(AddHook)。

'模块
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 GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
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 Type MOUSEMSGS
X As Long 'x座标
Y As Long 'y座标
a As Long
b As Long
time As Long 'Window运行时间
End Type
Public Type POINTAPI
X As Long
Y As Long
End Type
Public Const WH_KEYBOARD_LL = 13
Public Const WH_MOUSE_LL = 14
Public Const Alt_Down = &H20
'-----------------------------------------
'消息
Public Const HC_ACTION = 0
Public Const HC_SYSMODALOFF = 5
Public Const HC_SYSMODALON = 4
'键盘消息
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const WM_SYSKEYDOWN = &H104
Public Const WM_SYSKEYUP = &H105
'鼠标消息
Public Const WM_MOUSEMOVE = &H200
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_RBUTTONUP = &H205
Public Const WM_RBUTTONDBLCLK = &H206
Public Const WM_MBUTTONDOWN = &H207
Public Const WM_MBUTTONUP = &H208
Public Const WM_MBUTTONDBLCLK = &H209
Public Const WM_MOUSEACTIVATE = &H21
Public Const WM_MOUSEFIRST = &H200
Public Const WM_MOUSELAST = &H209
Public Const WM_MOUSEWHEEL = &H20A
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 MouseMsg As MOUSEMSGS
Public lHook(1) As Long
'----------------------------------------
'模拟鼠标
Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4
Private Const MOUSEEVENTF_ABSOLUTE = &H8000 ' absolute move
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As

Long, ByVal dwExtraInfo As Long)
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
'--------------------------------------
'模拟按键
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal

dwExtraInfo As Long)
'鼠标钩子
Public Function CallMouseHookProc(ByVal code As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim pt As POINTAPI

If code = HC_ACTION Then
CopyMemory MouseMsg, lParam, LenB(MouseMsg)

Form1.txtMsg(1).Text = "X=" + Str(MouseMsg.X) + " Y=" + Str(MouseMsg.Y)
Form1.txtHwnd(1) = Format(wParam, "0")

If wParam = WM_MBUTTONDOWN Then '把中键改为左键
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
CallMouseHookProc = 1
End If

If wParam = WM_MBUTTONUP Then
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
CallMouseHookProc = 1
End If

End If

If code <> 0 Then
CallMouseHookProc = CallNextHookEx(0, code, wParam, lParam)
End If

End Function
'键盘钩子
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)
Form1.txtMsg(0).Text = "键名:" + Left(strKeyName, strLen) + " 虚拟码:" + Format(keyMsg.vKey And &HFF, "0") + "

扫描码:" + Format(lKey / 65536, "0")

Form1.txtHwnd(0) = ""
If (GetKeyState(vbKeyControl) And &H8000) Then
Form1.txtHwnd(0) = Form1.txtHwnd(0) + "Ctrl "
End If

If (keyMsg.flag And Alt_Down) <> 0 Then
Form1.txtHwnd(0) = Form1.txtHwnd(0) + "Alt "
End If

If (GetKeyState(vbKeyShift) And &H8000) Then
Form1.txtHwnd(0) = Form1.txtHwnd(0) + "Shift"
End If

'keyMsg.vKey And &HFF 虚拟码
'lKey / 65536 扫描码

If (keyMsg.vKey And &HFF) = vbKeyY Then '把Y键替换为N
If wParam = WM_SYSKEYDOWN Or wParam = WM_KEYDOWN Then
keybd_event vbKeyN, 0, 0, 0
End If
CallKeyHookProc = 1 '屏蔽按键
End If
If (keyMsg.vKey And &HFF) = 91 Then '屏蔽左开始菜单,92是右开始菜单
If wParam = WM_SYSKEYDOWN Or wParam = WM_KEYDOWN Then
keybd_event vbKeyN, 0, 0, 0
End If
CallKeyHookProc = 1
End If
End Select
End If

If code <> 0 Then
CallKeyHookProc = CallNextHookEx(0, code, wParam, lParam)
End If
End Function

'窗体
Private Sub AddHook_Click()
'键盘钩子
lHook(0) = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf CallKeyHookProc, App.hInstance, 0)
'鼠标钩子
lHook(1) = SetWindowsHookEx(WH_MOUSE_LL, AddressOf CallMouseHookProc, App.hInstance, 0)
End Sub

Private Sub Form_Unload(Cancel As Integer)
UnhookWindowsHookEx lHook(0)
UnhookWindowsHookEx lHook(1)
End Sub

VB6+XP测试通过。

2007-05-05 14:40
快速回复:[求助]钩子截获消息后
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026394 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved