//****************** 类mk头文件mk.h **************************
#if !defined(AFX_MK_H__B024D48F_090A_4F6F_A199_32996DF699B3__INCLUDED_)
#define AFX_MK_H__B024D48F_090A_4F6F_A199_32996DF699B3__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "winable.h"
class mk
{
public:
mk();
static void Key(BYTE k, int mSeconds=0);//按一个键,mSeconds表按键后到
//下一次操作延迟的时间:ms
static void Key2(BYTE k1, BYTE k2,int mSeconds=0);//按2个键
static void Key3(BYTE k1,BYTE k2,BYTE k3,int mSeconds=0);//按3个键
static void Click(int x,int y,int mSeconds=0);//单击
static void RClick(int x,int y,int mSeconds=0);//右击
static void DClick(int x,int y,int mSeconds=0);//双击
static void CtrlClick(int x, int y, int mSeconds=0);//Ctrl+单击
static void Drag(int x1,int y1,int x2,int y2,int mSeconds=0);//鼠标拖动
virtual ~mk();
};
#endif // !defined(AFX_MK_H__B024D48F_090A_4F6F_A199_32996DF699B3__INCLUDED_)
//****************** 类mk头文件mk.h end**************************
//****************** 类mk实现文件mk.cpp **************************
#include "stdafx.h"
#include "mk.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
mk::mk()
{
}
mk::~mk()
{
}
void mk::Click(int x, int y, int mSeconds)
{
SetCursorPos(x,y);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
Sleep(mSeconds);
}
void mk::DClick(int x, int y, int mSeconds)
{
SetCursorPos(x,y);
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);
Sleep(mSeconds);
}
void mk::Drag(int x1, int y1, int x2, int y2, int mSeconds)
{
SetCursorPos(x1,y1);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
SetCursorPos(x2,y2);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
Sleep(mSeconds);
}
void mk::RClick(int x, int y, int mSeconds)
{
SetCursorPos(x,y);
mouse_event(MOUSEEVENTF_RIGHTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_RIGHTUP,0,0,0,0);
Sleep(mSeconds);
}
void mk::Key(BYTE k, int mSeconds)
{
keybd_event(k,0,0,0);
keybd_event(k,0,KEYEVENTF_KEYUP,0);
Sleep(mSeconds);
}
void mk::Key2(BYTE k1, BYTE k2, int mSeconds)
{
//法1
//keybd_event(k1, 0, 0 ,0);
//keybd_event(k2, 0, 0 ,0);
//keybd_event(k2, 0, KEYEVENTF_KEYUP,0);
//keybd_event(k1, 0, KEYEVENTF_KEYUP,0);
//法2,更通用,但须先#include "winable.h"
INPUT input[4];
memset(input,0,sizeof(input));
input[0].type=input[1].type=input[2].type=input[3].type=INPUT_KEYBOARD;
input[0].ki.wVk=input[3].ki.wVk=k1;
input[1].ki.wVk=input[2].ki.wVk=k2;
input[2].ki.dwFlags = input[3].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(4, input, sizeof(INPUT));
Sleep(mSeconds);
}
void mk::CtrlClick(int x, int y, int mSeconds)
{
SetCursorPos(x,y);
keybd_event(VK_CONTROL ,0,0,0);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
keybd_event(VK_CONTROL,0,KEYEVENTF_KEYUP,0);
Sleep(mSeconds);
}
void mk::Key3(BYTE k1, BYTE k2, BYTE k3, int mSeconds)
{
if(k1==VK_CONTROL&&k2==VK_MENU&&k3==VK_DELETE)
ShellExecute(NULL,NULL,"taskmgr.exe",NULL,NULL,SW_SHOW);
else
{
INPUT input[6];
memset(input,0,sizeof(input));
input[0].type=input[1].type=input[2].type=input[3].type=input[4].type=input[5].type=INPUT_KEYBOARD;
input[0].ki.wVk=input[5].ki.wVk=k1;
input[1].ki.wVk=input[4].ki.wVk=k2;
input[2].ki.wVk=input[3].ki.wVk=k3;
input[3].ki.dwFlags=input[4].ki.dwFlags=input[5].ki.dwFlags=KEYEVENTF_KEYUP;
SendInput(6, input, sizeof(INPUT));
}
Sleep(mSeconds);
}
//****************** 类mk实现文件mk.cpp end**************************
===================================================================
VC模拟键盘操作
INPUT input[4];
memset(input, 0, sizeof(input));
//设置模拟键盘输入
input[0].type = input[1].type = input[2].type = input[3].type = INPUT_KEYBOARD;
input[0].ki.wVk = input[2].ki.wVk = VK_CONTROL;
input[1].ki.wVk = input[3].ki.wVk = VK_ESCAPE;
// 释放按键
input[2].ki.dwFlags = input[3].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(4, input, sizeof(INPUT));
*********************************************************************************
符号常量 十六进制值 指定的鼠标或键盘按键
VK_LBUTTON 01 鼠标左键
VK_RBUTTON 02 鼠标右键
VK_CANCEL 03 Control-break 过程
VK_MBUTTON 04 鼠标中键
VK_BACK 08 BACKSPACE 键
VK_TAB 09 TAB 键
VK_CLEAR 0C CLEAR 键
VK_RETURN 0D ENTER 键
VK_SHIFT 10 SHIFT 键
VK_CONTROL 11 CTRL 键
VK_MENU 12 ALT 键
VK_PAUSE 13 PAUSE 键
VK_CAPITAL 14 CAPS LOCK 键
VK_ESCAPE 1B ESC 键
VK_SPACE 20 SPACEBAR
VK_PRIOR 21 PAGE UP 键
VK_NEXT 22 PAGE DOWN 键
VK_END 23 END 键
VK_HOME 24 HOME 键
VK_LEFT 25 LEFT ARROW 键
VK_UP 26 UP ARROW 键
VK_RIGHT 27 RIGHT ARROW 键
VK_DOWN 28 DOWN ARROW 键
VK_SELECT 29 SELECT 键
VK_EXECUTE 2B EXECUTE 键
VK_SNAPSHOT 2C PRINT SCREEN键(用于Windows 3.0及以后版本)
VK_INSERT 2D INS 键
VK_DELETE 2E DEL 键
VK_HELP 2F HELP 键
///////////////////////////////////////////////////
对于字母键和非小键盘上的数字键,直接在单引号中加入该键就行.
比如:a键:'A'
1键:'1'
//////////////////////////////////////////////
VK_LWIN 5B Left Windows 键 (Microsoft自然键盘)
VK_RWIN 5C Right Windows 键 (Microsoft自然键盘)
VK_APPS 5D Applications 键 (Microsoft自然键盘)
VK_NUMPAD0 60 数字小键盘上的 0 键
VK_NUMPAD1 61 数字小键盘上的 1 键
VK_NUMPAD2 62 数字小键盘上的 2 键
VK_NUMPAD3 63 数字小键盘上的 3 键
VK_NUMPAD4 64 数字小键盘上的 4 键
VK_NUMPAD5 65 数字小键盘上的 5 键
VK_NUMPAD6 66 数字小键盘上的 6 键
VK_NUMPAD7 67 数字小键盘上的 7 键
VK_NUMPAD8 68 数字小键盘上的 8 键
VK_NUMPAD9 69 数字小键盘上的 9 键
VK_MULTIPLY 6A Multiply 键
VK_ADD 6B Add 键
VK_SEPARATOR 6C Separator 键
VK_SUBTRACT 6D Subtract 键
VK_DECIMAL 6E Decimal 键
VK_DIVIDE 6F Divide 键
VK_F1 70 F1 键
VK_F2 71 F2 键
VK_F3 72 F3 键
VK_F4 73 F4 键
VK_F5 74 F5 键
VK_F6 75 F6 键
VK_F7 76 F7 键
VK_F8 77 F8 键
VK_F9 78 F9 键
VK_F10 79 F10 键
VK_F11 7A F11 键
VK_F12 7B F12 键
VK_F13 7C F13 键
VK_F14 7D F14 键
VK_F15 7E F15 键
VK_F16 7F F16 键
VK_F17 80H F17 键
VK_F18 81H F18 键
VK_F19 82H F19 键
VK_F20 83H F20 键
VK_F21 84H F21 键
VK_F22 85H F22 键
VK_F23 86H F23 键
VK_F24 87H F24 键
VK_NUMLOCK 90 NUM LOCK 键
VK_SCROLL 91 SCROLL LOCK 键
VK_ATTN F6 Attn 键
VK_CRSEL F7 CrSel 键
VK_EXSEL F8 ExSel 键
VK_EREOF F9 Erase EOF 键
VK_PLAY FA Play 键
VK_ZOOM FB Zoom 键
VK_OEM_CLEAR FE Clear 键
举例:
(一)响应单独的按键:
先添加PreTranslateMessage()(响应WM_CHAR)也是同样的效果,因为本例只捕捉键盘)
BOOL CMydilog::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if (pMsg->message == WM_KEYDOWN)
{
if(pMsg->wParam=='M')//直接用上面的虚码代替就可以响应所指键
MessageBox("hello");//如果按下M键弹出消息.比如想当按下小键盘1时
//弹出就用VK_NUMPAD1代替'M'
}
return CDialog::PreTranslateMessage(pMsg);
}
(二)组合键的用法:(本例响应Ctrl+X键)
BOOL CMydilog::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if (pMsg->message == WM_KEYDOWN)
{
switch (pMsg->wParam)
{
case VK_ESCAPE:
SetFocus ();
return TRUE;
case 'X':
if(::GetKeyState(VK_CONTROL) < 0)//如果是Shift+X这里就
//改成VK_SHIFT
MessageBox("hello");
return TRUE;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
posted on 2007-07-19 21:36
聂文龙 阅读(10468)
评论(6) 编辑 收藏 引用 所属分类:
c++