AutoHotkey Community

It is currently May 26th, 2012, 6:46 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 13 posts ] 
Author Message
PostPosted: June 18th, 2009, 5:55 pm 
Offline

Joined: June 18th, 2009, 4:39 pm
Posts: 19
Location: Salt Lake City, Utah
Hi everyone,

I've been using AutoHotKey for a couple years now and I am just starting to get into moderate coding. I am stumped with one particular annoyance.

In my script, I have a lot of hotkeys similar to the following:

Code:
#+4:: Run %USERPROFILE%,,UseErrorLevel


Sometimes when I open a program or particularly a new explorer window, the new window will appear unfocused and behind my currently focused window. Other times, it will come up as the active window. I want new windows to always appear in the foreground as the active window. I've tried using WinActivate with marginal success.

My question: Are there any other ways to guarantee that a new window is activate after I execute a hotkey?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 18th, 2009, 6:08 pm 
Offline
User avatar

Joined: May 24th, 2009, 5:35 am
Posts: 2099
Location: Iowa, USA
I've had issues with focusing on a particular window - typically it seemed to be a timing issue when using WinActivate. There's probably a better answer, but I just used WinActivate 2 or 3 times.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 18th, 2009, 6:12 pm 
Offline

Joined: June 18th, 2009, 4:39 pm
Posts: 19
Location: Salt Lake City, Utah
Haha interesting. I've noticed that if the window comes up in the background, I can press the hotkey a second time and it'll pop up how I want it.

I was messing around with WinWait, but I wasn't able to get satisfactory results across multiple types of windows and programs.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 18th, 2009, 6:19 pm 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
I have same issues sometimes, but only explorer.exe?
I thought it is related to XP or explorer.exe.
Have you got same problems with another app?

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 18th, 2009, 6:50 pm 
Offline

Joined: June 18th, 2009, 4:39 pm
Posts: 19
Location: Salt Lake City, Utah
Yes.

One that comes to mind is Prism (From Mozilla). When I start a Prism web applications via hotkey, they always start up in the background. But like I said, once it is started, I can press the hot key again and it'll bring the window to the front and in focus.

Explorer is hit and miss with the focus/background thing and I want to say there are some other applications that are like that as well, but I can't think of any off the top of my head.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 18th, 2009, 7:12 pm 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
kylirh wrote:
When I start a Prism web applications via hotkey, they always start up in the background. .


Can you try following code after you pressed your hotkey?.
Possibly sometimes another (hidden) window pops up?
Code:
Loop 3
   Loop 100
   {
      WinGetTitle,var%A_Index%,A
      test .= "`n" . var%A_Index%
      ToolTip % test
      If A_Index in 20,40,60,80,100
         test=
   }

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 18th, 2009, 7:24 pm 
Offline

Joined: June 18th, 2009, 4:39 pm
Posts: 19
Location: Salt Lake City, Utah
When I run that code snippet after hotkey strokes, it usually shows the currently focused window and then when (and if) the target window becomes focused, it'll switch the title to the new application.

With the Prism launcher, it never switches titles and only lists the currently focused window. With Explorer, it'll immediately say "Windows Explorer" and then it shows the folder name that opened.

Having a different window momentarily steal the focus makes sense.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 18th, 2009, 7:38 pm 
Offline

Joined: June 18th, 2009, 4:39 pm
Posts: 19
Location: Salt Lake City, Utah
I found a program, other than Explorer, that behaves erratically with the focusing. That is the built in calculator (C:\Windows\system32\calc.exe). Sometimes when I run it, it'll come up in focus, other times it won't.

I wrote this earlier today to see if it would fix the problem, and it has to some degree, but not entirely:

Code:
RunAndFocus(Command)
{
   Run %Command%,,UseErrorLevel
   WinActivate
}


I tried running WinActivate 3 times in a row, like jethrow suggested, but the calculator still came up unfocused.

Is it possible that something else in my AHK script is preventing the focusing from happening? (It is ~1300 lines long)

Note: Post modified to change member information per member request. ~ sinkfaze (07/07/2011)


Last edited by kylirh on June 18th, 2009, 7:51 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 18th, 2009, 7:50 pm 
Offline

Joined: June 18th, 2009, 4:39 pm
Posts: 19
Location: Salt Lake City, Utah
Ah-ha!

At the top of my script, I have:

Code:
#UseHook On


If I remove it, the focusing issue for the calculator, at least, goes away and it comes up completely focused like I want it.

I added the #UseHook thing previously to get some stuff to work correctly, but now I don't remember what it does and the documentation for it doesn't make sense.

Anyone care to explain what UseHook is doing? Any ideas why removing it resolves the focusing issue?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 18th, 2009, 10:25 pm 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
I cannot reproduce the problem :?
I do not think it is related to hook.
Code:
#UseHook
c::
Run calc.exe
Return


Regarding hook see here

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 18th, 2009, 11:22 pm 
Offline

Joined: June 18th, 2009, 4:39 pm
Posts: 19
Location: Salt Lake City, Utah
Yeah, you're right. The hook isn't the real problem. It was definitely doing something with the Calculator program for sure, but even with it off some of my windows occasionally are still coming up unfocused.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 1st, 2009, 3:21 pm 
I had this same problem when launching FireFox. I found this post while looking for a solution. Eventually I found it by using "WinWait". Still not sure why it's launching w/o focus, but at least there is an automated way to give it the focus after it is launched... nearly the same thing. Here's my example of my firefox launcher bound to the Win-F key combo with the WinWait. Hope it helps.

Code:
#f::
IfWinExist Mozilla Firefox
     WinActivate
else
     Run "C:\Program Files\Mozilla Firefox\firefox.exe"
     WinWait Mozilla Firefox
     WinActivate
return


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: July 1st, 2009, 6:59 pm 
Offline

Joined: June 18th, 2009, 4:39 pm
Posts: 19
Location: Salt Lake City, Utah
Interesting. I'll see if I can get that to work. In my case I think it'll be a little more complex, but I'll post a follow up.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: dra, rbrtryn and 67 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group