Welcome

Wheresoever you go, go with all your heart. (Confucius)

5/25/2012

Using Key simulation in MFC

Sometimes, we need to send keys to another process when developing MFC application.
We called sending keys "Key simulation"
For this purpose, we can use the function "SendInput" as a function of MFC.
Here I will describe an example. So, see the function description.


We have to get a window handle to send keys.
In MFC, there are several methods for this. Here I will use FindWindowEx method.
When finding the specific window handle, you can describe "class name" or "Window title name"
I think "Window title name" is more convenient.

// Get window handle

HWND hWindow = ::FindWindowEx(NULL, NULL, "Class Name", 0);
or
HWND hWindow = ::FindWindowEx(NULL, NULL, 0, "Window title name");

// Get thread and process id

PROCESS_INFORMATION pi;
pi.dwThreadId = ::GetWindowThreadProcessId(hWindow, &(pi.dwThreadId));

SendKey(WORD('A'), MapVirtualKey('A',0), pi);


Refer two methods 'SendKey' and 'SendCtrlShiftKey'


void SendKey(WORD vk, WORD scan, PROCESS_INFORMATION pi)
{

INPUT is;

is.type = INPUT_KEYBOARD;  //INPUT_KEYBOARD = 1
is.ki.time = 0;

is.ki.wVk = vk;
is.ki.wScan = scan;
is.ki.dwFlags = NULL;
is.ki.dwExtraInfo = NULL;

SendInput(1, &is, sizeof(INPUT)); // Key down
WaitForInputIdle( pi.hProcess, INFINITE );

is.ki.wVk = vk;
is.ki.dwFlags = KEYEVENTF_KEYUP;
is.ki.wScan = scan;
is.type = INPUT_KEYBOARD;

SendInput(1, &is, sizeof(INPUT)); // Key up
WaitForInputIdle( pi.hProcess, INFINITE );
}


void SendCtrlShiftKey(WORD vk, PROCESS_INFORMATION pi)
{
INPUT inp[6];
short Ecnt = 0x00;


// VK_CONTROL down.
inp[0].ki.time = NULL;
inp[0].type = INPUT_KEYBOARD;
inp[0].ki.dwExtraInfo = NULL;
inp[0].ki.dwFlags = NULL;
inp[0].ki.wVk = VK_CONTROL;
inp[0].ki.wScan = MapVirtualKey(VK_CONTROL,0);

// VK_SHIFT down.
inp[1].ki.time = NULL;
inp[1].type = INPUT_KEYBOARD;
inp[1].ki.dwExtraInfo = NULL;
inp[1].ki.dwFlags = NULL;
inp[1].ki.wVk = VK_SHIFT;
inp[1].ki.wScan = MapVirtualKey(VK_SHIFT,0);

// Character Key down.
inp[2].ki.time = NULL;
inp[2].type = INPUT_KEYBOARD;
inp[2].ki.dwExtraInfo = NULL;
inp[2].ki.dwFlags = NULL;
inp[2].ki.wVk = vk;
inp[2].ki.wScan = MapVirtualKey(vk,0);

// VK_CONTROL up.
inp[3].ki.time = NULL;
inp[3].type = INPUT_KEYBOARD;
inp[3].ki.dwExtraInfo = NULL;
inp[3].ki.dwFlags = KEYEVENTF_KEYUP;
inp[3].ki.wVk = VK_CONTROL;
inp[3].ki.wScan = MapVirtualKey(VK_CONTROL,0);

// VK_SHIFT up.
inp[4].ki.time = NULL;
inp[4].type = INPUT_KEYBOARD;
inp[4].ki.dwExtraInfo = NULL;
inp[4].ki.dwFlags = KEYEVENTF_KEYUP;
inp[4].ki.wVk = VK_SHIFT;
inp[4].ki.wScan = MapVirtualKey(VK_SHIFT,0);

// Character Key up.
inp[5].ki.time = NULL;
inp[5].type = INPUT_KEYBOARD;
inp[5].ki.dwExtraInfo = NULL;
inp[5].ki.dwFlags = KEYEVENTF_KEYUP;
inp[5].ki.wVk = vk;
inp[5].ki.wScan = MapVirtualKey(vk,0);



SendInput(1,inp,sizeof(INPUT)); // VK_CONTROL down
WaitForInputIdle( pi.hProcess, INFINITE );

SendInput(1,&inp[1],sizeof(INPUT)); // VK_SHIFT down
WaitForInputIdle( pi.hProcess, INFINITE );

SendInput(1,&inp[2],sizeof(INPUT)); // Key down
WaitForInputIdle( pi.hProcess, INFINITE );

SendInput(1,&inp[5],sizeof(INPUT)); // Key up
WaitForInputIdle( pi.hProcess, INFINITE );

SendInput(1,&inp[4],sizeof(INPUT)); // VK_SHIFT up
WaitForInputIdle( pi.hProcess, INFINITE );

SendInput(1,&inp[3],sizeof(INPUT)); // VK_SHIFT up
WaitForInputIdle( pi.hProcess, INFINITE );
}

No comments: