This program can use a hotkey to move the mouse to a specified coordinate. Is a sample program that defines a hotkey.
The hotkey of this program is 5 of the keypad, and the coordinate can be specified in the edit box.
Unit mainunit;
Interface
Uses Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls, Mask
type TForm1 = class (TForm) btnSetHK: TButton; btnExit: TButton; GroupBox2: TGroupBox; Label3: TLabel; Label4: TLabel; btnUnsetHK: TButton; edYPos: TMaskEdit; edXPos: TMaskEdit; Memo: TMemo; procedure btnExitClick (Sender: TObject) ; procedure btnSetHKClick (Sender: TObject); procedure btnUnsetHKClick (Sender: TObject); procedure OnHotKey (var Message: TWMHOTKEY); message WM_HOTKEY; procedure FormDestroy (Sender: TObject); public {public declarations} end;
Var Form1: TFORM1;
Const IDHOTKEY: WORD = 0;
IMPLEMENTATION
{$ R * .dfm}
Procedure TFORM1.BTNEXITCLICK (Sender: TOBJECT); begin close;
procedure TForm1.btnSetHKClick (Sender: TObject); begin if idHotKey <> 0 then Exit; idHotKey: = GlobalAddAtom ( 'EmuMouse'); // hotkey to obtain a unique identifier RegisterHotKey (Handle, idHotKey, 0, VK_NUMPAD5); // Register the hotkey END;
Procedure TFORM1.ONHOTKEY (VAR MESSAGE: TWMHOTKEY); VAR POINT: TPOINT; X, Y: Word; Begin getCursorpos (POINT); // Remarks Current coordinates
TRY X: = STRTOINT (EDXPOS.TEXT); Y: = STRTOINT (EDYPOS.Text); Except ShowMessage ('Coordinate input is incorrect.'); exit;
Try mouse_event (MouseEventf_absolute MouseEventf_leftdown, Point.x, Point.y, 0, GetMessageExtrainfo);
SetCursorpos (X, Y);
Mouse_event (MouseEventf_absolute MouseEventf_leftup, x, y, 0, getMessageExtrainfo);
Except ShowMessage ('Error');
END;
procedure TForm1.btnUnsetHKClick (Sender: TObject); begin if idHotKey = 0 then Exit; UnRegisterHotKey (Handle, idHotKey); // cancellation hotkey DeleteAtom (idHotKey); // cancellation identifier idHotKey: = 0; end; procedure TForm1.FormDestroy (Sender: TOBJECT); begin btnunsethk.click;
End.
This is a DFM file
object Form1: TForm1 Left = 296 Top = 238 AutoSize = True BorderStyle = bsDialog BorderWidth = 8 Caption = 'simulated mouse drag' ClientHeight = 265 ClientWidth = 211 Color = clBtnFace Font.Charset = GB2312_CHARSET Font.Color = clWindowText Font.Height = -12 Font.Name = 'Times new Roman' Font.Style = [] OldCreateOrder = False OnDestroy = FormDestroy PixelsPerInch = 96 TextHeight = 12 object btnSetHK: TButton Left = 136 Top = 8 Width = 75 Height = 25 Caption = 'hotkey (& H) 'TabOrder = 0 OnClick = btnSetHKClick end object btnExit: TButton Left = 136 Top = 72 Width = 75 Height = 25 Caption =' exit (& X) 'TabOrder = 2 OnClick = btnExitClick end object GroupBox2: TGroupBox Left = 0 Top = 0 width = 129 height = 97 caption = 'destination coordinate' Taborder = 3 Object label3: TLABEL LEFT = 16 TOP = 29 width = 6 height = 12 caption = 'x' end Object label4: TLabel Left = 16 TOP = 61 Width = 6 height = 12 Caption = 'Y' end object edXPos: TMaskEdit Left = 32 Top = 24 Width = 73 Height = 20 EditMask = '0000; 1; _' MaxLength = 4 TabOrder = 0 Text = '0000' end object edYPos: TMaskEdit Left = 32 TOP = 56 width = 73 height = 20 EditMask = '0000; 1; _' maxlength = 4 Taborder = 1 text = '0000'