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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.