Remapping Middle click to Alt + Middle click on certain website

Get help with using AutoHotkey and its commands and hotkeys
Gizmo
Posts: 3
Joined: 19 Mar 2017, 11:37

Remapping Middle click to Alt + Middle click on certain website

19 Mar 2017, 11:48

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] [Download] GeSHi © Codebox Plus

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

All help greatly appreciated.
Helgef
Posts: 2251
Joined: 17 Jul 2016, 01:02
Contact:

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

19 Mar 2017, 12:30

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] [Download] GeSHi © Codebox Plus

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

Good luck.
Nightwolf85
Posts: 269
Joined: 05 Feb 2017, 00:03

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

19 Mar 2017, 12:48

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] [Download] GeSHi © Codebox Plus

#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] [Expand] [Download] GeSHi © Codebox Plus



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
Last edited by Nightwolf85 on 19 Mar 2017, 12:59, edited 2 times in total.
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

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

19 Mar 2017, 12:56

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.
Gizmo
Posts: 3
Joined: 19 Mar 2017, 11:37

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

20 Mar 2017, 05:19

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] [Expand] [Download] GeSHi © Codebox Plus

Nightwolf85
Posts: 269
Joined: 05 Feb 2017, 00:03

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

20 Mar 2017, 10:14

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] [Download] GeSHi © Codebox Plus

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.
Gizmo
Posts: 3
Joined: 19 Mar 2017, 11:37

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

20 Mar 2017, 16:36

Thank you very much for your help, it works! Taking out the middle click and your posted code made it work first time.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Phroster and 86 guests