Hold down key while mouse button is down

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
pocpoc
Posts: 2
Joined: 21 Apr 2017, 05:05

Hold down key while mouse button is down

21 Apr 2017, 05:12

I want a key to be pressed while I hold ctrl+mouseClick, but it only presses it once.

Code: Select all

^LButton::Send {Volume_Up} ; this sends Volume_Up once when I hold the mouse
However, when I do the same with a keyboard key, it works fine.

Code: Select all

^Up::Send {Volume_Up} ; this sends Volume_Up once when I press the key then continuously (the normal behaviour for a direct keypress)
Thank you for your help,
pocpoc
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Hold down key while mouse button is down

21 Apr 2017, 06:07

Keyboard keys have a native key repeat, so holding a button down and releasing it results in the events: down-down-down-down-up; repeatedly triggering the hotkey. Mouse button don't have that, so you need to mimic it:

Code: Select all

^LButton::
	Loop {
		Send {Volume_Up}
		Sleep 200
	}Until !GetKeyState("LButton","P")
Return
pocpoc
Posts: 2
Joined: 21 Apr 2017, 05:05

Re: Hold down key while mouse button is down

21 Apr 2017, 06:22

I suspected that the intrinsic behaviour was different, thank you!

Interestingly enough however, it doesn't behave the same with Volume_Up and with Volume_Down.
With Volume_Up the volume steadily increases but with Volume_Down it starts normally then suddenly accelerates.

EDIT: This has actually nothing to do with autohotkey but is the behaviour of the Volume_Down key on my computer. After more than 6 fast presses, the volume step changes from 2 to about 10. Funny that this only happens with Volume_Down though. I guess it's better to be able to quickly turn the volume down than up :)
zakirbaig
Posts: 3
Joined: 26 Dec 2018, 01:33

Re: Hold down key while mouse button is down

26 Dec 2018, 01:40

I want my Lshift to be held down as long as I'm holding down my MButton. I wrote the script mentioned below but what it does is locks the LShift key down and does not release it even after I let go of my MButton. Where am I going wrong?

MButton::
Loop {
Send {LShift down}
}Until !GetKeyState("MButton","P")
Return
sausagemaster
Posts: 1
Joined: 24 Sep 2020, 05:13

Re: Hold down key while mouse button is down

29 Sep 2020, 02:34

zakirbaig wrote:
26 Dec 2018, 01:40
I want my Lshift to be held down as long as I'm holding down my MButton. I wrote the script mentioned below but what it does is locks the LShift key down and does not release it even after I let go of my MButton. Where am I going wrong?

MButton::
Loop {
Send {LShift down}
}Until !GetKeyState("MButton","P")
Return

I realize this post is almost 2 years old, but I'm writing this for future reference if someone else encounters the same problem: What you need to do is release the shift button once the mouse button is released:

Code: Select all

MButton::
  Loop {
    Send {LShift Down}
  } Until !GetKeyState("MButton","P")
Return
MButton Up:: Send {LShift Up}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ntepa, vanove and 180 guests