Click to See Complete Forum and Search --> : Help! How to send a CTRL+X key to another window?


May 21st, 1999, 10:00 PM
There is a "SendKeys" procedure in VB to send one or more keystrokes to the active window as if typed at the keyboard, even a combination keystrokes,such as "CTRL+X".How to implement this function in VC?

Thanks

shellreef
May 21st, 1999, 10:47 PM
Get the hwnd of the window you want to send the keys to (for example CWnd* pWnd = CWnd::FindWindow(NULL, "Untitled - Notepad"), and then post or send a WM_CHAR message to it. For example:

[ccode]
// Get the window that we want to send Ctrl+X to. In this example 'pWnd' will be set to
// the edit control in Notepad (dialog ID 0x0F). Change this.
CWnd* pWnd = CWnd::FindWindow(NULL, "Untitled - Notepad");
if (pWnd)
{
pWnd = pWnd->GetDlgItem(0x0F);
} else {
AfxMessageBox("Can't find notepad");
exit(0);
}
pWnd->SendMessage(WM_CHAR, 24, 0, 0); // 24 = Ctrl+X




Hope this helps.

May 22nd, 1999, 12:06 AM
Thank you very much!
Another question,How about the "CTRL+SHIFT+somekey","ALT+somekey","CTRL+ALT+somekey","ALT+SHIFT+somekey"or other similar keystrokes combinations.

Thanks.

Sally
May 22nd, 1999, 06:59 AM
Great, this is something I have been looking for, but....

How did you get from Ctrl-X to 24 ?

Sally

shellreef
May 22nd, 1999, 01:42 PM
if (GetKeyState(VK_CTRL) > 0 && GetKeyState(VK_SHIFT) > 0)
{
// Ctrl+Shift is pressed (Note: I'm not sure if it's VK_CTRL or VK_CONTROL)
}



it looks like there's no VK_ALT.

shellreef
May 22nd, 1999, 01:46 PM
It's the 24th letter in the alphabet.

Ctrl+letter returns the position of 'letter' in the alphabet.

Sally
May 23rd, 1999, 03:37 AM
How do you send a VK_CONTROL, VK_SHIFT or VK_MENU combination?

Sally

Sally
May 23rd, 1999, 03:38 AM
and Ctrl 0-9 etc?

Sally

shellreef
May 23rd, 1999, 03:24 PM
Send? What do you mean?

sally
May 23rd, 1999, 10:29 PM
SendMessage( WM_KEYDOWN, //Shift F5) as an example
SendMessage( WM_KEYDOWN, //Alt F5) as an example
SendMessage( WM_KEYDOWN, //Ctrl F5) as an example

PostMessage( WM_KEYDOWN, //Shift F5) as an example
PostMessage( WM_KEYDOWN, //Alt F5) as an example
PostMessage( WM_KEYDOWN, //Ctrl F5) as an example

Sally

May 24th, 1999, 03:39 AM
Hey, I found a good solution.
Use the following code:

HWND hWnd=::FindWindow(NULL, some window caption);
if (hWnd)
{
BYTE ks[1]=
{
VK_CTRL//if wanna to send a ALT+somekey, change to VK_MENU
};
DWORD tid=::GetWindowThreadProcessId(hWnd,NULL);
::SetKeyboardState(ks);
::AttachThreadInput(::GetCurrentThreadId(),tid,TRUE);
::BringWindowToTop(hWnd);
::PostMessage(hWnd,WM_KEYDOWN,someletter, 0);
::AttachThreadInput(::GetCurrentThreadId(),tid,FALSE);
}
However, I can't send the "shift+somekey", nor the combination of 3 keys, sush as "CTRL+SHIFT+somekey".

Jason Teagle
May 24th, 1999, 04:24 AM
<Ctrl>0 - <Ctrl>9 do not have a corresponding ASCII code as the letters do; to test for this you would trap the WM_CHAR for the digits 0-9, and use (::GetAsyncKeyState(WK_CONTROL) & 0x8000) (boolean test) to see if the <Ctrl> key is down at the same time.

VB's SendKeys() has special characters to indicate <Ctrl> pressed, but in VC++ you would have to send WM_KEYDOWN messages for the key being pressed, then the WM_CHAR for the digit, then the WM_KEYUP for the key release. Not the most elegant way of doing it, unfortunately. The <Alt> key is even worse.

Jason Teagle
May 24th, 1999, 04:25 AM
Oops, that should have been VK_CONTROL, not WK_CONTROL. Stupid keyboard.

Jason Teagle
May 24th, 1999, 04:39 AM
Sending keystrokes to a window:

* ASCII key (letter, digit, space, etc.):
WM_CHAR with ASCII code.
* Special + ASCII (special = non-ASCII, but not <Alt>):
WM_KEYDOWN + VK_ (VK_SHIFT, VK_CONTROL, VK_NEXT (<PgDn>), etc.) code for each special key to be simulated, then WM_CHAR with ASCII code, then matching WM_KEYUP for special keys.
* <Alt> + ASCII:
WM_SYSCHAR with ASCII code. This SHOULD work, but if it doesn't then send the individual WM_SYSKEYDOWN and WM_SYSKEYUP instead.
* <Alt> + Special + ASCII:
WM_KEYDOWN for special keys, then WM_SYSCHAR (or key down and key up, if that doesn't work) with ASCII code, then WM_KEYUP for special keys.
* Special on its own:
WM_KEYDOWN and WM_KEYUP with VK_ code.
* <Alt> on its own:
WM_KEYDOWN and WM_KEYUP with VK_MENU (= <Alt>). Note that WM_SYSKEYxxx is for OTHER keys pressed with <Alt>, not <Alt> alone.

I have had difficulty getting the <Alt> combinations to work sometimes, but the theory is OK!

sally
May 24th, 1999, 07:17 AM
You must be kidding........ I hope....

Sally

Sally
May 24th, 1999, 07:17 AM
You must be kidding........ I hope....

Sally

Jason Teagle
May 24th, 1999, 07:21 AM
Kidding about what?

Sally
May 24th, 1999, 07:23 AM
So, if I have understood it correctly:

Sending Ctrl-1 is

SendMessage( WM_KEYDOWN, VK_CONTROL, 0 );
SendMessage( WM_KEYDOWN, '1', 0 );
SendMessage( WM_KEYUP, '1', 0 );
SendMessage( WM_KEYUP, VK_CONTROL, 0 );

correct??

Sally

Jason Teagle
May 24th, 1999, 07:43 AM
Essentially, yes; but not quite.

---// Repeat count is always 1 or more, for ANY key, ASCII-generating
// or otherwise.
SendMessage( WM_KEYDOWN, VK_CONTROL, 1 );

// These two could be replaced by a WM_CHAR (WM_SYSCHAR for <Alt>+) -
// as can all ASCII-generating keys and key combos.
SendMessage( WM_KEYDOWN, '1', 1 );
SendMessage( WM_KEYUP, '1', 0xC0000001 ); // Check out the help
// for this message.

SendMessage( WM_KEYUP, VK_CONTROL, 0xC0000001 );




---

Painful, isn't it?

sally
May 24th, 1999, 07:45 AM
Does Windows have to make it THAT complicated to send a simple keystroke?????

Sally

Sally
May 24th, 1999, 07:45 AM
Does Windows have to make it THAT complicated to send a simple keystroke?????

Sally

Jason Teagle
May 24th, 1999, 07:51 AM
It IS Microsoft. Say no more.

(If a Microsoft lawyer is reading this post, sit on this .|..).

sally
May 24th, 1999, 08:11 AM
OK, I give up
How do you get to be a Linux, Unix or a Mac programmer?

Sally

Sally
May 24th, 1999, 08:11 AM
OK, I give up
How do you get to be a Linux, Unix or a Mac programmer?

Sally

ericpan
May 24th, 1999, 10:38 PM
Oh,My God! How a discussion! Why don't you try my way:


HWND hWnd=::FindWindow(NULL, some window caption);
if (hWnd)
{
BYTE ks[1]=
{
VK_CTRL//if wanna to send a ALT+somekey, change to VK_MENU
};
DWORD tid=::GetWindowThreadProcessId(hWnd,NULL);
::SetKeyboardState(ks);
::AttachThreadInput(::GetCurrentThreadId(),tid,TRUE);
::BringWindowToTop(hWnd);
::PostMessage(hWnd,WM_KEYDOWN,someletter, 0);
::AttachThreadInput(::GetCurrentThreadId(),tid,FALSE);
}
However, I can't send the "shift+somekey", nor the combination of 3 keys, sush as "CTRL+SHIFT+somekey".

ericpan
May 24th, 1999, 10:47 PM
Hey, I am a faithful linux fan.I am now studying
the network and XWindow programming. Is there anybody interested in these?

June 29th, 1999, 03:08 AM
Thats all exciting ... but I tried to use your code to send WM_SYSKEYDOWN / -UP messages from a child to its parent (that is from a tab page to its containing dialog). I sent the message from out PreTranslateMessage() when receiving a WM_SYSKEYxxx for a system key that doesn't exist on this page. It doesn't do anything at all, even the WM_KEYDOWN message does not reach the parent. What am I doing wrong?

PreTranslateMessage( ... )
{
...
else if( ((pMsg->message == WM_SYSKEYDOWN) || (pMsg->message == WM_SYSKEYUP)) && (pMsg->wParam != VK_MENU) )
{
if( !IsSysKey(pMsg->wParam) ) // SysKey dieser Page ?
if( ((CPraxisDialog*)GetOwner()->GetOwner())->IsSysKey(pMsg->wParam) )
{
BYTE ks[1] = { VK_MENU };
GetOwner()->GetOwner()->SetFocus();
::SetKeyboardState( ks );
::AttachThreadInput( ::GetCurrentThreadId(), ::GetWindowThreadProcessId(GetOwner()->GetOwner()->m_hWnd, NULL), TRUE );
::BringWindowToTop( GetOwner()->GetOwner()->m_hWnd );

GetOwner()->GetOwner()->PostMessage( (pMsg->message == WM_SYSKEYDOWN)? WM_KEYDOWN : WM_KEYUP, pMsg->wParam, 0 );//pMsg->lParam );

::AttachThreadInput( ::GetCurrentThreadId(),
::GetWindowThreadProcessId(GetOwner()->GetOwner()->m_hWnd, NULL), FALSE );

return TRUE;
}
}

Andonic
November 11th, 2005, 04:09 AM
Hi All:

I hope this can help
Try this: (first of all you need the HWND of the window wich will receive the message, in this case ventana)

HWND ventana;
.....


::BringWindowToTop(ventana); //bring ventana to TOP
// simulate Alt + return
keybd_event(VK_MENU,
0,
0,
0);
keybd_event(VK_RETURN,
0,
0,
0);
keybd_event(VK_RETURN,
0,
KEYEVENTF_KEYUP,
0);
keybd_event(VK_MENU,
0,
KEYEVENTF_KEYUP,
0);

NotepadGuru
August 24th, 2008, 05:50 AM
Get the hwnd of the window you want to send the keys to (for example CWnd* pWnd = CWnd::FindWindow(NULL, "Untitled - Notepad"), and then post or send a WM_CHAR message to it. For example:

[ccode]
// Get the window that we want to send Ctrl+X to. In this example 'pWnd' will be set to
// the edit control in Notepad (dialog ID 0x0F). Change this.
CWnd* pWnd = CWnd::FindWindow(NULL, "Untitled - Notepad");
if (pWnd)
{
pWnd = pWnd->GetDlgItem(0x0F);
} else {
AfxMessageBox("Can't find notepad");
exit(0);
}
pWnd->SendMessage(WM_CHAR, 24, 0, 0); // 24 = Ctrl+X




Hope this helps.

Can you pls tell how did you get this 24 equelent fo Ctrl + X ?

GCDEF
August 24th, 2008, 09:07 AM
Since Shellreef hasn't posted for over 9 years, I wouldn't waste a lot of time waiting for a response. :ehh:

NotepadGuru
August 24th, 2008, 09:18 AM
oh, is this thread 9 years old, oh then they are now too old to post a reply :)