I can get a handle on the window I want to send the alt+F4 to but HOW do I send it?
Announcement
Collapse
No announcement yet.
send I A alt+F4
Collapse
X
-
Mark ---
Sub SendAltF4 (hwndClose&)
Local hwndact&, scan1&, scan2&
scan1& = MapVirtualKey(%VK_MENU, 0)
scan2& = MapVirtualKey(%VK_F4, 0)
hwndact& = GetForegroundWindow
SetForegroundWindow hwndClose&
KeyBd_Event %VK_MENU, scan1&, 0, 0: ApiSleep 20
KeyBd_Event %VK_F4, scan2&, 0, 0: ApiSleep 20
KeyBd_Event %VK_F4, scan2&, %KEYEVENTF_KEYUP, 0: ApiSleep 20
KeyBd_Event %VK_MENU, scan1&, %KEYEVENTF_KEYUP, 0: ApiSleep 20
SetForegroundWindow hwndact&
End Sub
-
But to imitate keyboard typing to transfer data from one application to another isn't a good solution.
Jeffrey Richter & Jonathan Locke in their book Windows 95 a Developers Guide say the
only foolproof method is to use Journal Playback.
They spend a number of pages explaining the problems using all other methods.
James
Comment
-
Thanks for the code Semen - it works great!
I am having difficulty obtaining the handle of the window to be sent the key-codes. If I use "GetForegroundWindow" function and the window is not in the foreground I get (naturally) the wrong handle!
Can *anyone* help me on this one? I need a sure-fire method of obtaining the handle.
TIA
William Fletcher
Comment
-
William --
If you know the exact window title of the target app, you can use the FindWindow API function. If you only know a partial title (like "Netscape - [name of site]) then the EnumWindows function will probably do the trick.
But...
Semen's code will not always work properly on Windows 98 and Windows 2000. Microsoft has intentionally "neutered" the SetForegroundWindow API function in those (and all future) versions of Windows, so if your program does not already "own" the foreground, the SetForegroundWindow function will not work the way it does in Windows 95 and NT4. If another program has the foreground when your program tries to use SetForegroundWindow, the only thing that will happen is that the app's Task Bar button will blink. The focus will not be changed.
In other words, even if you are able to obtain the window handle that you need, Windows will not allow you to use it in the way that you require. And the keys you send will end up being received by whichever app has the foreground.
-- Eric
-------------
Perfect Sync: Perfect Sync Development Tools
Email: mailto:[email protected][email protected]</A>
"Not my circus, not my monkeys."
Comment
-
Eric --
I used this code (under Win98SE) to close DIALOGs in own threads and didn't tried to send Alt-F4 to external application.
When you talk about possible troubles, do you mean "own" windows or "external" windows, or both ?
[This message has been edited by Semen Matusovski (edited January 04, 2000).]
Comment
-
Semen --
(You completely changed your message between the time I read it and the time I posted my reply. Here is a modified response.)
I am talking about windows that do not belong to my program, and (I believe) so is William. Even if he is talking about a window that his program owns, it does not matter. If another app has the foreground when that code is executed, you won't be able to send keys to "yourself", either.
If another application is currently in the foreground, your application cannot use SetForegroundWindow to "steal" the foreground for itself, and so it cannot then give it to another window. With Windows 98 and 2000, only the app that currently owns the foreground can use SetForegroundWindow to change the focus.
If I understood his message correctly, William said that he can't be sure which window will have the foreground when his app is run, so there is no way for him to reliably use SetForegroundWindow to give the foreground to the "target" app.
And even if he could do it once, it could not be repeated. Once you have given the foreground to another app, you cannot use SetForegroundWindow to take it back, or to give it to another window.
-- Eric
-------------
Perfect Sync: Perfect Sync Development Tools
Email: mailto:[email protected][email protected]</A>
[This message has been edited by Eric Pearson (edited January 04, 2000).]"Not my circus, not my monkeys."
Comment
-
Eric --
Sorry, I too often correct a text of message. It very easy to lose a track.
Of course, William should exactly explain, which window he want to close, and in which situation.
But I imagine situation so: user clicks any button on my DIALOG and I execute his request by closing some windows.
When user clicks, my application has foreground or not ?
Comment
-
Thank you Eric - FindWindow seems a likely candidate.
I am actually writing a "wrapper" for another program which I want to send the keystrokes to. First I asynchronously Shell to the other pgm, then I offer my wrapper (dialog) to selectively send keystrokes via a mouse-menu (buttons). Believe it or not, it really comes in handy. I could never rewrite the original, but the wrapper enhances its useability for me.
Can you clue me in to what I should use for the ASCIIZ "ClassName" ? Otherwise, I'll use the Window's title in its Caption for "WindowName" - right?
Many thanks
William Fletcher
Comment
-
Semen --
> When user clicks, my application has foreground or not ?
Yes, in that case it should work properly. And if the target window responds to the keystroke(s) by closing, the foreground would then be returned to your application. But William's message sounds (to me) like he is attempting to send keys to another app which may or may not have the foreground when the key-send operation is executed.
We should probably stop speculating and let William tell us more about what he needs to do...
-- Eric
-------------
Perfect Sync: Perfect Sync Development Tools
Email: mailto:[email protected][email protected]</A>
"Not my circus, not my monkeys."
Comment
-
...which he apparently did in the time that it took me to post my previous response, since his most recent message appears before mine. {G}
Try this...
Code:DIM sWindowTitle AS LOCAL STRING DIM hTargetWindow AS LOCAL LONG sWindowTitle = "app to find" hTargetWindow = FindWindow("", BYVAL STRPTR(sWindowTitle))
-------------
Perfect Sync: Perfect Sync Development Tools
Email: mailto:[email protected][email protected]</A>
[This message has been edited by Eric Pearson (edited January 04, 2000).]"Not my circus, not my monkeys."
Comment
-
Would'nt you know it ... I'm going to have to use the EnumWindows method because the Caption of the window is terribly complex and I cannot tell if they use 1 or 2 spaces, etc plus it has a Copyright symbol and I'm not sure exactly which character that is. Al-in-all its about 50+ convoluted characters long!
So Eric ... I'm digging in the Api manuals ... if you can shed any light it would be appreciated (again!). I know how the Caption starts (like "Netscape" ...)
Thanks!
William Fletcher
Comment
-
William --
I'll put something together this afternoon and post it in the Source Code forum.
-- Eric
-------------
Perfect Sync: Perfect Sync Development Tools
Email: mailto:[email protected][email protected]</A>
"Not my circus, not my monkeys."
Comment
-
William --
It's there now. Let me know if you have any questions.
-- Eric
-------------
Perfect Sync: Perfect Sync Development Tools
Email: mailto:[email protected][email protected]</A>
"Not my circus, not my monkeys."
Comment
Comment