Alt-tab using middle mouse button ONLY Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SJN

Alt-tab using middle mouse button ONLY

21 Mar 2017, 10:26

Hi All,

I'm trying to make the Middle Mouse Button fire Alt-Tab events, but in such a way that the activation is automatically made after the middle mouse button has been idle for a while.
This to allow scrolling through the Alt Tab menu by repetively clicking the middle mouse button, and having the selection activated by not clicking again for say 2 secs.
I tried several things, including forcecully restarting the script, but it doesn't give the desired result.
Then I came across the built-in AltTabMenu and AltTabMenuDismiss functions, but getting AltTabMenuDismiss fired after 2 secs doesn't work (function can only be hooked to a hotkey one-liner).

Something I tried at the start (which I thought would work: the script restarts, keeps pressing alt, and fires another tab - but unfortunately the re-firing doesn't work well):

Code: Select all

#SingleInstance force
MButton::
Send {Alt down}{Tab} <-- this could also be split into two sentences, also didn't work
Sleep 2000
Send {Alt up}
return
Other things I tried are (which I also thought was going to work):

Code: Select all

#SingleInstance force
MButton::
Send {LAlt down}
Loop
{
	Send {TAB}
	Sleep 1000
	if not GetKeyState("MButton", "P")
		break
}
Send {LAlt up}
I hope someone has a clever idea.
I must admit I only started with AutoHotKey 2 hrs ago... A great tool though!!! :bravo:
SJN

Re: Alt-tab using middle mouse button ONLY

21 Mar 2017, 10:34

Also tried this, but doesn't work:

Code: Select all

#SingleInstance force
MButton::
Send {LAlt down}
Send {Tab}
Sleep 2000
Send {LAlt up}
return

!MButton:: <-- fires only if the button is pressed whilst Alt is down - so to allow scrolling
Send {Tab}
SJN

Re: Alt-tab using middle mouse button ONLY

21 Mar 2017, 10:42

I think this is a great and simple start as explained above, but the latter part of activating the selection after x seconds is the challenge:

Code: Select all

#SingleInstance force
MButton::AltTabAndMenu
This had potential, but also doesn't work because the lines below the single-line hotkey are never fired,
and the AltTab or AltTabAndMenu cannot be fired as 'commands' in multi-line hotkey scripts.

Code: Select all

#SingleInstance force
MButton::AltTabAndMenu
Sleep 2000
Send {LAlt up} <-- or {Alt up} if you wish...
return
4GForce
Posts: 553
Joined: 25 Jan 2017, 03:18
Contact:

Re: Alt-tab using middle mouse button ONLY

21 Mar 2017, 12:15

Try this ...

Code: Select all

#singleinstance force

IdleDelay := 2000

MButton::
	Send {Alt down}
	Send {Tab}
	WinWaitActive ahk_class TaskSwitcherWnd
	SetTimer TaskSwitcherIdle, -%IdleDelay%
Return

!MButton::
	Sleep 100		; I can't explain why but it won't work without this delay
	Send {Tab}
	SetTimer TaskSwitcherIdle, -%IdleDelay%
Return

TaskSwitcherIdle:
	Send {Alt up}
Return
SJN

Re: Alt-tab using middle mouse button ONLY

22 Mar 2017, 02:40

Thanks - that works!
I was interested by the Sleep requirement, and the check-if-window-is-active expression.
After digging, I found the check-if-window-is-active isn't needed.
Instead of the sleep, one could use {Blind}{Tab} or even {Alt down}{Tab} in the !MButton part.
I saw that even sending something completely strange in front (a single character 's' before {Tab} for example) already works.

The Timer was interesting - apparently interrupting a sleeping script with '#singleinstance force' isn't really effective.
The timer solution though does exactly what the sleep was ought to do: wait before executing the command, unless the script terminates (reloads) before that.

This is what I made of it - thanks a lot for your input!

Code: Select all

#singleinstance force
iIdleDelay := 2000

MButton::
	Send {Alt down}{Tab}
	SetTimer OnIdle, %iIdleDelay%
	Return

!MButton::
	Send {Blind}{Tab}
	SetTimer OnIdle, %iIdleDelay%
	Return

OnIdle:
	Send {Alt up}
	Return
SJN

Re: Alt-tab using middle mouse button ONLY

22 Mar 2017, 02:58

...And we should not forget the following in the Timer routine! :mrgreen:

Code: Select all

SetTimer OnIdle, Off
I have one remaining question:
Without using timers for manuall detecting short or long presses, is there a build-in way in Hotkey declarations to configure the action only to fire upon LONG PRESS of a button or key?
I.e. I'm looking for a way to make the left and right mouse buttons do something (e.g. trigger a scroll action), but only when one of them is HELD DOWN. Short presses should just do a normal mouse click.
In the meantime I'll try and use a timer, or make the combination of a left and right mouse button trigger the action only.
SJN

Re: Alt-tab using middle mouse button ONLY

22 Mar 2017, 04:20

The below seems to work great!

Code: Select all

; holding down middle mouse button whilst clicking left mouse button triggers scrolling up
MButton & LButton::Click WheelUp

; holding down middle mouse button whilst clicking right mouse button triggers scrolling down
MButton & RButton::Click WheelDown
I've now also expanded the alt-tab features to allow moving left in the task switcher window upon clicking the left mouse button.
The entire code looks like this now and works perfect:

Code: Select all

#singleinstance force
iIdleDelay := 2000

; initiate the alt-tab menu upon first middle mouse button click
MButton::
	Send {Alt down}{Tab}
	SetTimer OnIdle, %iIdleDelay%
	Return

; send tabs to move forward in the menu upon further middle mouse button clicks
!MButton::
	Send {Blind}{Tab}
	SetTimer OnIdle, %iIdleDelay%
	Return

; send left tabs to move backward in the menu upon left mouse button clicks
!LButton::
	Send {Blind}+{Tab}
	SetTimer OnIdle, %iIdleDelay%
	Return

; activate the selection after the set idle time
OnIdle:
	Send {Alt up}
	SetTimer OnIdle, Off
	Return

; holding down middle mouse button whilst clicking left mouse button triggers scrolling up
MButton & LButton::Click WheelUp

; holding down middle mouse button whilst clicking right mouse button triggers scrolling down
MButton & RButton::Click WheelDown
You might wonder why this is needed.
The target is a control station which has no keyboard or mouse, only a trackball, with three hardware buttons (L/M/R mouse button).
The users need to alt-tab in a more straight-forward way than was possible so far; plus the users want to be able to scroll (but have no easy way for doing so yet).
The above code solves both quests, and I think can work quite user-friendly. Lets see what the end users thinks :mrgreen:
4GForce
Posts: 553
Joined: 25 Jan 2017, 03:18
Contact:

Re: Alt-tab using middle mouse button ONLY  Topic is solved

22 Mar 2017, 14:49

SJN wrote:...And we should not forget the following in the Timer routine! :mrgreen:

Code: Select all

SetTimer OnIdle, Off
This is not needed if you use negative delay like I did.
https://autohotkey.com/docs/commands/SetTimer.htm wrote:[v1.0.46.16+]: If Period is negative, the timer will run only once.
As for held down buttons, you could use KeyWait

Code: Select all

RButton::
	start := A_TickCount
	KeyWait RButton, "T3"
	msgbox % "RButton was held for : " . A_TickCount - start . " ms"
Return
SJN

Re: Alt-tab using middle mouse button ONLY

27 Mar 2017, 02:18

Stupidly enough I thought to read earlier that you put a ~ in front, and couldn't find in the Help what this would do when referencing a variable.
Now I see it was a minus (-) instead! Thanks :headwall: :mrgreen:

For other people possibly reading the above:
{Blank} in front of the {Tab} can better be replaced by another time {Alt down} or alternatively the Sleep 100 (as 4GForce suggested).
{Blank} doesn't always do the trick when the script has just loaded, but the two alternatives apparantly always work.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mamo691, MrDoge and 233 guests