Several Methods for simulating mouse and keyboard input in Windows (with source code)

Source: Internet
Author: User

Several Methods for simulating mouse and keyboard input in Windows

Recently, the server used by Xiao Dingdong (ASP server on the 3rd side, not IIS) cannot run automatically after startup. There may be bugs in it. You need to click the start button to connect to the Internet.
In order to ensure the stable operation of Xiao Dingdong, I set my machine (home) to start once every hour and automatically run the server software after it starts.
This software does not have a command line startup method similar to:/run, so I wonder if it can be automatically implemented by the program.Simulate Mouse clicking?

Because my requirements and running environment allow me to do this:
1. After the software is started, the "run" button is fixed.
2. You only need to click the run button,

There are two conditions above, so it is feasible to use the program to simulate and execute the "run" button.
So I used Google to investigate
As a programmer, there are two ways to solve this problem: 1. Use a ready-made program and 2. Let's take a look at how to solve it.

Input on Google and simulate the mouse to find several options
1. Use off-the-shelf software such as the "Button wizard" (this is not described in this article)
2 There Is A. Net Method of mouse simulation http://www.cnblogs.com/edobnet/archive/2004/06/09/14334.aspx
3. The most widely used MFC implementation method is Xu Jingzhou's article on vckbase (see Xu Jingzhou's column)
4 Programming Using MS active accessibility Interface Technology

Lightweight, heavyweight, and ready-to-use

The following methods are used:SetcursorposAndMouse_eventImplemented by winapi.
The implementation prototype is as follows:
Setcursorpos (XXX, XXX );
Mouse_event (mouseeventf_leftdown, 0, 0, 0 );
Mouse_event (mouseeventf_leftup, 0, 0, 0 );

If you are interested, refer to the introduction in the above article.

This article introduces another method:
Usage:SendinputWinapi. This method is described as follows:
[New for Windows NT 4.0 Service Pack 3.]
The sendinput FunctionSynthesizes keystrokes, mouse motions, and button clicks.
It seems that it integrates information such as keyboard buttons, mouse activity, and tablet input, which can fully meet the preceding requirements.
Another structure is used with sendinput:
Structure:Input
[New for Windows NT 4.0 Service Pack 3.]
The input structure is usedSendinputTo SYnthesize keystrokes, mouse motions, and button clicks.
Typedef struct taginput {
DWORD type;
Union
{
Mouseinput Mi;
Keybdinput Ki;
Hardwareinput Hi;
};
} Input, * pinput, far * lpinput;

For more details, refer:
Http://www.piclist.com/techref/ OS /win/api/win32/struc/src/str09_12.htm
Http://www.china-askpro.com/msg18/qa18.shtml
Http://www.daniweb.com/techtalkforums/showthread.php? T = 6727

The following code is compiled using Dev ++. (Two files in total)

// Stdafx. h
# Pragma once
# Define win32_lean_and_mean
# DEFINE _ win32_winnt 0x0500 // note that the VC generation code value is: 0x0400. When running in VC, note that
# Include <windows. h>

// Main. cpp
# Include "stdafx. H"

Int main (INT argc, char * argv []) {
Input * buffer = new input [3];
Buffer-> type = input_mouse;
Buffer-& gt; Mi. dx = 100;
Buffer-& gt; Mi. DY = 100;
Buffer-> mi. mousedata = 0;
Buffer-> mi. dwflags = (mouseeventf_absolute | mouseeventf_move );
Buffer-> mi. Time = 0;
Buffer-> mi. dwextrainfo = 0;

(Buffer + 1)-> type = input_mouse;
(Buffer + 1)-> mi. dx = 100;
(Buffer + 1)-> mi. DY = 100;
(Buffer + 1)-> mi. mousedata = 0;
(Buffer + 1)-> mi. dwflags = mouseeventf_leftdown;
(Buffer + 1)-> mi. Time = 0;
(Buffer + 1)-> mi. dwextrainfo = 0;

(Buffer + 2)-> type = input_mouse;
(Buffer + 2)-> mi. dx = 100;
(Buffer + 2)-> mi. DY = 100;
(Buffer + 2)-> mi. mousedata = 0;
(Buffer + 2)-> mi. dwflags = mouseeventf_leftup;
(Buffer + 2)-> mi. Time = 0;
(Buffer + 2)-> mi. dwextrainfo = 0;

Sendinput (3, buffer, sizeof (input ));
Delete (buffer );
Return 0;
}

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.