Dual functionality of middle mouse button based on length of hold

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
A Keymaker
Posts: 454
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Dual functionality of middle mouse button based on length of hold

Post by A Keymaker » 05 Jun 2022, 11:05

I have the simple

Code: Select all

MButton::c
that works A-OK, but because of limited numbers of extra mouse buttons I need my Middle Mouse Button to:

stop sending >>c<< and send >>v<< when not released but held down for at least half a second


This does not work:

Code: Select all

MButton::c
    Sleep, 666
    if while GetKeyState("MButton"), P
    {
    Send {v}
    }
return

[Mod edit: Added subject line.]

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Dual functionality of middle mouse button based on length of hold

Post by boiler » 05 Jun 2022, 11:19

Google "autohotkey long press" and you'll find this topic addressed many times, such as here.

User avatar
A Keymaker
Posts: 454
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: Dual functionality of middle mouse button based on length of hold

Post by A Keymaker » 05 Jun 2022, 12:32

Well, these do not work:

Code: Select all

#UseHook,On
MButton::
    Send,% c
    KeyWait,% MButton,T0.5
    If ErrorLevel {
        Send {v}
        DeadKey:=MButton
    }
    KeyWait,% MButton
Return

Code: Select all

$*MButton::
Send {c}
KeyWait, MButton, T0.5
If ErrorLevel {
    Send {v}
    KeyWait, MButton
}
return

Code: Select all

!MButton::
  keywait, MButton, T0.5
  err := Errorlevel
  if (err)
  { Send, {v}
    return
  }
  else
  { Send, {c}
    return
  }
  return

Code: Select all

~MButton::
keywait MButton, t.0.5
Send {v}
if errorlevel
    Send {c}
return


And so I would like to kindly request some more help with this task

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Dual functionality of middle mouse button based on length of hold

Post by boiler » 05 Jun 2022, 13:45

Code: Select all

MButton::
    Send, {c down}
    KeyWait, MButton, T0.5
    if ErrorLevel
        Send, {c up}{v down}
    KeyWait, MButton
	Send, {c up}{v up}
return

User avatar
A Keymaker
Posts: 454
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: Dual functionality of middle mouse button based on length of hold

Post by A Keymaker » 05 Jun 2022, 15:17

boiler wrote:
05 Jun 2022, 13:45

Code: Select all

MButton::
    Send, {c down}
    KeyWait, MButton, T0.5
    if ErrorLevel
        Send, {c up}{v down}
    KeyWait, MButton
	Send, {c up}{v up}
return
No matter if I just quick click or hold it, the Key history and script info shows that what is send is: down-c, up-c, up-v

Thus all I get is what pressing of >>c<< does
Last edited by A Keymaker on 07 Jun 2022, 10:46, edited 1 time in total.

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Dual functionality of middle mouse button based on length of hold

Post by boiler » 05 Jun 2022, 15:43

Are you sure you closed all other scripts then saved this one before running it? Make sure you don't have any stray scripts running by checking the overflow section of the system tray for AHK icons.

There is no reason for it to act as you described. Below is what I get when I hold MButton down for over half a second (and you should too). It presses c down, then after half a second it releases c, then holds v down until MButton is released, then it releases v (and releases c as well even if it is no longer down in case it gets to that point from a quick release of the MButton).

Code: Select all

VK  SC	Type	Up/Dn	Elapsed	Key	
----------------------------------------
04  000	h		d		0.72	MButton        	
43  02E	i		d		0.00	c              	
43  02E	i		u		0.52	c              	
56  02F	i		d		0.02	v              	
04  000	s		u		1.36	MButton        	
43  02E	i		u		0.02	c              	
56  02F	i		u		0.01	v   

User avatar
A Keymaker
Posts: 454
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: Dual functionality of middle mouse button based on length of hold

Post by A Keymaker » 07 Jun 2022, 10:05

Yes, I am sure

I closed them and not merely re-run them on top of previous version, just to be sure. I have max 2 AHK files now, the main one and the second in one I tests separately small pieces of code if they do not work in conjunction with all the rest

And I know now who is the culprit: the mouse. Because if change this to

Code: Select all

q::
    Send, {c down}
    KeyWait, q, T0.5
    if ErrorLevel
        Send, {c up}{v down}
    KeyWait, q
    Send, {c up}{v up}
return
the it works A-OK, be it in Notepad or filehandler - or in Winamp, where it is needed for real. And it is reported as

43 02E i d 9.39 c
43 02E i u 0.50 c
56 02F i d 0.00 v
43 02E i u 0.33 c
56 02F i u 0.00 v


But when I replace every q with MButton then this is how it looks like on my machine

04 000 h d 2.58 MButton
43 02E i d 0.00 c
43 02E i u 0.00 c
56 02F i u 0.00 v
04 000 a u 2.13 MButton



I tried replacing

Code: Select all

Send, {c up}{v down}
with

Code: Select all

Send, {c up}{Text}v

Code: Select all

Send {c up}{Text}v

Code: Select all

Send, {c up}{Blind}v

Code: Select all

Send {c up}{Blind}v

Code: Select all

SendRaw cv
but the outcomes are the same - i.e. Middle Mouse Button produces only c, while when replaced by key q I will get c and the v. And as a test I also replaced MButton with RButton - and it also worked just like when swapping with q

Ergo: the problem is with the Middle Mouse Button of my mouse - it apparently does not get registered as being held down by AutoHotkey. That mouse also have another middle button activated when scroll wheel is pressed [not turned] down - but that "normal" MButton is functioning probably everywhere A-OK

And so the code has to somehow work around the issue of MButton not being registered in a hold down status
Last edited by A Keymaker on 12 Jul 2022, 16:07, edited 3 times in total.

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Dual functionality of middle mouse button based on length of hold

Post by boiler » 07 Jun 2022, 10:14

I'm not familiar with mouse devices that have another middle button other than a scroll wheel button that acts as MButton when it is pressed. If one of them won't register as being held down, then yes, that would appear to be a problem that AHK can't do much about. This is a simple test to isolate whether it's registering as being held down or not:

Code: Select all

MButton::
	SoundBeep, 400
	KeyWait, MButton
	SoundBeep, 600
return

User avatar
A Keymaker
Posts: 454
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: Dual functionality of middle mouse button based on length of hold

Post by A Keymaker » 07 Jun 2022, 15:32

I hear no beeps when I press or press and hold down the wheel

I hear 2 beeps when I press the normal Middle Mouse Button ; the one that I was using all that time. And only 1 beep if I remove the

Code: Select all

SoundBeep, 600
line from that code

And the history for those 2 beeps is

04 000 h d 2.47 MButton
04 000 a u 0.31 MButton

User avatar
A Keymaker
Posts: 454
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: Dual functionality of middle mouse button based on length of hold

Post by A Keymaker » 12 Jul 2022, 16:23

I have tested once more some other ways of approaching this within AutoHotkey

And as a last resort I thought that maybe I could workaround this issue of MButton not being registered properly [while other mouse buttons are] with this code

Code: Select all

q::
    Send, {c down}{c up}
    Gosub, StopPlaybackCommand
Exit, ExitCode

StopPlaybackCommand:
    Sleep, 555
    while GetKeyState("q")
    Send, {v down}{v up}
return
but I does not work - i.e. it just apparently sends c every half second, never getting to executing v, to which attest this History recorder when I held down the q thus experienced three consecutive pausing and un-pausings

51 010 h d 0.05 q
51 010 h d 0.02 q
51 010 h d 0.05 q
51 010 h d 0.03 q
51 010 h d 0.05 q
51 010 h d 0.05 q
51 010 h d 0.03 q
51 010 h d 0.05 q
51 010 h d 0.01 q
51 010 h d 0.03 q
51 010 h d 0.05 q
51 010 h d 0.05 q
43 02E i d 0.00 c
43 02E i u 0.00 c
51 010 h d 0.03 q
51 010 h d 0.05 q
51 010 h d 0.03 q
51 010 h d 0.03 q
51 010 h d 0.03 q
51 010 h d 0.05 q
51 010 h d 0.03 q
51 010 h d 0.05 q
51 010 h d 0.03 q
51 010 h d 0.03 q
51 010 h d 0.03 q
51 010 h d 0.05 q
51 010 h d 0.03 q
51 010 h d 0.05 q
51 010 h d 0.03 q
51 010 h d 0.05 q
43 02E i d 0.00 c
43 02E i u 0.00 c
51 010 h d 0.02 q
51 010 h d 0.05 q
51 010 h d 0.03 q
51 010 h d 0.05 q
51 010 s u 0.01 q


And I do not understand why the above code does not work even when utilizing for test purposes the q key instead of the target MButton

User avatar
A Keymaker
Posts: 454
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: Dual functionality of middle mouse button based on length of hold

Post by A Keymaker » 12 Jul 2022, 16:31

And that problematic mouse of mine is Logitech Mouse MX Anywhere 2 Business Travel / Logitech Mouse MX Anywhere 2 For Amazon



Is there by any chance a way of describing Middle Mouse Button within an AHK file other than

Code: Select all

MButton
?

gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: Dual functionality of middle mouse button based on length of hold

Post by gregster » 12 Jul 2022, 16:40

Check the physical keystate:

Code: Select all

GetKeyState("q", "P")

XMCQCX
Posts: 224
Joined: 14 Oct 2020, 23:44

Re: Dual functionality of middle mouse button based on length of hold

Post by XMCQCX » 12 Jul 2022, 17:38

Code: Select all

Mbutton::
If !Ta
 SetTimer, Ta, -500
Ta++
Return
Ta:
Switch
{ Case GetKeyState("Mbutton", "P"): SendInput Held`n
  Case Ta = 2                     : SendInput Double`n
  Case Ta = 1                     : SendInput Single`n
}
KeyWait, Mbutton
Ta =
Return

User avatar
A Keymaker
Posts: 454
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: Dual functionality of middle mouse button based on length of hold

Post by A Keymaker » 13 Jul 2022, 12:08

gregster wrote:
12 Jul 2022, 16:40
Check the physical keystate:

Code: Select all

GetKeyState("q", "P")
Yes- correcting the test version to

Code: Select all

q::
    Send, {c down}{c up}
    Gosub, StopPlaybackCommand
Exit, ExitCode

StopPlaybackCommand:
    Sleep, 555
    while GetKeyState("q", "P")
    Send, {v down}{v up}
return
works: in that now when the q is held down stops the playback and not just un-pauses it to pause it gain. But then again- it is only a test, which stops working when q is replaced with MButton



As for the second solution

I figured out that I need to do something like

Code: Select all

q::
If !Ta
 SetTimer, Ta, -99
Ta++
Return
Ta:
Switch
{ Case GetKeyState("q", "P")  : SendInput {v}
  Case Ta = 2                 : SendInput {c}
  Case Ta = 1                 : SendInput {c}
}
KeyWait, q
Ta =
Return
which seems to work A-OK - but then again, this is a test version

And once again, after changing all q's to MButton's it stops. And a test consisting of pushing quickly q, pushing it quickly again and then holding it down which looks on History like this

51 010 h d 1.67 q
51 010 s u 0.05 q
43 02E i d 0.06 c
43 02E i u 0.00 c
51 010 h d 0.76 q
51 010 s u 0.05 q
43 02E i d 0.06 c
43 02E i u 0.00 c
51 010 h d 1.41 q
56 02F i d 0.11 v
56 02F i u 0.00 v
51 010 h d 0.16 q
51 010 h d 0.03 q
51 010 h d 0.05 q
51 010 h d 0.03 q
51 010 h d 0.03 q
51 010 h d 0.05 q
51 010 h d 0.03 q
51 010 h d 0.03 q
51 010 h d 0.03 q
51 010 h d 0.05 q
51 010 h d 0.03 q


will look somewhat like this

04 000 h d 3.02 MButton
04 000 # u 0.08 MButton
43 02E i d 0.03 c
43 02E i u 0.00 c
04 000 h d 1.44 MButton
04 000 # u 0.06 MButton
43 02E i d 0.05 c
43 02E i u 0.00 c
04 000 h d 1.03 MButton
43 02E i d 0.11 c
43 02E i u 0.00 c
04 000 # u 1.48 MButton


when also pushing quickly Middle Mouse Button, pushing it quickly again and then holding it down - which as we can see results with MButton not being registered multiple times in a row thus not producing v but only c [as if Middle Mouse Button was only quickly pushed for the third time and not held down for few seconds]


And also: in both versions, LButton and RButton do work when replacing q- it is only the MButton that is half-working


And on the side note: in the Help files I found reference only to Switch and to ! but not to that Ta element - what is that? I tried replacing it with my descriptive StopPlaybackCommand but then this script broke the rest of AHK file it was put on

User avatar
A Keymaker
Posts: 454
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: Dual functionality of middle mouse button based on length of hold

Post by A Keymaker » 16 Aug 2022, 12:58

In Winamp there is this

Options > Preferences > Global Preferences > Global Hotkeys

feature. Among others It has an action for stopping of playback described as its own native Hotkey Stop; and also described [i.e. evoked] as Ctrl + Shift + Alt + 1, with that second one being a configuration of my choice [as I use this combination for my gamepad which I had turned into a remote control for Winamp, working in every program, by using a software provided by its manufacturer]. So maybe I could try utilizing those two Winamp's Hotkeys instead of trying to sent simply v key to it?

Or am I right suspecting that it will be just a waste of time, because I still will not be able to properly send MButton to Winamp [thus evoke whatever Global Hotkeys Winamp might have]?

User avatar
A Keymaker
Posts: 454
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: Dual functionality of middle mouse button based on length of hold

Post by A Keymaker » 25 Feb 2023, 09:23

And here is another example of apparently the same issue with Middle Mouse Button: viewtopic.php?f=76&t=112961&p=503583#p503583


I have reported this, with a larger scope, in a brand new topic: viewtopic.php?f=76&t=112961&p=503582

Post Reply

Return to “Ask for Help (v1)”