Page 1 of 1

Hold down key while mouse button is down

Posted: 21 Apr 2017, 05:12
by pocpoc
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

Re: Hold down key while mouse button is down

Posted: 21 Apr 2017, 06:07
by Nextron
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

Re: Hold down key while mouse button is down

Posted: 21 Apr 2017, 06:22
by pocpoc
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 :)

Re: Hold down key while mouse button is down

Posted: 26 Dec 2018, 01:40
by zakirbaig
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

Re: Hold down key while mouse button is down

Posted: 29 Sep 2020, 02:34
by sausagemaster
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}