Page 1 of 1

Remapping Middle click to Alt + Middle click on certain website

Posted: 19 Mar 2017, 11:48
by Gizmo
Hello, this is my first post! I am having difficulty getting my script to work. I am using the GetActiveBrowserURL() function found on https://autohotkey.com/boards/viewtopic.php?t=3702. I also want to write my script such that when I am on a specific website and I hold down MButton, I want it so simulate pressing Alt and MButton at the same time. How could I do this?
My code so far(it's not very good!):

Code: Select all

#include ;file location	
#If WinActive("ahk_exe chrome.exe")	
	#If InStr(GetActiveBrowserURL(),"cad.onshape.com/documents/")
		MButton::!MButton	  
	#If
#If
All help greatly appreciated.

Re: Remapping Middle click to Alt + Middle click on certain website

Posted: 19 Mar 2017, 12:30
by Helgef
You cannot nest #ifs. You can do #if a && b though.
Another thing, I tried to use GetActiveBrowserURL() in an #if once, I never got it to work, I don't know why, maybe there was a timeout issue. Anyways, I did something like this instead,

Code: Select all

Key::
	If (GetActiveBrowserURL()!="something")
		Send, key
	; ...
return
Good luck.

Re: Remapping Middle click to Alt + Middle click on certain website

Posted: 19 Mar 2017, 12:48
by Nightwolf85
Hello,
I'm not sure if that is your exact code for this portion, but the #Include needs to have the file that contains the function, so I'll assume you have that right as it doesn't run as is.

As far as I am aware you can't have more than on #IF active at a time, so your second use of it overwrites the first.

Could you maybe try to change the second #IF to a regular IF like so:

Code: Select all

#Include PATH TO FILE WITH FUNCTION ;file location	
#If WinActive("ahk_exe chrome.exe")	
MButton::
	IF( InStr(GetActiveBrowserURL(),"cad.onshape.com/documents/") )
		Send, !{MButton}
	Else
		Send, {MButton}
Return
#If
And reading how the function GetActiveBrowserURL() works you probably don't even need the #IF at all, since it would return an empty string if a browser isn't active, which would fail the IF( InStr(GetActiveBrowserURL(),"cad.onshape.com/documents/") ) line already.

If you need the button to hold down while you are physically holding it down you could try this as it keeps the button down instead of just sending it:

Code: Select all

MButton::
	IF( InStr(GetActiveBrowserURL(),"cad.onshape.com/documents/") ) {
		Send, !{MButton Down}
		While GetKeyState("MButton", "P")
			Continue
		Send {MButton Up}
	}
	Else {
		Send, {MButton Down}
		While GetKeyState("MButton", "P")
			Continue
		Send {MButton Up}
	}
Return
Please note I haven't tested this completely, but please try and let me know.


**Edit
Helgef, I believe you might be right that using GetActiveBrowserURL() in an #IF is a timeout issue.

Gizmo, If you want to use something closer to your initial code you can play with #IfTimeout and see if you can get it to work.
https://autohotkey.com/docs/commands/_IfTimeout.htm

Re: Remapping Middle click to Alt + Middle click on certain website

Posted: 19 Mar 2017, 12:56
by Almost_there
Normally the Window name on the web browser will reflect the website title - use SetTitleMatchMode, 2.

And then something like #IfWinActive, nameOfWebsite
Ant then remap the MButton to whatever you like.

Re: Remapping Middle click to Alt + Middle click on certain website

Posted: 20 Mar 2017, 05:19
by Gizmo
Nightwolf85 I modified your script a little bit but it works! Well apart from one thing. It appears to double click the middle mouse when I release the middle mouse.
My script needs both alt and middle mouse to be held down, so I used {Alt Down}{MButton Down} instead. I then had to add an {Alt Up}. How could I fix the double mouse click? Also, Is the else statement necessary? Since I want the middle mouse to function normally when not on the specific site.

Code: Select all

#include C:\Program Files\AutoHotkey\My Scripts\geturl.ahk	
#If WinActive("ahk_exe chrome.exe")	
~MButton::
	IF( InStr(GetActiveBrowserURL(),"cad.onshape.com/documents/") ) {
		
		while GetKeyState("MButton", "P")==1
			Sendevent, {Alt down}{MButton Down}
		sendevent,{Alt up}{Mbutton up}
	}

	sendevent,{alt up}
Return
#If

Re: Remapping Middle click to Alt + Middle click on certain website

Posted: 20 Mar 2017, 10:14
by Nightwolf85
I see you put the ~ qualifier on the hotkey. Which is actually fine, and is probably better than my proposed code. However it will allow the middle click through, then the code is also sending a middle click, thus resulting in 2 clicks.

So try taking out the {Mbutton Down} and {MButton Up} from the code.

The only thing I can think of is the order will probably be {MButton} then {Alt} so if that is an issue, then using no ~ and the else is better, if order doesn't matter this is fine. The point of the else was to give normal MButton fuinctionality when the condition wasn't met, which is also accomplished using ~.

Note it is more efficient to do the holding this way:

Code: Select all

Sendevent, {Alt down}  ; Push the button down only once
while (GetKeyState("MButton", "P"))  ; No need to test if it equals 1, since it does that behaviour by default.
	Continue
sendevent,{Alt up}
I've seen in some rare cases where constantly pushing a key down causes an issue. Give it a try and see, and if it doesn't work go back to pushing it every while loop.

Re: Remapping Middle click to Alt + Middle click on certain website

Posted: 20 Mar 2017, 16:36
by Gizmo
Thank you very much for your help, it works! Taking out the middle click and your posted code made it work first time.