I read "deep into windows core programming in Delphi" and thought it was okay to write something. At least I learned something.
So I sorted outCode, And add a note to send it.
The first is the DLL used by the most important keyboard HOOK:
Unit Unitdll; Interface Uses Windows; Const Buffer_size = 16 * 1024 ;// Size of the file mapped to the memory Const Hook_mem_filename = ' Mem_file ' ;// Image File Name Const Hook_mutex_name = ' Mutex_name ' ;// Mutex name Type // Shared structure tshared = Record Keys: Array [ 0 .. Buffer_size] Of Char; keycount: integer; End ; // Share Structure pointer pshared = ^ Tshared; VaR Memfile, hookmutex: thandle; // File handle and mutex handle holdkeyhook: hhook; // Hook variable shared: pshared; // Shared variable Implementation // Important: keyboard hook callback Function Keyhookproc (icode: integer; wparam: wparam; lparam: lparam): lresult; Stdcall ; Export ; Const Keypressmask = $ 80000000 ; Begin If Icode < 0 Then Result: = Callnexthookex (holdkeyhook, icode, wparam, lparam) Else Begin If (Lparam And Keypressmask) = 0 ) Then Begin // Keyboard Message capture shared ^. Keys [Shared ^. keycount]: = Char (wparam And $ 00FF); Inc (shared ^. keycount ); // Reset if the memory size exceeds the limit If Shared ^. keycount> = buffer_size- 1 Then Shared ^. keycount: = 0 ; End ; Result: = 0 ; End ; End ; // Installation hook Function Enablekeyhook: bool; Export ; Begin Shared ^. keycount: = 0 ; If Holdkeyhook = 0 Then Begin // Set hook Filter { Wh_keyboard: installed with the keyboard hook keyhookproc: Message callback, hinstance: callback function instance thread ID } Holdkeyhook: = Setwindowshookex (wh_keyboard, keyhookproc, hinstance, 0 ); End ; Result: = (Holdkeyhook <> 0 ); End ; { Undo hook Filter Function } Function Disablekeyhook: bool; Export ; Begin If Holdkeyhook <> 0 Then Begin Unhookwindowshookex (holdkeyhook); holdkeyhook: = 0 ; Shared ^. keycount: = 0 ; End ; Result: = (Holdkeyhook = 0 ); End ; // How many buttons are obtained? Function Getkeycount: integer; Export ; Begin Result: = Shared ^. keycount; End ; // Obtain the I button Function Getkey (Index: integer): Char; Export ; Begin Result: = Shared ^. Keys [Index]; End ; // Clear button Procedure Clearkeystring; Export ; Begin Shared ^. keycount: = 0 ; End ; // Export function list Exports Enablekeyhook, disablekeyhook, getkeycount, clearkeystring, getkey; Initialization // Create a mutex variable. Only one DLL process can use hookmutex: = Createmutex ( Nil , True, hook_mutex_name ); // Open the file image memfile: = Openfilemapping (file_map_write, false, hook_mem_filename ); // If this file image does not exist, it is created. If Memfile = 0 Then Memfile: = Createfilemapping ($ ffffffff, Nil , Page_readwrite, 0 , Sizeof (tshared), hook_mem_filename ); // File ing memory shared: = Mapviewoffile (memfile, file_map_write, 0 , 0 , 0 ); // Release the mutex variable releasemutex (hookmutex ); // Turn off the mutex handle closehandle (hookmutex ); Finalization // Remove hook Filter If Holdkeyhook <>0 Then Disablekeyhook; // Release the ing unmapviewoffile (shared ); // Disable the image file closehandle (memfile ); End .
After reading this, you can directly write a client to call it.
Unit Unit2; Interface Uses Windows, messages, sysutils, classes, graphics, controls, forms, dialogs, stdctrls, extctrls; Type Tform1 = Class (Tform) memo1: tmemo; bsethook: tbutton; bcancelhook: tbutton; breadkeys: tbutton; bclearkeys: tbutton; panel2: tpanel; Procedure Bsethookclick (Sender: tobject ); Procedure Bcancelhookclick (Sender: tobject ); Procedure Breadkeysclick (Sender: tobject ); Procedure Bclearkeysclick (Sender: tobject ); End ; VaR Form1: tform1; Implementation { $ R *. DFM } Function Enablekeyhook: bool; External ' Keyhook. dll ' ; Function Disablekeyhook: bool; External ' Keyhook. dll ' ; Function Getkeycount: integer; External ' Keyhook. dll ' ; Function Getkey (idx: integer): Char; External ' Keyhook. dll ' ; Procedure Clearkeystring; External ' Keyhook. dll ' ; Procedure Tform1.bsethookclick (Sender: tobject ); Begin Enablekeyhook; bsethook. Enabled: = False; bcancelhook. Enabled: = True; breadkeys. Enabled: = True; bclearkeys. Enabled: = True; panel2.caption: =' The keyboard Hook has been set. ' ; End ; Procedure Tform1.bcancelhookclick (Sender: tobject ); Begin Disablekeyhook; bsethook. Enabled: = True; bcancelhook. Enabled: = False; breadkeys. Enabled: = False; bclearkeys. Enabled: = False; panel2.caption: = ' Keyboard hook not set ' ; End ; Procedure Tform1.breadkeysclick (Sender: tobject ); VaR I: integer; Begin Memo1.lines. Clear; { Show the hit key history in memo1 } For I: =0 To Getkeycount- 1 Do Memo1.text: = Memo1.text + Getkey (I ); End ; Procedure Tform1.bclearkeysclick (Sender: tobject ); Begin Memo1.clear; clearkeystring; End ; End .