Selecting Text - Pls Help!!
-
:confused: This should be easy. All i want to do is have some text highlighted in a control in another window. The control doesn't respond to any EM_* messages so i'm simulating the selection of text by sending a series of VK_RIGHT and VK_SHIFT key presses. The problem is that it the 'right arrow' key gets pressed but the control doesn't have the effect of the 'shift' key. I know it gets pressed because once i move around in the control, the shift key is still down (when i uncomment the shift release key press - see last line of below method)!! I have to do it this way as i have no other choice. Here's the code: window->BringWindowToTop(); //THIS PART WORKS //Same effect as pressing the right arrow key 10 times for (int a = 0; a < 10; a++){ wnd->SendMessage(WM_KEYDOWN, VK_RIGHT, 0); wnd->SendMessage(WM_KEYUP, VK_RIGHT, 0); } //This part should select the next 5 characters of text. //Simulates holding down shift and pressing 'Right Arrow' key 5 times INPUT input[2]; for (int b = 0; b < 5; b++){ input[0] = GetKeyboardInput(VK_LSHIFT, 0); input[1] = GetKeyboardInput(VK_RIGHT, 0); //Press down shift and right arrow key SendInput(2, input, sizeof(INPUT)); input[1] = GetKeyboardInput(VK_RIGHT, 2); //Release right arrow key SendInput(2, input, sizeof(INPUT)); } //Release the shift key input[0] = GetKeyboardInput(VK_LSHIFT, 2); SendInput(2, input, sizeof(INPUT)); INPUT CFindInWindowDlg::GetKeyboardInput(short key, int downUp) { INPUT input; input.type = 0x01; //INPUT_KEYBOARD input.ki.dwExtraInfo = 0; input.ki.time = 0; input.ki.wScan = 0; input.ki.wVk = key; input.ki.dwFlags = downUp; return input; } Any help would be greatly appreciated. My head is going to explode! :wtf:
-
:confused: This should be easy. All i want to do is have some text highlighted in a control in another window. The control doesn't respond to any EM_* messages so i'm simulating the selection of text by sending a series of VK_RIGHT and VK_SHIFT key presses. The problem is that it the 'right arrow' key gets pressed but the control doesn't have the effect of the 'shift' key. I know it gets pressed because once i move around in the control, the shift key is still down (when i uncomment the shift release key press - see last line of below method)!! I have to do it this way as i have no other choice. Here's the code: window->BringWindowToTop(); //THIS PART WORKS //Same effect as pressing the right arrow key 10 times for (int a = 0; a < 10; a++){ wnd->SendMessage(WM_KEYDOWN, VK_RIGHT, 0); wnd->SendMessage(WM_KEYUP, VK_RIGHT, 0); } //This part should select the next 5 characters of text. //Simulates holding down shift and pressing 'Right Arrow' key 5 times INPUT input[2]; for (int b = 0; b < 5; b++){ input[0] = GetKeyboardInput(VK_LSHIFT, 0); input[1] = GetKeyboardInput(VK_RIGHT, 0); //Press down shift and right arrow key SendInput(2, input, sizeof(INPUT)); input[1] = GetKeyboardInput(VK_RIGHT, 2); //Release right arrow key SendInput(2, input, sizeof(INPUT)); } //Release the shift key input[0] = GetKeyboardInput(VK_LSHIFT, 2); SendInput(2, input, sizeof(INPUT)); INPUT CFindInWindowDlg::GetKeyboardInput(short key, int downUp) { INPUT input; input.type = 0x01; //INPUT_KEYBOARD input.ki.dwExtraInfo = 0; input.ki.time = 0; input.ki.wScan = 0; input.ki.wVk = key; input.ki.dwFlags = downUp; return input; } Any help would be greatly appreciated. My head is going to explode! :wtf:
c121hains wrote: The problem is that it the 'right arrow' key gets pressed but the control doesn't have the effect of the 'shift' key. I know it gets pressed because once i move around in the control, the shift key is still down (when i uncomment the shift release key press - see last line of below method)!! Try the following. I tested it with an edit control in the same dialog and it worked... May or may not work in your case, but I hope you'll let us know :)
static INPUT GetKeyboardInput(short key, DWORD dwFlags)
{
INPUT input;
input.type = INPUT_KEYBOARD;
input.ki.dwExtraInfo = 0;
input.ki.time = 0;
input.ki.wScan = 0;
input.ki.wVk = key;
input.ki.dwFlags = dwFlags;
return input;
}
void CFindInWindowDlg::Select(CWnd* window, int start, int count)
{
window->BringWindowToTop();
INPUT input[4];
input[0] = GetKeyboardInput(VK_SHIFT, KEYEVENTF_EXTENDEDKEY);
input[1] = GetKeyboardInput(VK_SHIFT, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY);
input[2] = GetKeyboardInput(VK_RIGHT, 0);
input[3] = GetKeyboardInput(VK_RIGHT, KEYEVENTF_KEYUP);
// move to start
for (int i = 1; i < start; i++)
{
SendInput(2, &input[2], sizeof(INPUT));
}
// press shift key
SendInput(1, &input[0], sizeof(INPUT));
// move cursor
for (int j = 0; j < count; j++)
{
SendInput(2, &input[2], sizeof(INPUT));
}
// release shift key
SendInput(1, &input[1], sizeof(INPUT));
}I think your problem was: 1. Not using KEYEVENTF_EXTENDEDKEY for the shift key. 2. Sending too many shift keys (where you say "release the right key" you were actually sending [shift down + right up], and after the loop you were sending [shift up + right up]). Hope that helps, -- jlr http://jlamas.blogspot.com/[^]
-
c121hains wrote: The problem is that it the 'right arrow' key gets pressed but the control doesn't have the effect of the 'shift' key. I know it gets pressed because once i move around in the control, the shift key is still down (when i uncomment the shift release key press - see last line of below method)!! Try the following. I tested it with an edit control in the same dialog and it worked... May or may not work in your case, but I hope you'll let us know :)
static INPUT GetKeyboardInput(short key, DWORD dwFlags)
{
INPUT input;
input.type = INPUT_KEYBOARD;
input.ki.dwExtraInfo = 0;
input.ki.time = 0;
input.ki.wScan = 0;
input.ki.wVk = key;
input.ki.dwFlags = dwFlags;
return input;
}
void CFindInWindowDlg::Select(CWnd* window, int start, int count)
{
window->BringWindowToTop();
INPUT input[4];
input[0] = GetKeyboardInput(VK_SHIFT, KEYEVENTF_EXTENDEDKEY);
input[1] = GetKeyboardInput(VK_SHIFT, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY);
input[2] = GetKeyboardInput(VK_RIGHT, 0);
input[3] = GetKeyboardInput(VK_RIGHT, KEYEVENTF_KEYUP);
// move to start
for (int i = 1; i < start; i++)
{
SendInput(2, &input[2], sizeof(INPUT));
}
// press shift key
SendInput(1, &input[0], sizeof(INPUT));
// move cursor
for (int j = 0; j < count; j++)
{
SendInput(2, &input[2], sizeof(INPUT));
}
// release shift key
SendInput(1, &input[1], sizeof(INPUT));
}I think your problem was: 1. Not using KEYEVENTF_EXTENDEDKEY for the shift key. 2. Sending too many shift keys (where you say "release the right key" you were actually sending [shift down + right up], and after the loop you were sending [shift up + right up]). Hope that helps, -- jlr http://jlamas.blogspot.com/[^]