MouseClickDrag And Hold While Holding Down A Key

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
steevy
Posts: 11
Joined: 16 Dec 2020, 18:08

MouseClickDrag And Hold While Holding Down A Key

Post by steevy » 16 Dec 2020, 18:29

Hello everyone!
I am new to AutoHotKey.
Last couple of days, I have been looking for one script that suits my need but couldn't find anywhere.
My problem is, I want to Drag and Hold Left Mouse button and keep holding it as long as a Key is being pressed.
And when that Key is released mouse pointer would go back to its previous position.
It would be much appreciated if someone could help me on this.
Thanks in advance.
Have a nice day!
I tried to do with the following codes but no success.

Code: Select all

Numpad1::
Send {LButton down}
MouseClickDrag, Left, 1476, 931, 1467, 631, 40
Send {LButton up}
return

User avatar
mikeyww
Posts: 26951
Joined: 09 Sep 2014, 18:38

Re: MouseClickDrag And Hold While Holding Down A Key

Post by mikeyww » 16 Dec 2020, 18:36

Code: Select all

Numpad1::
MouseGetPos, xpos, ypos
Click, down
SoundBeep, 1000, 20
KeyWait, %A_ThisHotkey%
Click, up
SoundBeep, 1500, 20
MouseMove, xpos, ypos
Return

steevy
Posts: 11
Joined: 16 Dec 2020, 18:08

Re: MouseClickDrag And Hold While Holding Down A Key

Post by steevy » 16 Dec 2020, 18:51

Sorry, that doesn't work for me. It drags the mouse only one time and doesn't go back to starting position.
Here is what I have found so far. But it is only a mouse click. I would like a Mouse Drag instead.

Code: Select all

Numpad1::
  Loop {
    SendEvent {Click, 1476, 931, D}	
  } Until !GetKeyState("Numpad1","P")
Return
Numpad1 Up:: Send {LButton Up}
[Mod edit: [code][/code] tags added.]

User avatar
mikeyww
Posts: 26951
Joined: 09 Sep 2014, 18:38

Re: MouseClickDrag And Hold While Holding Down A Key

Post by mikeyww » 16 Dec 2020, 19:02

1. Are you sure that you saved & executed the script, & removed others that were running?

2. Where did the MouseMove take your cursor?

steevy
Posts: 11
Joined: 16 Dec 2020, 18:08

Re: MouseClickDrag And Hold While Holding Down A Key

Post by steevy » 16 Dec 2020, 19:21

Well, first of all, I would like to thank you for your time.
Yes, I did everything you asked.
Let me clarify my exact problem.
I am play an android game on an emulator and there is no drag and hold option in keymapping.
I would like to stimulate it with my mouse left click and drag.
So there is a button on the screen. And I would like to drag that button up and keep holding it as long as I am pressing a Key.
When I release that key the mouse pointer would go back to its original position.

The original position is X=1476, Y=931.
The final drag position is X=1476, Y=631.

The following is a simple MouseClickDrag script.

Numpad1::
MouseClickDrag, Left, 1476, 931, 1467, 631, 40

But this does only one time and when I keep pressing Numpad1 it keeps repeating the action.

User avatar
mikeyww
Posts: 26951
Joined: 09 Sep 2014, 18:38

Re: MouseClickDrag And Hold While Holding Down A Key

Post by mikeyww » 16 Dec 2020, 19:52

Code: Select all

Numpad1::
MouseGetPos, xpos, ypos
x := 1476, y1 := 931, y2 := 631
Click, %x%, %y1%, down
SoundBeep, 1500, 20
MouseMove, x, y2
KeyWait, %A_ThisHotkey%
Click, up
SoundBeep, 1000, 20
MouseMove, xpos, ypos
Return

steevy
Posts: 11
Joined: 16 Dec 2020, 18:08

Re: MouseClickDrag And Hold While Holding Down A Key

Post by steevy » 16 Dec 2020, 20:04

Thank you very much.
It is working almost perfectly.
Only 2 things I would like to add.
I would like to slow down the Mouse Drag Movement.
And the Mouse Cursor doesn't go back to the starting/initial position (in this case it is X=1476, Y=931) when I release the key (in this case Numpad1).

steevy
Posts: 11
Joined: 16 Dec 2020, 18:08

Re: MouseClickDrag And Hold While Holding Down A Key

Post by steevy » 16 Dec 2020, 20:12

I tweaked a little bit to the script.
That is exactly what I have been looking for.
So the problem is solved.
Thanks again.
Have a nice day!

Code: Select all

Numpad1::
MouseGetPos, xpos, ypos
x := 1476, y1 := 931, y2 := 631
Click, %x%, %y1%, down
SoundBeep, 1500, 20
MouseMove, x, y2, 10
KeyWait, %A_ThisHotkey%
Click, up
SoundBeep, 1000, 20
MouseMove, 1476, 931
Return
[Mod edit: [code][/code] tags added.]

User avatar
mikeyww
Posts: 26951
Joined: 09 Sep 2014, 18:38

Re: MouseClickDrag And Hold While Holding Down A Key

Post by mikeyww » 16 Dec 2020, 20:16

Good! Here's another tweak. Enjoy.

Code: Select all

Numpad1::
x := 1476, y1 := 931, y2 := 631
Click, %x%, %y1%, down
SoundBeep, 1500, 20
MouseMove, x, y2, 10
KeyWait, %A_ThisHotkey%
Click, up
SoundBeep, 1000, 20
MouseMove, x, y1
Return

steevy
Posts: 11
Joined: 16 Dec 2020, 18:08

Re: MouseClickDrag And Hold While Holding Down A Key

Post by steevy » 16 Dec 2020, 20:18

Thank you very much.
Have a nice day!

steevy
Posts: 11
Joined: 16 Dec 2020, 18:08

Re: MouseClickDrag And Hold While Holding Down A Key

Post by steevy » 05 Jul 2022, 04:43

Hello again after a long time!
This time I would like to drag mouse from left to right while holding down a key.
This is what I came up with but failed.
Could you please help me on this?
Thanks in advance.

Code: Select all

Numpad1::
x1 := 1560, x2 := 1660, y := 656
Click, %x1%, %y%, down
Sleep, 1
MouseMove, x1, x2, 10
KeyWait, %A_ThisHotkey%
Click, up
MouseMove, x1, y
Return
Last edited by steevy on 05 Jul 2022, 04:50, edited 1 time in total.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: MouseClickDrag And Hold While Holding Down A Key

Post by BoBo » 05 Jul 2022, 04:45

@steevy - your posting have been edited several times by forum moderators bc of missing code-tags. Please add those on your own. Start NOW! Thx for listening. :)

steevy
Posts: 11
Joined: 16 Dec 2020, 18:08

Re: MouseClickDrag And Hold While Holding Down A Key

Post by steevy » 05 Jul 2022, 04:52

Ops! I am sorry. I will definitely do it next time. Thank you.


steevy
Posts: 11
Joined: 16 Dec 2020, 18:08

Re: MouseClickDrag And Hold While Holding Down A Key

Post by steevy » 05 Jul 2022, 08:13

Thank you indeed.

Post Reply

Return to “Ask for Help (v1)”