Remote control: Use Delphi to simulate keyboard input/mouse click

Source: Internet
Author: User
Tags ord

Analog keyboard We use Keybd_event This API function, simulates the mouse button with the Mouse_event function.

The keybd_event function can trigger a keystroke event that generates a WM_KEYDOWN or wm_keyup message, typically using these two messages to simulate a

The next key (the process of pressing the button is: press, then bounce), but not directly with this function is convenient.
Keybd_event A total of four parameters:
The first is the key value of the virtual key, such as the ENTER key for Vk_return, Tab is vk_tab;
The second parameter is a scan code, generally do not set, with 0 instead of the line;
The third parameter is the option flag, if the KeyDown is 0, if the KEYUP is set to "Keyeventf_keyup";
The fourth parameter is usually set to 0.

For example, the following code can be implemented to simulate pressing the I key, where the $49 represents the I key virtual key value:

keybd_event ($49,0,0,0);

keybd_event ($49,0,keyeventf_keyup,0);

  

Mouse_event is best used with Setcursorpos (x, y) functions, positioning the mouse first, and then generating mouse events.
The mouse_event has five parameters:
The first is the option flag, for Mouseeventf_leftdown when the left key is pressed, the left key for the Mouseeventf_leftup to release, send the appropriate message to the system;
The 23rd parameter represents the x, Y relative position, which can be set to 0, 0, respectively;
The 45th parameter is not important, it can also be set to 0, 0.

Example code for Mouse_event:

Setcursorpos (20,132);

Mouse_event (mouseeventf_leftdown,0,0,0,0);

Mouse_event (mouseeventf_leftup,0,0,0,0);

Mouse_event (mouseeventf_leftdown,0,0,0,0);

Mouse_event (mouseeventf_leftup,0,0,0,0); ...

The above code indicates a double-click of the mouse, to indicate that you clicked, with two mouse_event (one drop, one release at a time).

Attention
Whether it is analog keyboard or mouse events, you should pay attention to restore, that is, press the key to release, a KeyDown corresponding to a keyup; Click the mouse to release, or it may affect the function of the program.

Example 1: Simulate pressing the ' A ' key
keybd_event (65,0,0,0);
keybd_event (65,0,keyeventf_keyup,0);

Example 2: Analog Press the ' ALT+F4 ' key
keybd_event (18,0,0,0);
keybd_event (115,0,0,0);
keybd_event (115,0,keyeventf_keyup,0);
keybd_event (18,0,keyeventf_keyup,0);

SendMessage Analog Mouse click ==============================================================

2008-03-15 10:06

Simulate mouse clicks
SendMessage (H, Wm_lbuttondown, 1, 1);
SendMessage (H, Wm_lbuttonup, 1, 1);
Click is a press and a lift up the composition!
The message parameters for the mouse are:
SendMessage (H, wm_lbuttondown, x-coordinate, y-coordinate);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////

Var
X,y:integer;
Begin
X:=form1. Left+webbrowser1. left+570;
Y:=form1. Top+webbrowser1. top+210;
Setcursorpos (x, y);
Mouse_event (mouseeventf_leftdown,x,y,0,0);
Mouse_event (mouseeventf_leftup,x,y,0,0);
End

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////

Procedure Tform1.button2mousedown (Sender:tobject; Button:tmousebutton;
Shift:tshiftstate; X, Y:integer);
Begin
PostMessage (button2.handle,wm_lbuttondown,0, (y shl) +x);
End

Procedure Tform1.button2mouseup (Sender:tobject; Button:tmousebutton;
Shift:tshiftstate; X, Y:integer);
Begin
PostMessage (button2.handle,wm_lbuttonup,0, (y shl) +x);
End

◇[delphi] Send virtual key value Ctrl V
Procedure Sendpaste;
Begin
Keybd_event (Vk_control, Mapvirtualkey (Vk_control, 0), 0, 0);
keybd_event (Ord (' V '), Mapvirtualkey (Ord (' V '), 0), 0, 0);
keybd_event (Ord (' V '), Mapvirtualkey (Ord (' V '), 0), KEYEVENTF_KEYUP, 0);
Keybd_event (Vk_control, Mapvirtualkey (Vk_control, 0), KEYEVENTF_KEYUP, 0);
End

Remote control: Use Delphi to simulate keyboard input/mouse click

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.