|
-
Oct 13th, 2005, 03:04 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Sending keys to previous window... ?
Ok, i know how to sendkeys, thats easy, say if i wanted Control + F i would do
Thats simple enough, BUT i need to send Ctrl + F to the last active window, would fire the Find dialog.
How would i go about sending the keys to the previously active window ?
Note: My application will be run from a shortcut key on a keyboard and this is why its needed to send its keys to the previous window, it Unloads itself after its task is complete.
-
Oct 13th, 2005, 03:15 PM
#2
Re: Sending keys to previous window... ?
Doubtful you could get the previously activated window without your program being open to detect it.
Or is your program open and only activated by the keyboard?
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Oct 13th, 2005, 03:21 PM
#3
Thread Starter
Frenzied Member
Re: Sending keys to previous window... ?
 Originally Posted by chemicalNova
Doubtful you could get the previously activated window without your program being open to detect it.
Or is your program open and only activated by the keyboard?
chem
No, i'm afraid its opened entirely from the keyboard, i thought there might be some way of getting the previous hWnd property...
Are you sure theres no known workaround for this ?
If not i might need to rethink it...Hmm.
EDIT: I've got another idea here, i can sendkeys Alt n Tab, record the hWnd of the currently active program, then send Ctrl + F to it, then close my program, do you think this could work ?
Also, if you do, could you show me how... hehe.
Not too sure on sending keys to other windows you see...
Last edited by thegreatone; Oct 13th, 2005 at 03:26 PM.
Zeegnahtuer?
-
Oct 13th, 2005, 03:25 PM
#4
Hyperactive Member
Re: Sending keys to previous window... ?
Like This
VB Code:
AppActivate "Title of the window to activate"
DoEvents
Sendkeys "^F", 0
AppActivate allows you to put focus on any application window as long as you know what is on title bar.
Of course it is TOTALLY unreliable, but it does get the job done in a quick and dirty way.
-
Oct 13th, 2005, 03:36 PM
#5
Hyperactive Member
Re: Sending keys to previous window... ?
If you are going to be controling other apps through sendkeys and appactivate be sure to also include the sleep lib
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
and use liberal amounts of sleep and doevents.
-
Oct 13th, 2005, 04:31 PM
#6
Thread Starter
Frenzied Member
Re: Sending keys to previous window... ?
Help with the following code, it doesn't seem to return the previous programs titlebar...
VB Code:
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function FlashWindow Lib "user32" (ByVal hwnd As Long, ByVal bInvert As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Sub Form_Activate()
Dim MyStr As String
Dim PrevhWnd As Long
SendKeys "%" & vbKeyTab
Sleep (500)
PrevhWnd = GetForegroundWindow() 'Find out what window has focus
SetForegroundWindow PrevhWnd
MyStr = String(GetWindowTextLength(PrevhWnd) + 1, Chr$(0))
GetWindowText PrevhWnd, MyStr, Len(MyStr)
'Text1.Text = PrevhWnd
'ShowWindow PrevhWnd, 6
Sleep (250)
MsgBox MyStr
AppActivate MyStr
DoEvents
SendKeys "^F", 0
'SendKeys "^F", 1
'Unload Me
End Sub
-
Oct 13th, 2005, 06:52 PM
#7
Re: Sending keys to previous window... ?
Chucking it in Form_Load does the job for me..
Should brush up on your Windoez Messages and the order they are executed in 
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Oct 14th, 2005, 04:06 AM
#8
Thread Starter
Frenzied Member
Re: Sending keys to previous window... ?
 Originally Posted by chemicalNova
Should brush up on your Windoez Messages and the order they are executed in 
chem
Explain what you mean there Chem...
I'll test this is Form_Load when i get back home in around 5 hours...
Thanks for the help.
-
Oct 14th, 2005, 09:40 AM
#9
Thread Starter
Frenzied Member
Re: Sending keys to previous window... ?
Ok, back home, everything seems to work fine (Placed code in Load Event)
Such a nice workaround for getting the previous hWnd. I'm still sure theres a better way though.
For now, resolved.
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
|