Monitor the keyboard and output special words to the clipboard after getting a specific button

zhaozj2021-02-12  188

Monitor the keyboard and output special words to the clipboard after getting a specific button

When typing, enter a special character is always so annoying, like α, β, γ, μ, π, φ, Δ, ° C, ° F, £. In order to simplify the input of these characters, this program is designed. However, here is just capturing "a", and then send "α" characters to the clipboard. In order to make the program more practical, please join the CTR, Alt, the Shift button to Toggled code, I will not write all. Design ideas, design the background thread monitoring keyboard, see which buttons are pressed. When discovers "a" press (in fact, if it is to be practical, it should be a determination CTR Shit A to avoid conflict), send "α" characters to the clipboard, then use CTR V to be posted to you want to output The place is there. Specific implementation, create a form, add two buttons, one for starting hook, one for unhook. Here is a log hook so that it itself is a background thread, whether or not the form is Active, it can run. In the hook, use an EventMSG to get the keyboard button, the low position of the paraml in the message is the ASCII code of the button, get this, you can open the clipboard, empty, set the format, set the clipboard content. This is done. In order to write this code, I saw Hubdog's "Delphi's uncontrolled sunflower book" (Version 2.5) All articles about hooks and clipboards, searching for 9CBS "programmers" Delphi section of articles and belt source code Controls or programs. I spent more than a dozen hours before I wrote it. The result is actually a dozen lines of code. Hey! Programming this thing, not, but don't know! If the master is trying to capture the active window, use the WM_CopyData or IPC mechanism to output the characters directly to where you want to output, it is perfect. I am tired, temporarily use the clipboard!

Unit unit1;

Interface

Uses Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls, Clipbrd;

type TForm1 = class (TForm) Button1: TButton; Button2: TButton; procedure FormCreate (Sender: TObject); procedure Button1Click (Sender: TObject); procedure Button2Click (Sender: TObject); private {Private declarations} public {Public declarations} end ;

Varform1: TForm1; HHOOK: Integer; Eventarr: EventMsg;

IMPLEMENTATION

Function hookproc (icode: integer; wparam: wparam; lparam: lparam): LRESULT; stdcall; var c: integer

BeginResult: = 0; if iF iCode <0 Then Result: = CallNexthooKex (HHOOK, ICODE, WPARAM, LPARAM); Eventarr: = peventmsg (lparam) ^; // Let Eventarr get keyboard messages if Eventarr = = HC_action life: // Hook to something if Eventarr.Message = wm_keydown the begin // Whether the button is pressed C: = LO (Eventarr.Paraml); // Paraml low is the ASCII code IF of the pressed button C = 65 THEN Begin

Clipboard.open; // Open the clipboard, empty, set format, set the clipboard content clipboard.clear; clipboard.formats [cf_text]; Clipboard.SetTextBuf (PCHARD.CLOSE; END;

END;

END;

{$ R * .dfm}

Procedure TFORM1.FormCreate (Sender: TOBJECT); begin button1.caption: = 'hook'; button2.caption: = 'unhook'; button2.enabled: = false;

END;

procedure TForm1.Button1Click (Sender: TObject); begin file: // build the keyboard hook message chain hHook: = SetwindowsHookEx (WH_JOURNALRECORD, HookProc, HInstance, 0); Button2.Enabled: = True; Button1.Enabled: = False;

END;

Procedure tForm1.Button2Click (Sender: TOBJECT); Begin UnHookWindowsHookex (hHOOK); // Unloading hHOOK: = 0; button1.enabled: = true; button2.enabled: = false;

END;

End.

转载请注明原文地址:https://www.9cbs.com/read-6121.html

New Post(0)