Saturday, August 22, 2020

Intercepting Keyboard Input With Delphi

Blocking Keyboard Input With Delphi Consider for a second making of some quick arcade game. All the illustrations are shown, lets state, in a TPainBox. TPaintBox can't get the info center - no occasions are terminated when the client presses a key; we can't catch cursor keys to move our war vessel. Delphi help! Catch Keyboard Input Most Delphi applications ordinarily handle client contribution through explicit occasion handlers, those that empower us to catch client keystrokes and procedure mouse development. We realize that center is the capacity to get client contribution through the mouse or console. Just the article that has the center can get a console occasion. A few controls, for example, TImage, TPaintBox, TPanel, and TLabel can't get center. The basic role of most realistic controls is to show content or designs. In the event that we need to catch console contribution for controls that can't get the information concentrate well need to manage Windows API, snares, callbacks and messages. Windows Hooks Actually, a snare work is a callback work that can be embedded in the Windows message framework so an application can get to the message stream before other handling of the message happens. Among numerous kinds of windows snares, a console snare is called at whatever point the application calls the GetMessage() or PeekMessage() capacity and there is a WM_KEYUP or WM_KEYDOWN console message to process. To make a console snare that blocks all console input coordinated to a given string, we have to call SetWindowsHookEx API work. The schedules that get the console occasions are application-characterized callback capacities called snare capacities (KeyboardHookProc). Windows calls your snare work for every keystroke message (key up and key down) before the message is put in the applications message line. The snare capacity can process, change or dispose of keystrokes. Snares can be neighborhood or worldwide. The arrival estimation of SetWindowsHookEx is a handle to the snare just introduced. Before ending, an application must call the UnhookWindowsHookEx capacity to free framework assets related with the snare. Console Hook Example As an exhibit of console snares, well make a task with graphical control that can get key presses. TImage is gotten from TGraphicControl, it very well may be utilized as a drawing surface for our theoretical fight game. Since TImage can't get console presses through standard console occasions well make a snare work that blocks all console input coordinated to our drawing surface. TImage Processing Keyboard Events Start new Delphi Project and spot one Image segment on a structure. Set Image1.Align property to alClient. That is it for the visual part, presently we need to do some coding. Initially, well need some worldwide factors: var   Form1: TForm1;   KBHook: HHook; {this catches console input}  â cx, cy : whole number; {track fight ships position}  â {callbacks declaration}  â function KeyboardHookProc(Code: Integer; WordParam: Word; LongParam: LongInt): LongInt; stdcall; usage ... To introduce a snare, we call SetWindowsHookEx in the OnCreate case of a structure. strategy TForm1.FormCreate(Sender: TObject) ; start  {Set the console snare so we  can catch console input}  KBHook:SetWindowsHookEx(WH_KEYBOARD,  â â â â â â â â â â {callback } KeyboardHookProc,                           HInstance,                           GetCurrentThreadId()) ;  {place the fight transport in  the center of the screen}  cx : Image1.ClientWidth div 2;  cy : Image1.ClientHeight div 2;  Image1.Canvas.PenPos : Point(cx,cy) ; end; To free framework assets related with the snare, we should call the UnhookWindowsHookEx work in the OnDestroy occasion: strategy TForm1.FormDestroy(Sender: TObject) ; start  â {unhook the console interception}   UnHookWindowsHookEx(KBHook) ; end; The most significant piece of this venture is the KeyboardHookProc callback technique used to process keystrokes. work KeyboardHookProc(Code: Integer; WordParam: Word; LongParam: LongInt) : LongInt; start  case WordParam of   vk_Space: {erase fight ships path}  â â begin  â â â with Form1.Image1.Canvas do  â â â begin      Brush.Color : clWhite;      Brush.Style : bsSolid;      Fillrect(Form1.Image1.ClientRect) ;  â â â end;  â â end;   vk_Right: cx : cx1;   vk_Left: cx : cx-1;   vk_Up: cy : cy-1;   vk_Down: cy : cy1;  end; {case}  If cx 2 then cx : Form1.Image1.ClientWidth-2;  If cx Form1.Image1.ClientWidth - 2 then cx : 2;  If cy 2 then cy : Form1.Image1.ClientHeight - 2 ;  If cy Form1.Image1.ClientHeight-2 then cy : 2;  with Form1.Image1.Canvas do  begin   Pen.Color : clRed;   Brush.Color : clYellow;   TextOut(0,0,Format(%d, %d,[cx,cy])) ;   Rectangle(cx-2, cy-2, cx2,cy2) ;  end;  Result:0; {To keep Windows from passing the keystrokes  to the objective window, the Result esteem must  be a nonzero value.} end; That is it. We currently have a definitive console handling code. Note only a certain something: this code is not the slightest bit confined to be utilized distinctly with TImage. The KeyboardHookProc work fills in as a general KeyPreview KeyProcess instrument.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.