[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.
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
Re: Sending keys to previous window... ?
Quote:
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...
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.
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.
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
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 :D
chem
Re: Sending keys to previous window... ?
Quote:
Originally Posted by chemicalNova
Should brush up on your Windoez Messages and the order they are executed in :D
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.
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.