// ******************* Class MK header file. h **************************
# If! Defined (afx_mk_h%b024d48f_090a_4f6f_a199_%6df699b3%included _)
# Define afx_mk_h1_b024d48f_090a_4f6f_a199_4156df699b31_encoded _
# 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); // press a key.
// Delay time of the next operation: Ms
Static void key2 (byte K1, byte K2, int mseconds = 0); // press two keys
Static void key3 (byte K1, byte K2, byte K3, int mseconds = 0); // press three keys
Static void click (int x, int y, int mseconds = 0); // click
Static void rclick (int x, int y, int mseconds = 0); // right-click
Static void dclick (int x, int y, int mseconds = 0); // double-click
Static void ctrlclick (int x, int y, int mseconds = 0); // Ctrl + Click
Static void drag (INT X1, int Y1, int X2, int Y2, int mseconds = 0); // drag the cursor
Virtual ~ MK ();
};
# Endif //! Defined (afx_mk_h%b024d48f_090a_4f6f_a199_%6df699b3%included _)
// ******************* Class MK header file. h end **************************
// ******************** Class MK implementation file. 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 );
Mouse_event (mouseeventf_leftup, 0, 0, 0 );
Sleep (mseconds );
}
Void MK: dclick (int x, int y, int mseconds)
{
Setcursorpos (x, y );
Mouse_event (mouseeventf_leftdown, 0, 0, 0 );
Mouse_event (mouseeventf_leftup, 0, 0, 0 );
Mouse_event (mouseeventf_leftdown, 0, 0, 0 );
Mouse_event (mouseeventf_leftup, 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 );
Setcursorpos (X2, Y2 );
Mouse_event (mouseeventf_leftup, 0, 0, 0 );
Sleep (mseconds );
}
Void MK: rclick (int x, int y, int mseconds)
{
Setcursorpos (x, y );
Mouse_event (mouseeventf_rightdown, 0, 0, 0 );
Mouse_event (mouseeventf_rightup, 0, 0, 0 );
Sleep (mseconds );
}
Void MK: Key (byte K, int mseconds)
{
Keybd_event (K, 0, 0 );
Keybd_event (K, 0, keyeventf_keyup, 0 );
Sleep (mseconds );
}
Void MK: key2 (byte K1, byte K2, int mseconds)
{
// Method 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 );
// Method 2, more common, but must first # 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 );
Mouse_event (mouseeventf_leftdown, 0, 0, 0 );
Mouse_event (mouseeventf_leftup, 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 );
}
// ******************** Class MK implementation file. CPP end **************************
========================================================== ======================================
VC simulated keyboard operation
Input input [4];
Memset (input, 0, sizeof (input ));
// Set the analog keyboard 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;
// Release the button
Input [2]. Ki. dwflags = input [3]. Ki. dwflags = keyeventf_keyup;
Sendinput (4, input, sizeof (input ));
**************************************** **************************************** *
The mouse or keyboard key specified by the hexadecimal value of the symbolic constant
Vk_lbutton 01 left mouse button
Vk_rbutton 02 right-click
Vk_cancel 03 control-Break Process
Vk_mbutton 04
Vk_back 08 backspace key
Vk_tab 09 Tab key
Vk_clear 0C clear key
Vk_return 0d enter
Vk_shift 10 shift key
Vk_control 11 CTRL
Vk_menu 12 Alt key
Vk_pause 13 pause key
Vk_capital 14 Caps Lock key
Vk_escape 1B ESC key
Vk_space 20 spacebar
Vk_prior 21 page up key
Vk_next 22 page down key
Vk_end 23 end key
Vk_home 24 Home Key
Vk_left 25 left arrow
Vk_up 26 up arrow key
Vk_right 27 right arrow key
Vk_down 28 down arrow key
Vk_select 29 select key
Vk_execute 2B execute key
Vk_snapshot 2C print screen key (for Windows 3.0 and later versions)
Vk_insert 2D ins key
Vk_delete 2E DEL key
Vk_help 2f help key
//////////////////////////////////////// ///////////
For numeric keys on the primary key and non-keypad, simply add this key to single quotes.
For example, key A: 'A'
1 key: '1'
//////////////////////////////////////// //////
Vk_lwin 5B left Windows key (Microsoft natural keyboard)
Vk_rwin 5C right Windows key (Microsoft natural keyboard)
Vk_apps 5d applications key (Microsoft natural keyboard)
Vk_numpad0 60 key 0 on the keypad
Vk_numpad1 61 key on the keypad
Vk_numpad2 62 key 2 on the keypad
Vk_numpad3 63 key 3 on the keypad
Vk_numpad4 64 key 4 on the keypad
Vk_numpad5 65 key 5 on the keypad
Vk_numpad6 66 Key 6 on the keypad
Vk_numpad7 67 key 7 on the keypad
Vk_numpad8 68: 8 keys on the keypad
Vk_numpad9 69 key on the keypad
Vk_multiply 6a multiply key
Vk_add 6B add key
Vk_separator 6C separator key
Vk_subtract 6D subtract key
Vk_decimal 6e decimal key
Vk_divide 6f divide key
Vk_f1 70 F1 key
Vk_f2 71 F2 key
Vk_f3 72 F3 key
Vk_f4 73 F4 key
Vk_f5 74 F5 key
Vk_f6 75 F6 key
Vk_f7 76 F7 key
Vk_f8 77 F8 key
Vk_f9 78 F9 key
Vk_f10 79 F10 key
Vk_f11 7A F11 key
Vk_f12 7b F12 key
Vk_f13 7C f13 key
Vk_f14 7d F14 key
Vk_f15 7E F15 key
Vk_f16 7f F16 key
Vk_f17 80 h f17 key
Vk_f18 81 h F18 key
Vk_f19 82 H F19 key
Vk_f20 83 H F20 key
Vk_f21 84 h F21 key
Vk_f22 85 H f22 key
Vk_f23 86 H F23 key
Vk_f24 87 h f24 key
Vk_numlock 90 Num Lock key
Vk_scroll 91 scroll Lock key
Vk_attn F6 Attn key
Vk_crsel F7 crsel key
Vk_exsel F8 exsel key
Vk_ereof F9 erase EOF key
Vk_play fa play key
Vk_zoom FB zoom key
Vk_oem_clear Fe clear key
Example:
(1) responding to separate buttons:
Adding pretranslatemessage () (responding to wm_char) is the same, because in this example, only the keyboard is captured)
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') // you can directly use the above virtual code to respond to the specified key.
MessageBox ("hello"); // If you press the M key to bring up a message, for example, if you want to press keypad 1
// Replace 'M' with vk_numpad1 in the pop-up'
}
Return cdialog: pretranslatemessage (PMSG );
}
(2) usage of the key combination: (In this example, the CTRL + x key is returned)
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) // If SHIFT + X is used
// Change to vk_shift
MessageBox ("hello ");
Return true;
}
}
Return cdialog: pretranslatemessage (PMSG );
This article Reprinted from the network base camp: http://www.xrss.cn/Dev/C/2007112817837.Html