delphi 鍵盤鉤子的實現(100)

  • 主题发起人 主题发起人 abc_def
  • 开始时间 开始时间
A

abc_def

Unregistered / Unconfirmed
GUEST, unregistred user!
我寫了一個鍵盤鉤子﹐ 但有一個問題﹐ 在NOTEPAD中輸入時OK﹐ 但在DELPHI應用程序介面時輸入﹐ 截獲的卻是重復鍵?? 比如 ﹕ 123 變成 ﹕ 112233﹐ 請高手幫忙﹐以下是回調函數代碼﹕ library KeyBoardHookDll;uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, db, ADODB, StdCtrls;type TShared = record KeyHook:HHook; Moudle :THandle; ExeHandle : HWnd; end; PShared = ^TShared;const BUFFER_SIZE = 16 * 1024; HOOK_MEM_FILENAME = 'MEM_FILE';var myHook: Integer; str : string; icno : array[0..9] of char; i : integer; MemFile: THandle; Shared: PShared;function icno_return : string; stdcall; export;begin result := icno;end;Function HookKeyBoard(iCode:Integer; wParam:wParam; lParam:lParam): LRESULT; stdcall; export;const KeyPressMask = $80000000;var s : char; F : TextFile; cnt : integer; q : TADOQuery;begin If iCode >= 0 Then begin if ((lParam and KeyPressMask)= 0) then // 键按下 begin if ((wParam and $00ff)=VK_RETURN) and (icno <> '') then begin AssignFile(F, 'E:/PI/Program/USB_IMPORT/BARCODE.txt'); append(F); Writeln(F, icno); //wk_return = #13#10 CloseFile(F); icno := ''; i := 0; end else begin if ((wParam and $00ff) <> (VK_RETURN)) then begin s := Char(wParam and $00ff); if pos(s, '0123456789') > 0 then begin icno := s; inc(i); end; end; end; end; End; Result := CallNextHookEx(myHook, iCode, wParam, lParam);end;function InstallHook(Sender: HWnd): Boolean; stdcall; export;begin MemFile := OpenFileMapping(FILE_MAP_WRITE,False,HOOK_MEM_FILENAME); if MemFile = 0 then MemFile := CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(TShared),HOOK_MEM_FILENAME); Shared := MapViewOfFile(MemFile,File_MAP_WRITE,0,0,0); Shared^.Moudle := GetModuleHandle(PChar('KeyBoardHookDll')); Shared^.KeyHook := SetWindowsHookEx(WH_KEYBOARD, @HookKeyBoard, Shared^.Moudle, 0); Shared^.ExeHandle := Sender; if Shared^.KeyHook = 0 then begin Result := False; Exit; end; Result := true;end;function UnloadHook: Boolean; stdcall; export;begin Result := UnHookWindowsHookEx(Shared^.KeyHook); Shared^.KeyHook := 0; Freelibrary(Shared^.Moudle); UnmapViewOfFile(Shared); CloseHandle(memFile);end;procedure ado_test; stdcall; export;var q : TADOQuery; cnt : integer; temp : string;begin temp := '0016575183'; q := TADOQuery.create(nil); with q do begin try close; connectionstring := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:/PI/Program/USB_IMPORT/ICNOLIST.mdb;Persist Security Info=True'; sql.clear; sql.add('select * from ICNOSTD where icno=:icno'); parameters.parambyname('icno').value := string(temp); open; cnt := recordcount; close; if cnt > 0 then begin sql.clear; sql.add('insert into ICNOENTER(ICNO, FLAG, ENTERTIME)'); sql.add('values (:ICNO, :FLAG, :ENTERTIME)'); parameters.parambyname('ICNO').value := string(temp); parameters.parambyname('FLAG').value := 0; parameters.parambyname('ENTERTIME').value := now; execsql; showMessage('insert OK!'); end else begin beep; showMessage('Insert Error!'); end; finally free; end; end;end;{$R *.res}exports HookKeyBoard, InstallHook, UnloadHook, ado_test, icno_return;begin i := 0; icno := '';end.
 
后退
顶部