Assign a function to a double middle click press? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Milincho
Posts: 10
Joined: 15 May 2024, 09:26

Assign a function to a double middle click press?

Post by Milincho » 15 May 2024, 09:34

How can I assign a function to a double middle click press?

I'm a total noob with AutoHotkey and I don't have any coding experience...
User avatar
Seven0528
Posts: 395
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Assign a function to a double middle click press?

Post by Seven0528 » 15 May 2024, 10:08

 I'm not sure about your coding skills, so I've kept it as simple as possible for now.
Questions are always welcome.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

i := 0
*MButton::  {
    global i
    ++i
    setTimer(check, -250)
}
check()    {
    global i
    if (i == 2)    {
        tooltip("Double Middle Click")
        setTimer(tooltip, -800)
        ;  TO DO
    }  else  {
        click("Middle")
    }
    i := 0
}
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
User avatar
kunkel321
Posts: 1149
Joined: 30 Nov 2015, 21:19

Re: Assign a function to a double middle click press?

Post by kunkel321 » 15 May 2024, 10:12

I was going to suggest this:

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+

MButton::
{ 	IF (A_PriorHotkey = "MButton") and (A_TimeSincePriorHotkey < 200)
	msgbox 'mbutton double click detected'
}
I think Seven's is probably better though. Mine prevents the MButton from being used for normal actions.
ste(phen|ve) kunkel
Milincho
Posts: 10
Joined: 15 May 2024, 09:26

Re: Assign a function to a double middle click press?

Post by Milincho » 15 May 2024, 10:38

@Seven0528

Ok man, thanks for fast reply!

So if I want for that 'double middle mouse click' to do 'Ctrl+Alt+Y' where/how do I put that in the script?
Milincho
Posts: 10
Joined: 15 May 2024, 09:26

Re: Assign a function to a double middle click press?

Post by Milincho » 15 May 2024, 11:01

Ah, I guess I put the combination in the 'TO DO' part... 😆

Thanks again! 👍🏻
User avatar
Seven0528
Posts: 395
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Assign a function to a double middle click press?

Post by Seven0528 » 15 May 2024, 11:07

Code: Select all

;  TO DO
    ↓

Code: Select all

send("^!y")
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
Milincho
Posts: 10
Joined: 15 May 2024, 09:26

Re: Assign a function to a double middle click press?

Post by Milincho » 15 May 2024, 11:18

Argh... but using this script disables my StrokeIt mouse gestures software, which uses the middle mouse button for drawing (dragging) the gestures... 😔🤦🏻
User avatar
kunkel321
Posts: 1149
Joined: 30 Nov 2015, 21:19

Re: Assign a function to a double middle click press?

Post by kunkel321 » 15 May 2024, 13:36

Milincho wrote:
15 May 2024, 11:18
Argh... but using this script disables my StrokeIt mouse gestures software, which uses the middle mouse button for drawing (dragging) the gestures... 😔🤦🏻
That was the problem that I found as well. I think that putting a dollar sign before the hotkey $MButton:: is "supposed" to prevent the script from stealing the native function of the hotkey. It wasn't working for me though, with the above code. Sorry... It was the tilde ~ I was thinking of.
Last edited by kunkel321 on 15 May 2024, 20:22, edited 1 time in total.
ste(phen|ve) kunkel
User avatar
Seven0528
Posts: 395
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Assign a function to a double middle click press?

Post by Seven0528 » 15 May 2024, 13:51

 *~MButton::
Hotkey Modifier Symbols

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

i := 0
*~MButton::  {
    global i
    ++i
    setTimer(check, -250)
}
check()    {
    global i
    if (i == 2)
        send("^!y")
    i := 0
}
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
User avatar
Seven0528
Posts: 395
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Assign a function to a double middle click press?

Post by Seven0528 » 15 May 2024, 14:22

 If I don't consider beginners and write code at my full potential, it would look like this.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
MButtonRemapping.InputIntervalTick := 250
#HotIf
*~MButton:: MButtonRemapping.onPress()
#HotIf MButtonRemapping.IsContinuousInput
*MButton::  MButtonRemapping.onPress(true)
#HotIf

class MButtonRemapping ;  ahk2.0
{
    static _pressedTick     := 0
        ,_inputIntervalTick := this._doubleClickSpeed
        ,_pressedCount      := 0
        ,_objbmResetCount   := objBindMethod(this, "_resetCount")
        
    static IsContinuousInput => (this._tickCount < this._pressedTick + this._inputIntervalTick)
    static InputIntervalTick    {
        get => this._inputIntervalTick
        set => this._inputIntervalTick := value
    }
    ;--------------------------------------------
    static onPress(keyBlocked := false)    {
        this._pressedTick := this._tickCount
        ++this._pressedCount, setTimer(this._objbmResetCount, -this._inputIntervalTick)
        if (keyBlocked)    {
            switch (this._pressedCount)
            {
                case 2:
                    this._resetCount()

                    ;  TO DO
                    tooltip("Double Middle Click")
                    setTimer(tooltip, -800)
            }
        }
    }
    static _resetCount()    {
        this._pressedCount := 0
    }
    ;--------------------------------------------
    static _tickCount           => (A_Is64bitOS ? this._tickCount64 : this._tickCount32)
    static _tickCount32         => dllCall("Kernel32.dll\GetTickCount", "UInt") ;  0x0 to 0xFFFFFFFF
    static _tickCount64         => dllCall("Kernel32.dll\GetTickCount64", "Int64") & 0x7FFFFFFFFFFFFFFF ;  0x0 to 0x7FFFFFFFFFFFFFFF
    static _doubleClickSpeed    => regRead("HKEY_CURRENT_USER\Control Panel\Mouse", "DoubleClickSpeed", 500)
}
Last edited by Seven0528 on 15 May 2024, 15:36, edited 1 time in total.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
Milincho
Posts: 10
Joined: 15 May 2024, 09:26

Re: Assign a function to a double middle click press?

Post by Milincho » 15 May 2024, 14:37

@Seven0528

Now it almost works, but it does weird stuff:

1. After doing the double middle click, the next gesture drawn holding single middle click is not recognized. It works just a single middle click, it doesn't detect it as a button being held.

2. Keyboard starts behaving strange, backspace/delete doesnt work, arrow keys don't work...
User avatar
Seven0528
Posts: 395
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Assign a function to a double middle click press?

Post by Seven0528 » 15 May 2024, 15:09

  1. What does 'next gesture' exactly mean?
  2. It's strange that the keyboard is affected. Would it be the same even when executed without the Send command (meaning only the Tooltip exists)?
 BTW, I provided two pieces of code. Which one did you test?
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
Milincho
Posts: 10
Joined: 15 May 2024, 09:26

Re: Assign a function to a double middle click press?

Post by Milincho » 15 May 2024, 15:31

I tested both, the first code (short one) doesn't do the Ctrl+Alt+y when I double middle click.
The second one is the one that works... but has those weird side effects.

1. To do a mouse gesture with StrokeIt I hold middle click and drag it across the screen to 'draw' a gesture. StrokeIt makes a defined action depending on the gesture you draw. After doing a middle double click using your script, the next time I try to 'draw' a gesture holding middle click it doesn work, it just registers as if I pressed the button, NOT holding it to 'draw'. The next time after that then it works again.

2. Yes, the keyboard thing was pretty shocking... most non alphanumeric keys do not work: Enter, Tab, Backspace, arrows, Insert, Del, Home, End, Pag Up, Pag Down, ... but letters and number do work.
User avatar
Seven0528
Posts: 395
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Assign a function to a double middle click press?

Post by Seven0528 » 15 May 2024, 15:58

 @Milincho
Sure, just to clarify, I didn't touch any keyboard-related parts in either the first or second code snippets I provided.
So, I genuinely share your confusion. However, I do have a suspicion that the state of the modifier keys sent via Send might be causing interference.
As for my original code without the Send command, it shouldn't cause any keyboard interference on its own, as I assumed.

I've just learned about StrokeIt myself.
While I'm curious to download it and test it out, it's not my top priority at the moment.
(Helping you has already taken up more time than I expected. Sorry.)

I might revisit this issue when I have more free time. Perhaps tomorrow? Or the day after? But it's not set in stone.
Could you provide a download link for StrokeIt? (I could find it myself, but...)
Also, could you explain in detail what Ctrl + Alt + Y does?
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
Milincho
Posts: 10
Joined: 15 May 2024, 09:26

Re: Assign a function to a double middle click press?

Post by Milincho » 15 May 2024, 16:13

@Seven0528

Sure, I'm very grateful for you spending your time trying to help me.

https://www.tcbmi.com/strokeit/ it's a very old freeware program. There are other newer mouse gestures programs like StrokesPlus.net, Gesturify and others, but time after time I find myself prefering StrokeIt for being light, fast and very easy to configure.

Ctrl+Alt+Y invokes a function I have configured in DisplayFusion to move the active window to the next monitor in a multi-display setup:
image.png
image.png (79.39 KiB) Viewed 545 times
Thanks again for your help, man!
Milincho
Posts: 10
Joined: 15 May 2024, 09:26

Re: Assign a function to a double middle click press?

Post by Milincho » 16 May 2024, 14:44

A guy here https://superuser.com/questions/1513171/make-double-clicking-the-middle-button-do-something-special posted this answer to the question:

Code: Select all

~MButton::
    If(A_TimeSincePriorHotkey<400) and (A_PriorHotkey="~MButton")
        Send 
Return
But that gave me errors.

I tried:

Code: Select all

~MButton::	
{
    If(A_TimeSincePriorHotkey<400) and (A_PriorHotkey="~MButton")
        send("!^y")
}
Which doesn't give errors, but do not work also... what am I doing wrong? 🤔


[Mod edit: Added [code][/code] tags. Please use them yourself when posting code.]
User avatar
boiler
Posts: 17242
Joined: 21 Dec 2014, 02:44

Re: Assign a function to a double middle click press?  Topic is solved

Post by boiler » 16 May 2024, 16:22

Are you sure it didn't give you an error? It should have if you ran it with AHK v2. Here's how to fix it:

Code: Select all

#Requires AutoHotkey v2.0
~MButton::	
{
    If (A_PriorHotkey="~MButton") and (A_TimeSincePriorHotkey<400)
        send("!^y")
}

Or not having to worry about the literal string in case you change the hotkey:

Code: Select all

#Requires AutoHotkey v2.0
~MButton:: {
	if (A_PriorHotkey = A_ThisHotkey) && (A_TimeSincePriorHotkey < 400)
		Send '!^y'
}
Milincho
Posts: 10
Joined: 15 May 2024, 09:26

Re: Assign a function to a double middle click press?

Post by Milincho » 16 May 2024, 18:21

@boiler

I see... yeah, your correction works but... it doesn't work properly detecting a double click. It send the keystroke with each single middle click, even if the previous one is from a different button or key.
User avatar
boiler
Posts: 17242
Joined: 21 Dec 2014, 02:44

Re: Assign a function to a double middle click press?

Post by boiler » 16 May 2024, 18:26

I can’t see why it would do that. When I test it in a text editor and sending just the letter y instead of !^y so I can see visible proof, it works only after two quick clicks. Make sure you don’t have any other scripts running.
Milincho
Posts: 10
Joined: 15 May 2024, 09:26

Re: Assign a function to a double middle click press?

Post by Milincho » 16 May 2024, 19:10

@boiler

It was a conflict with StrokeIt. Using a different mouse gestures software it works ok now.

Thanks! 👏🏻👍🏻
Post Reply

Return to “Ask for Help (v2)”