Several ways to simulate mouse keyboard input under Windows (with source code)

Source: Internet
Author: User
Tags pack

Several ways to simulate mouse keyboard input under Windows

Recently small ding dong used the server (3rd party ASP server, not IIS) after the start is always unable to automatically run, there may be bugs, you need to click the Launch button to connect to the Internet.
In order to ensure the stable operation of small Ding dong, I put my machine (put at home) configured to start once per hour, start automatically run Server Software
This software does not resemble:/run and so on command line startup way, so I thought, can use the program to automatically realizeAnalog Mouse click buttonThe function?

Because my needs and operating environment allow me to do this:
1 The "Run" button is fixed after the software starts
2 only need to click on the Run button,

There are 2 conditions above, so it is feasible to use the program to simulate the execution of the click Run button.
So I started investigating:-) with Google.
As a programmer, there are two ways to solve such problems: 1 Use the ready-made program 2 to do their own specific how to see .... actually need it.

On google input, mouse simulation, you can find a few options
1 use of off-the-shelf software such as "Key Wizard" (this is not covered in this article)
2 online with. NET to implement the method of mouse simulation http://www.cnblogs.com/edobnet/archive/2004/06/09/14334.aspx
3 Use MFC implementation methods, the most, most famous is the Xu Jingzhou on the vckbase (see Xu Jingzhou column)
4 Programming with MS Active Accessibility interface Technology

Lightweight, heavyweight, ready-made all have, hehe

The following methods are used:SetcursorposAndmouse_eventWINAPI to achieve.
The implementation prototype is as follows:
Setcursorpos (xxx, XXX);
Mouse_event (Mouseeventf_leftdown, 0,0,0,0);
Mouse_event (Mouseeventf_leftup, 0,0,0,0);

Interested readers can refer to the above article to see the introduction

This article describes another approach:
Use: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 the integration of keyboard keys, mouse activity, handwritten tablet input and other information, can fully achieve the requirements mentioned above.
There is another structure to use with SendInput:
Structure:INPUT
[New for Windows NT 4.0 Service Pack 3.]
The INPUT structure is used bySendInputto SYnthesize keystrokes, mouse motions, and button clicks.
typedef struct TAGINPUT {
DWORD type;
Union
{
Mouseinput mi;
Keybdinput Ki;
Hardwareinput Hi;
};
INPUT, *pinput, far* lpinput;

More detailed information reference:
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 by using dev++. (A total of 2 files)

StdAfx.h
#pragma once
#define Win32_lean_and_mean
#define _WIN32_WINNT 0x0500//Note VC generated code value is: 0x0400, put in the VC running time to pay attention to
#include <windows.h>

Main.cpp
#include "stdafx.h"

int main (int argc, char* argv[]) {
INPUT *buffer = new Input[3];
Buffer->type = Input_mouse;
BUFFER->MI.DX = 100;
Buffer->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 (INPUT) (3,buffer,sizeof);
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.