C++模拟键盘鼠标消息

#include <Windows.h>
/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  LeftClick
 *  Description:  左键按下消息
 * =====================================================================================
 */
void LeftClick ( )
{  
  INPUT    Input={0};
  // left down 
  Input.type      = INPUT_MOUSE;
  Input.mi.dwFlags  = MOUSEEVENTF_LEFTDOWN;
  ::SendInput(1,&Input,sizeof(INPUT));

  // left up
  ::ZeroMemory(&Input,sizeof(INPUT));
  Input.type      = INPUT_MOUSE;
  Input.mi.dwFlags  = MOUSEEVENTF_LEFTUP;
  ::SendInput(1,&Input,sizeof(INPUT));
}
/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  RightClick
 *  Description:  右键按下消息
 * =====================================================================================
 */
void RightClick ( )
{  
  INPUT    Input={0};
  // right down 
  Input.type      = INPUT_MOUSE;
  Input.mi.dwFlags  = MOUSEEVENTF_RIGHTDOWN;
  ::SendInput(1,&Input,sizeof(INPUT));

  // right up
  ::ZeroMemory(&Input,sizeof(INPUT));
  Input.type      = INPUT_MOUSE;
  Input.mi.dwFlags  = MOUSEEVENTF_RIGHTUP;
  ::SendInput(1,&Input,sizeof(INPUT));
}

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  鼠标移动消息
 *  Description:  
 * =====================================================================================
 */
void MouseMove (int x, int y )
{  
	double fScreenWidth    = ::GetSystemMetrics( SM_CXSCREEN )-1; 
	double fScreenHeight  = ::GetSystemMetrics( SM_CYSCREEN )-1; 
	double fx = x*(65535.0f/fScreenWidth);
	double fy = y*(65535.0f/fScreenHeight);
	INPUT  Input={0};
	Input.type      = INPUT_MOUSE;
	Input.mi.dwFlags  = MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE;
	Input.mi.dx = fx;
	Input.mi.dy = fy;
	::SendInput(1,&Input,sizeof(INPUT));
}
int main(){

	while(true)
	{
		MouseMove(0,100);

		Sleep(5000);

		MouseMove(200,0);

		Sleep(5000);
	
	}
    //RightClick ( );
    //LeftClick ( );
	return 0;
}

  以上是使用sendinput函数

以下是使用keybd_event函数

//#include <Windows.h>
#include <afxwin.h>

int main()
{

	//CWnd *pWnd = CWnd::FromHandle(FindWindow("ConsolWindowClass", NULL));   GetConsoleWindow(void);
	CWnd *pWnd = CWnd::FromHandle(GetConsoleWindow());   //本控制台程序发送按键消息
	if (pWnd->GetSafeHwnd()) 
		{ 
			pWnd->ShowWindow(SW_NORMAL); 
			pWnd->SetForegroundWindow(); 
			//keybd_event(VK_CONTROL, 0, 0 ,0);
			keybd_event('E', 0, 0 ,0);
			keybd_event('E', 0, KEYEVENTF_KEYUP ,0);

			//keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP,0);
		}
	return 0;
}

  demo

 1 /*
 2  * =====================================================================================
 3  *
 4  *       Filename:  get_message.cpp
 5  *
 6  *    Description:  子线程产生按键消息,主线程等待输入,并输出
 7  *
 8  *        Version:  1.0
 9  *        Created:  2013/10/1 10:33:36
10  *       Revision:  none
11  *       Compiler:  gcc
12  *
13  *         Author:  @礼杨_HDU, yuliyang@qq.com
14  *   Organization:  
15  *
16  * =====================================================================================
17  */
18 
19 #include <iostream>
20 #include <Windows.h>
21 using namespace std;
22 DWORD WINAPI ThreadFunction(LPVOID lpParameter)
23 {
24     printf("in thread......");
25     while (TRUE){
26         //your code
27         
28         INPUT Input1 = { 0 };
29         Input1.type       = INPUT_KEYBOARD;
30         Input1.mi.dwFlags = KEYEVENTF_EXTENDEDKEY;
31         Input1.ki.wVk   = 0x30; // 0
32         SendInput( 1, &Input1, sizeof( INPUT ) );
33         INPUT Input2 = { 0 };
34         Input2.type       = INPUT_KEYBOARD;
35         Input2.mi.dwFlags = KEYEVENTF_EXTENDEDKEY;
36         Input2.ki.wVk   = 0x0d; // 回车
37         SendInput( 1, &Input2, sizeof( INPUT ) );
38 
39         Sleep(10000);
40         
41         Input1.type       = INPUT_KEYBOARD;
42         Input1.mi.dwFlags = KEYEVENTF_EXTENDEDKEY;
43         Input1.ki.wVk   = 0x31; //1
44         SendInput( 1, &Input1, sizeof( INPUT ) );
45         
46         Input2.type       = INPUT_KEYBOARD;
47         Input2.mi.dwFlags = KEYEVENTF_EXTENDEDKEY;
48         Input2.ki.wVk   = 0x0d; // 回车
49         SendInput( 1, &Input2, sizeof( INPUT ) );
50     }
51     return true;
52 }
53 
54 int main(){
55     HANDLE hT=CreateThread(NULL,0,ThreadFunction,NULL,0,NULL);
56 
57     char m_char;
58     
59     while (cin>>m_char)
60     {
61         printf("%c\n",m_char);
62     }
63     return 0;
64 
65 }

 

转载于:https://www.cnblogs.com/yuliyang/p/3348373.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值