Sign in to follow this  
Followers 0
leuce

Make While...WEnd even faster

10 posts in this topic

G'day everyone

I wrote a little script to prevent windows and dialogs from stealing the focus of the current window.

The script identifies the current window and keeps activating it, but when the user clicks on or near the taskbar, I assume the user might have changed the current window, and then the script re-identifies the current window. However, I find that the script is not as responsive as I had hoped, and I have to press down on the mouse button (on the taskbar) for almost a second before the script recognises the click.

#Include <Misc.au3>

$dll = DllOpen("user32.dll")

Sleep ("2000")

Global $hWndActive = WinGetHandle("[ACTIVE]") ; get active window's handle

Global $taskbar = @DesktopHeight - 200

While 1

If _IsPressed("01", $dll) Then ; if left-click
Local $leftclickpos = MouseGetPos ()
If $leftclickpos[1] > $taskbar Then ; if left-click was on or near taskbar
Sleep ("3000") ; wait 3 seconds
Global $hWndActive = WinGetHandle("[ACTIVE]") ; get active window's handle
EndIf
EndIf

If WinExists ($hWndActive) Then ; does previous active window still exist?
WinActivate($hWndActive) ; activate it
Else
Global $hWndActive = WinGetHandle("[ACTIVE]") ; get active window's handle
EndIf

WEnd

DllClose ()

How can I make the script more responsive?

Thanks

Samuel

Share this post


Link to post
Share on other sites

Shorten the ridiculously long Sleep(3000)? How about Sleep(10)?

:idea:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

A little tip, When using "sleep" the common strategy is to use a suitable value that doesn't slow down what you want to do and also doesn't use up much CPU. It is crucial to find the appropriate value or your application will be rendered useless, practically.


[Not using this account any more. Using "iShafayet" instead]

Share this post


Link to post
Share on other sites

I wouldnt say 200 pixels was near the taskbar either, for me its more than quarter way up the screen

But with regards your problem I would say add a short sleep like 10 or something.

It needs to be just after the while, or just befor the wend.

Im not sure PsaltyDS really looked where your sleep was :idea:


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Shorten the ridiculously long Sleep(3000)? How about Sleep(10)?

On my computer (perhaps not on yours) the change from one window to another when clicking a tab on the taskbar is not instantaneous. Depending on how slow the computer is, or how sluggish the particular window is, it may take a second-and-a-half to two seconds before the clicked tab becomes a window (though a second is the norm). If the wait time (before the script re-identifies the "current" window) is too short, the script will identify the wrong window as the one that the user wants to be active.

Have you actually tried this script? I don't see how reducing the Sleep time will affect whether the script recognises the mouse-click. My problem is that unless I hold down the mouse button for about a second (when clicking the taskbar), the script does not recognise the mouse-click (and so when I click another window, even on the taskbar, the script immediately makes the previous window the current one again).

Share this post


Link to post
Share on other sites

A little tip, When using "sleep" the common strategy is to use a suitable value that doesn't slow down what you want to do and also doesn't use up much CPU. It is crucial to find the appropriate value...

I'm using Sleep specifically to stop the script's operation for 3 seconds. The mouse-click recognition should take place *before* the Sleep. The Sleep should not affect the script's responsiveness, as far as I can see.

As for the appropriate value, well, there isn't a single value. Some windows mimimise/maximise very quickly and some do it slowly. Some windows wait half a second before responding (that is normal on Microsoft Windows, no matter how fast your computer). So the script must wait long enough for the new window to become the "front" window before it identifies that window as the current one.

Share this post


Link to post
Share on other sites

I wouldnt say 200 pixels was near the taskbar either, for me its more than quarter way up the screen.

:-) Well, when this script becomes public, I'll figure out a way to determine where a user's taskbar is (perhaps even ask the user himself, heh-heh). For now, the 200 pixels needn't be precise, as long as it's more than enough (and not less than enough).

But with regards your problem I would say add a short sleep like 10 or something.

It needs to be just after the while, or just befor the wend.

If that is the case, then it means I can have only one If...Then loop in the While...WEnd loop, because the Sleep has to be inside the If...Then loop. I don't want the script to Sleep with *every* loop -- I want it to Sleep only if the conditions of the If...Then loop are met.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I wrote a little script to prevent windows and dialogs from stealing the focus of the current window.

Here's how to test the script on your computer:

1. Open at least three different programs, and tile their windows (i.e. resize the windows so that they're not maximised and you can see parts of all of them). Run the script, and click on one of the windows. The script will identify this window as the one that should remain on top.

2. Test 1: Click on any other window (which normally brings it to the front). The script should instantly bring the previous window to the front.

3. Test 2: Click on the current window's icon in the taskbar (which normally minimises it). The script should allow the window to be minimised (in my case, the script instantly restores the window and does not allow it to be minimised, unless I hold down the mouse-button for about 1 second).

4. Test 3: Click on the current window's icon in the taskbar (which normally minimises it). The script should allow the window to be minimised (in my case, the script instantly restores the window and does not allow it to be minimised, unless I hold down the mouse-button for about 1 second).

5. Test 4: Click on any other window's icon in the taskbar (which normally brings it to the front). The script should allow that window to be brought to the front and stay in front (in my case, the script instantly brings the previous window back to the front and does not allow me to start using the newly selected window, unless I hold down the mouse-button for about 1 second).

By the way, I know there are programs that allows the user to "stick" any window to the front, or that prevents other windows from stealing the focus (which is what my script is for), but those programs typically only work for existing windows. I want a program that disallows *any* focus stealing, even from new windows or from windows that pop up error message dialogs.

Edited by leuce

Share this post


Link to post
Share on other sites

If that is the case, then it means I can have only one If...Then loop in the While...WEnd loop, because the Sleep has to be inside the If...Then loop. I don't want the script to Sleep with *every* loop -- I want it to Sleep only if the conditions of the If...Then loop are met.

Yes but the actual while--wend loop has no sleep in it and is probably maxing your cpu, and thus not registering your keystroke quick enough

Try altering your loop

While 1
   Sleep(10)
   If _IsPressed("01", $dll) Then ; if left-click
      Local $leftclickpos = MouseGetPos ()
      If $leftclickpos[1] > $taskbar Then ; if left-click was on or near taskbar
         Sleep ("3000") ; wait 3 seconds
         $hWndActive = WinGetHandle("[ACTIVE]") ; get active window's handle
      EndIf
   EndIf
   If WinExists ($hWndActive) Then ; does previous active window still exist?
      WinActivate($hWndActive) ; activate it
   Else
      $hWndActive = WinGetHandle("[ACTIVE]") ; get active window's handle
   EndIf
WEnd

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

G'day everyone

I could not get my script to work fast enough, so I had to go with Plan B, which is changing focus using a hotkey. I was rather hoping to get away without using a hotkey, but at least the script works for me and it may be useful for users in the wild.

Attached please find the final script (it is also on my web site, with EXE file). I call it FickyFocus, and its purpose is to prevent focus stealing.

Thanks for all your comments!

Samuel

fickyfocus (without exe file).zip

Edited by leuce

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0