The handle is correct since I can read the title of the window and display the same in a messagebox
Amit
Main Topics
Browse All TopicsHi,
I am facing a problem simulating a keypress by a sendmessage
I wish to simulate pressing ALT+C to a particcular window
Actually the window has a hyperlink which has a shortcut as ALT+C
I have a handle to that window, and wish to send ALT+C to this window
Hence i tried sending the following messages
SendMessage ( handle, WM_SYSKEYDOWN, 18, 540540929) ;
SendMessage ( handle, WM_SYSKEYDOWN, 67, 539885569) ;
SendMessage ( handle, WM_SYSCHAR, 99, 539885569) ;
SendMessage ( handle, WM_SYSKEYUP, 18, -1070071807 ) ;
SendMessage ( handle, WM_KEYUP, 67, -1070727167) ;
but it does not work.................If I create a dummy window (using basic SDK programming) and process the keyboard messages writing them to a file, I am able to receieve all these message..........But the messages do not seem to work work on the actual window I want them to work on............ON clicking on the hyperlink OR pressing ALT+C, the window opens a new window
Any help would be greatly appreciated
I posted a similar question (but it was entitlled HOOks, hence posting it again)...
http://www.experts-exchang
I shall award points for the both the questions to the answerer
Amit
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
i think you used something like GetForegroundWindow or EnumWindows to get the handle.
the bad news is, that if i'm right, you got some hide-and-seek to pley.
try to use EnumWindowEx to find all sub windows of your current window handle.
iteratively send your message to the first, second, third and so on window, maybe in a loop
with a delay in it, so you can see when it works.
http://msdn.microsoft.com/
http://msdn.microsoft.com/
I have another aspect:
SendMessage ( handle, WM_KEYUP, 67, -1070727167) ;
what is handle? or, more exact: where does handle point to?
maybe you sent your messages to the wrong window or to the nirvana...
you can try this:
handle = GetForegroundWindow();
or this
handle = GetDesktopWindow(); // I don't know it the function exist with THIS name ?!?
regards
void_main
void_main,
handle points to the destination window
I have confirmed its correct by getting the titke of that window using handle
Also, i am not sure about using GetForeGroundWindow().....
I don't think so
Amit
Another idea: Will the other app recognize the key stroke ony using WM_KEYUP or should you mimic the entire sequence it would get when you press ALT+C:
WM_(SYS)KEYDOWN for ALT, WM_KEYDOWN for C, WM_KEYUP for C, WM_(SYS)KEYUP for ALT. I would think handling should act on xx_KEYDOWN not on keyup.
Or are the calls you gave in the first post that sequence?
I am sure you've all looked into a help file. This is from borland c++ builder 6.0:
You don't have to read everything (I copied everything to deliver as much information as possible)
Please look at "remarks"
-----------------------
The SendMessage function sends the specified message to a window or windows. The function calls the window procedure for the specified window and does not return until the window procedure has processed the message. The PostMessage function, in contrast, posts a message to a thread's message queue and returns immediately.
LRESULT SendMessage(
HWND hWnd, // handle of destination window
UINT Msg, // message to send
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
Parameters
hWnd
Identifies the window whose window procedure will receive the message. If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.
Msg
Specifies the message to be sent.
wParam
Specifies additional message-specific information.
lParam
Specifies additional message-specific information.
Return Values
The return value specifies the result of the message processing and depends on the message sent.
Remarks
Applications that need to communicate using HWND_BROADCAST should use the RegisterWindowMessage function to obtain a unique message for inter-application communication.
If the specified window was created by the calling thread, the window procedure is called immediately as a subroutine. If the specified window was created by a different thread, Windows switches to that thread and calls the appropriate window procedure. Messages sent between threads are processed only when the receiving thread executes message retrieval code. The sending thread is blocked until the receiving thread processes the message.
-----------------------
maybe it helps a bit
void_main
void_main,
I had read this in msdn.....but it does not help much because as I said, I am able to send messages to a window (created using basic windows SDK programming) and print those messgaes there
But i am not able to simulate the keyboard keypress OR mouse click
SteH,
I tried with PostMessage as well......no use
Also, I am not using MFC, hence cannot use CWND::SendMessgae.........
Amit
Fellas! you miss one thing. If you get window caption by window handle it is wrong window to copy/paste it is overlapped/MDI/Dialog window but is not window where you want to get text. handle must be retrieved only GetFocus function. GetFocus gives handle of window where real text is placed. Moreover Did you select text to copy?
MikiProg,
i'm sure it was ALT+C not CTRL+C
Sys_Prog,
I found that:
http://www.aivaliotis.com/
"its a call from user32.dll and it works on every windows"
have a nice weekend!
void_main
Perhaps the article
http://www.codeproject.com
can give some insights on how to use user32.dll for simulating key stroke.
PS: I can't get the webpage of M Aivaliotis neither. It seems to be his server.
If your sure you have the correct handle try using SetActiveWindow or SetFocus then send your keyboard message.
EnumWindows is unreliable unless you know your target is going to have a specific title and/or classname.
I tried to use EnumWindows for controlling ShellExecute untill I discovered WaitForSingleObject thing.. I even use FlashWindow in a loop just to flash a window to prove I had the correct handle.
RJ
Okay: Here's the text from the link:
Simulate Keypress
If you've ever wanted to programatically fire a keboard event then I have just the VI for you. This code makes a call to the Windows API calling a function in user32.dll available in all Windows versions. This function allows you to specify any key on the keyboard. When the VI is run, Windows will think the selected key has been pressed.
Looking at the function description available from MSDN we see the following:
keybd_event Function
--------------------------
The keybd_event function synthesizes a keystroke. The system can use such a synthesized keystroke to generate a WM_KEYUP or WM_KEYDOWN message. The keyboard driver's interrupt handler calls the keybd_event function.
Syntax
VOID keybd_event(BYTE bVk,
BYTE bScan,
DWORD dwFlags,
PTR dwExtraInfo
);
Parameters
bVk
[in] Specifies a virtual-key code. The code must be a value in the range 1 to 254. For a complete list, see Virtual-Key Codes.
bScan
This parameter is not used.
dwFlags
[in] Specifies various aspects of function operation. This parameter can be one or more of the following values.
KEYEVENTF_EXTENDEDKEY
If specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).
KEYEVENTF_KEYUP
If specified, the key is being released. If not specified, the key is being depressed.
dwExtraInfo
[in] Specifies an additional value associated with the key stroke.
Return Value
This function has no return value.
There are two inputs we need to be concerned about. The bScan and dwFlags. For the bScan we need to input the number which represents the key on your keyboard. This number is also available from MSDN. I won't display the complete list since you can retrieve it online from MSDN but I have included the complete list on the diagram of the VI.To make it easier during programming, I've created an enum control for all the possible keyboard values (0-254).
Another input of concern as mentioned above is the dwFlags. This must be toggled between 0 and 2 to indicate that a key is either depressed or released. This is especialy usefull with the ctrl or shift keys. I've created the following diagram and front panel for the above function.
(source: http://www.aivaliotis.com/
void_main
Stop here! When searching answer I loose myself in question. Lets go step by step.
>I wish to simulate pressing ALT+C to a particcular window.................If I create a dummy window (using basic SDK.....
I see you do it well.
>Actually the window has a hyperlink which has a shortcut as ALT+C
Hyperlink is a IHlink COM object that can not have shortcut. Thus shortcut ALT+C is shortcut to anything else that can have shortcuts. How do you realize that?
>>Hyperlink is a IHlink COM object that can not have shortcut. Thus shortcut ALT+C is shortcut to anything else that can have shortcuts. How do you realize that?
MikProg,
Just try NetMeeting using MSN Messenger
Login in to two MSN messenger accounts on two PC's
From one PC, Start a NetMeeting session and select the other users name and click OK
A request would be send to the other user [A window is shown to other user which has Accept/Decline as Hyperlinks which also have k/b shortcuts like ALT+C for Accepting and .......(i dont remember) for Declining
Amit
I'm shure in this case ALT-C combo processed elsewhere but not window function. Hot keys are created in windows in several ways. But mostly common is accelerators tables. Native windows application hides accelerators tables deep inside. The main idea of AT is catch keyboard input message, substitute by WM_COMMAND with predefined parameters and send WM_COMMAND to destination. This process takes place inside message loop so the only way is to send messages that emulates keyborad input to thread that owns destination window.
var
threadid, dw: DWORD;
begin
threadid:=GetWindowThreadP
PostThreadMessage(threadid
... more messages to send to emulate ALT-C
end;
i don't know if the above is correct or not, but i used the following to send and enter key to a window (its kinda simple, coz i didn't have to dela with the menu ... etc --- isn't there a direct shortcut to wht u are trying to do instead of first clcking on the menu ??)
---------
SetForegroundWindow(handle
keybd_event(VK_RETURN,0,0,
---------------
Business Accounts
Answer for Membership
by: drnickPosted on 2004-05-06 at 21:49:22ID: 11012051
hm, it seems to me like you don't have the correct window handle and be sending
the messages to the wrong window.