首页 >> 读书频道 >> 电脑 >> KeyBoard Key in的习惯分析(JournalRecord Hook)
 
· 更动MsgBox But
· Hook简介
· 如何得知Mouse已离开
· 网络数据库教程六
· TextBox模拟拖曳选
· 网络数据库教程五
· 如何在开始功能表的程式集
· 网络数据库教程四
· 设定Caret的大小与其
· 如何Restart Sh
· 于PictureBox/
· 如何用VB建立捷径(Sh
 
· 电视剧《刁蛮公主》下载和
· 美容养颜手册
· 加油金顺剧情分集介绍完整
· 红楼梦:120回全本[清
· 素质教育在美国
· 全唐诗卷四十六
· 韩剧《布拉格恋人》剧情介
· 夜航船[作者:明·张岱]
· 局外人[作者:韩·可爱淘
· 现场流行病学
· 源氏物语[日本:紫式部]
· 武林寓言故事
 
· (出租)中动商场部分及写
· (出租)中动动漫基地&#
· 喜剧学院
· 《善德女王》剧情介绍
· 魔女18号 剧情
· 丑女无敌剧情介绍
· 魔女幼熙剧情介绍
· 龙游天下剧情介绍
· 震撼世界的七日剧情介绍
· 静静的白桦林剧情介绍
· 心情日记—老公今天我想对
· 旗舰剧情介绍
欢迎来到月影社区!如果您觉得这里不错,请推荐给您的朋友们。月影社区:http://wf66.com/

KeyBoard Key in的习惯分析(JournalRecord Hook)


查看有无更新版本

关键字:VB 2006-9-18

 

如果说,我们想得知某些键被KeyIn的频率,或许会想,那设定Form KeyPreview不就可
以,是啊,不过这样做就变成您要使用者对着您的 Form做事才有,出了这个Form就没有
办法了,所以又要使用其他的方式了,第一个想到的是KeyBoard Hook,是的,这是一个
正确的想法。不过说真的,在VB中我试过了一段时间的Remote Hook for KeyBoard Hook
,如果搭配C或Delphi所作出的.DLL那应没有问题,但以vb本身要做出Remote Hook,似
乎只有使用ActiveX. Dll(因为Remote Hook大多只能搭配.dll来做,而VB5不能做如同
Function Call的.Dll,它只能做出ActiveX.Dll),但我testing 了好久,都没有成功
,而王国荣先生它说会安排在未来的Run PC中做介绍,那这问题就等他来介绍了。

可是除了Keyboard Hook之外尚有其他的Hook来做,那就是JournalRecord Hook,这个
Hook和JournalPlayBack Hook一样,是System Wide,但不用在.Dll之中,是个例外,
但它要求系统中只能有一个这种Hook。JournalRecord Hook是在硬体讯息(Mouse, Keyboaed等)
在System queue中被取出时所引发的Hook,所以 我们也可以拿来用上一用。

其Hook Procedure定义如下
Function JournalRecordProc (
    Byval   code    As Long ,   // hook code
    ByVal   wParam  As Long ,   // undefined
    ByVal   lParam  As Long     // address of message being processed
    ) As Long

code有以下的值
  HC_ACTION      The lParam parameter points to an EVENTMSG structure
                 containing information about a message removed from the
                 system queue. The hook procedure must record the contents of
                 the structure by copying them to a buffer or file.
  HC_SYSMODALOFF A system-modal dialog box has been destroyed. The hook
                 procedure must resume recording.
  HC_SYSMODALON  A system-modal dialog box is being displayed. Until the
                 dialog box is destroyed, the hook procedure must stop recording.

  注:HC_SYSMODALOFF, HC_SYSMODUALON只在16位元程式产生一个system-modal msgbox时
     才会收到。

wParam
  Specifies a NULL value.
lParam
  Points to an EVENTMSG structure that contains the message to be recorded.

而EventMsg的定义如下:
Type EVENTMSG
        message As Long
        paramL As Long
        paramH As Long
        time As Long
        hwnd As Long
End Type

message 若为键盘讯息可为WM_(SYS)KeyUp或WM_(SYSKeyDown),Mouse讯息则是
        WM_XButtonUp / WM_XButtonDown /WM_MOUSEMOVE等
paramL  若为键盘讯息,则是键盘的虚拟码,Mouse讯息则为Mouse相对Screen之x座标(Pixels)
paramH  若为键盘讯息,则是键盘的扫描码,Mouse讯息则为Mouse相对Screen之y座标(Pixels)
time    讯息发生的时间(tick time)

而我们可以用MapVirtualKey来转换虚拟码成UnShift的Ascii值,本来嘛,这种虚拟码或
扫描码本身就没有Shift+某个键 的控制,而按Shift+A ==>"a",那是TranslateMessage()
所做出来的事。所以我们可另外使用GetAsyncKeystate()来Check Shift有没有按下。
这个程式将重要的部份都完成,而记录KeyBoard 到底是Keyin了哪些键,是Keyup/KeyDown
有没有搭配Shift键等,这些可以另外使用资料库或档案来记录,这里便不再多说明。

'以下程式在.BASPublic Const WM_KEYDOWN = &H100Public Const WM_KEYUP = &H101Public Const HC_ACTION = 0Public Const WH_JOURNALRECORD = 0Type EVENTMSG message As Long paramL As Long paramH As Long time As Long hwnd As LongEnd TypeDeclare Function SetWindowsHookEx Lib "user32" Alias _ "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, _ ByVal hmod As Long, ByVal dwThreadId As Long) As LongDeclare Function UnhookWindowsHookEx Lib "user32" _ (ByVal hHook As Long) As LongDeclare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, _ ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As LongDeclare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" _ (lpvDest As Any, ByVal lpvSource As Long, ByVal cbCopy As Long)Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" _ (ByVal wCode As Long, ByVal wMapType As Long) As LongDeclare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As IntegerPublic hHook As Long ' handle of Hook ProcedurePublic msg As EVENTMSGSub EnableHook() hHook = SetWindowsHookEx(0, AddressOf HookProc, App.hInstance, 0)End SubSub FreeHook() Dim ret As Long ret = UnhookWindowsHookEx(hHook)End SubFunction HookProc(ByVal code As Long, ByVal wParam As Long, _ ByVal lParam As Long) As LongDim i As Long, j As IntegerIf code <> HC_ACTION Then HookProc = CallNextHookEx(hHook, code, wParam, lParam) Exit FunctionEnd IfCopyMemory msg, lParam, LenB(msg)If msg.message = WM_KEYUP Then j = GetAsyncKeyState(vbKeyShift) 'Check Shift是否正按下 i = MapVirtualKey(msg.paramL, 2) '转换成unshift Ascii Code If j <> 0 Then Debug.Print Chr(i And &HFF); " Key Up with Shift Down" Else Debug.Print Chr(i And &HFF); " Key Up without Shift Down" End IfEnd IfIf msg.message = WM_KEYDOWN Then j = GetAsyncKeyState(vbKeyShift) i = MapVirtualKey(msg.paramL, 2) If j <> 0 Then Debug.Print Chr(i And &HFF); " Key Down with Shift Down" Else Debug.Print Chr(i And &HFF); " Key Down without Shift Down" End IfEnd IfHookProc = CallNextHookEx(hHook, code, wParam, lParam)End Function'以下程式在FormPrivate Sub Form_Load()Call EnableHookEnd SubPrivate Sub Form_Unload(Cancel As Integer)Call FreeHookEnd Sub

KeyBoard Key in的习惯分析(JournalRecord Hook)

[ 1 ]
KeyBoard Key in的习惯分析(JournalRecord Hook) num

打印本页 关闭

关于我们版权声明本站导航友情连结作品演示 TOP↑