|
-
May 21st, 1999, 10:00 PM
#1
Help! How to send a CTRL+X key to another window?
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
-
May 21st, 1999, 10:47 PM
#2
Re: Help! How to send a CTRL+X key to another window?
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
#3
Re: Help! How to send a CTRL+X key to another window?
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.
-
May 22nd, 1999, 01:42 PM
#4
Re: Help! How to send a CTRL+X key to another window?
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.
-
May 22nd, 1999, 01:46 PM
#5
Re: Help! How to send a CTRL+X key to another window?
It's the 24th letter in the alphabet.
Ctrl+letter returns the position of 'letter' in the alphabet.
-
May 23rd, 1999, 03:24 PM
#6
Re: Help! How to send a CTRL+X key to another window?
-
May 23rd, 1999, 10:29 PM
#7
Re: Help! How to send a CTRL+X key to another window?
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
#8
Re: Help! How to send a CTRL+X key to another window?
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".
-
May 24th, 1999, 04:24 AM
#9
Re: Help! How to send a CTRL+X key to another window?
<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.
-
May 24th, 1999, 04:25 AM
#10
Re: Help! How to send a CTRL+X key to another window?
Oops, that should have been VK_CONTROL, not WK_CONTROL. Stupid keyboard.
-
May 24th, 1999, 04:39 AM
#11
Re: Help! How to send a CTRL+X key to another window?
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!
-
May 24th, 1999, 07:17 AM
#12
Re: Help! How to send a CTRL+X key to another window?
You must be kidding........ I hope....
Sally
-
May 24th, 1999, 07:21 AM
#13
Re: Help! How to send a CTRL+X key to another window?
-
May 24th, 1999, 07:43 AM
#14
Re: Help! How to send a CTRL+X key to another window?
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?
-
May 24th, 1999, 07:45 AM
#15
Re: Help! How to send a CTRL+X key to another window?
Does Windows have to make it THAT complicated to send a simple keystroke?????
Sally
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|