[Go] mouse and keyboard emulation API

Source: Internet
Author: User

In almost all games, the mouse is used to change the position and direction of the character, and the player uses only a small mouse
You can make your character swim in the world. So, how do we achieve a character that can walk without the player's participation? Actually realizing this is not
Difficult, just a few Windows API functions

Can be done, let's get to know these API functions first.

(1) Simulation Mouse Action API function mouse_event, it can achieve simulation mouse press and release and other actions.

VOID Mouse_event (
DWORD dwFlags,//mouse action ID.
DWORD DX,//mouse horizontal orientation position.
DWORD dy,//mouse vertical position.
The number of DWORD dwdata,//mouse wheel turns.
DWORD dwExtraInfo//An associated mouse action auxiliary information.
);

Among them, dwflags represents a variety of mouse actions and click Activity, its commonly used values are as follows:

Mouseeventf_move represents an analog mouse move event.

Mouseeventf_leftdown indicates that the left mouse button is pressed.

Mouseeventf_leftup indicates that the left mouse button is released.

The mouseeventf_rightdown represents the right mouse button for impersonation.

The mouseeventf_rightup represents the right mouse button for simulation release.

Mouseeventf_middledown indicates that the middle mouse button is pressed.

The mouseeventf_middleup indicates that the middle mouse button is released from the simulation.

(2), set and get the current mouse position of the API function. Get current mouse position using GetCursorPos ()
function to set the current mouse position using the

The Setcursorpos () function.

BOOL GetCursorPos (
Lppoint Lppoint//Returns the current position of the mouse.
);
BOOL Setcursorpos (
int X,//The horizontal position of the mouse.
The vertical position of the int Y//mouse.
);

Usually the game character's walk is moved through the mouse to the destination, then presses the mouse button to be done
The Below we use the API described above

function to simulate a character walking process.

CPoint Oldpoint,newpoint;
GetCursorPos (&oldpoint); Saves the current mouse position.
Newpoint.x = oldpoint.x+40;
Newpoint.y = oldpoint.y+10;
Setcursorpos (NEWPOINT.X,NEWPOINT.Y); Set the destination location.
Mouse_event (mouseeventf_rightdown,0,0,0,0);//Simulate pressing the right mouse button.
Mouse_event (mouseeventf_rightup,0,0,0,0);//Simulate release the right mouse button.

2. Keyboard Simulation Technology

In many games, not only the operation of the mouse is provided, but also the operation of the keyboard, in the attack on the object
Shortcut keys can also be used when attacking

。 In order for these attacks to be automated, the plug-in needs to use keyboard emulation techniques. Like mouse simulation technology
Windows API also provides a

A series of API functions to perform simulations of keyboard actions.

Analog Keyboard Action API function keydb_event, which simulates pressing a key or keys on the keyboard
or let go of the action.

VOID keybd_event (
BYTE BVK,//virtual key value.
BYTE Bscan,//Hardware scan code.
DWORD DwFlags,//action ID.
DWORD dwExtraInfo//auxiliary information associated with the keyboard action.
);

Where BVK represents a virtual key value, in fact it is a byte-type value of a macro with a value range of 1-254. Yes
The Virtual key value table is used on MSDN

Keyword "Virtual-key Codes" to find relevant information. Bscan means that when a key on the keyboard is pressed and released
, keyboard system hardware generates a scan code

, we can convert the Mapvirtualkey () function between the virtual key value and the scan code. dwflags represents various
All kinds of keyboard action, it has two

Values: Keyeventf_extendedkey and Keyeventf_keyup.

Below we use a piece of code implementation to press the Shift+r shortcut key in the game to attack the attack object.

Keybd_event (Vk_control,mapvirtualkey (vk_control,0), 0,0); Hold down CTRL
Key.
Keybd_event (0x52,mapvirtualkey (0x52,0), 0,0);//Key under R key.
Keybd_event (0x52,mapvirtualkey (0x52,0), keyeventf_keyup,0);//Release the R key.
Keybd_event (Vk_control,mapvirtualkey (vk_control,0),
keyeventf_keyup,0);//Release the CTRL key.

[Go] mouse and keyboard emulation API

Related Article

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.