Send clicks relative to cursor position?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
fadetoblack
Posts: 14
Joined: 01 Aug 2014, 20:04

Send clicks relative to cursor position?

18 Nov 2017, 16:51

Hi guys,

I was wondering how I could create a script that would send a click wth coordinates that are relative to the current cursor position ? Is this possible ? Ie the cursor position value being zero , then have a hot key which is assigned to always click 30 pixels to the right of the current cursor position ? Sorry if I sound like a noob but I am ! I'm just learning with Auto IT and could really used the help on this :D
User avatar
KuroiLight
Posts: 327
Joined: 12 Apr 2015, 20:24
Contact:

Re: Send clicks relative to cursor position?

18 Nov 2017, 17:00

Code: Select all

MouseMove, 5, 5,, R
Click
more specifically to your question:

Code: Select all

MouseGetPos, X, Y
MouseMove, 30, 0,, R
Click
MouseMove, %X%, %Y%,
btw, AutoIT and AHK are not the same, make sure you are in the right place, lol.
Windows 10, Ryzen 1600, 16GB G.Skill DDR4, 8GB RX 480 | [MyScripts][MySublimeSettings] [Unlicense][MIT License]
01/24/18
[/color]
fadetoblack
Posts: 14
Joined: 01 Aug 2014, 20:04

Re: Send clicks relative to cursor position?

18 Nov 2017, 17:03

Thanks for the speedy reply :) I will try that out . Its ok , I meant to say AHK not AutoIt
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Send clicks relative to cursor position?

18 Nov 2017, 17:27

Click has a Relative optional parameter.

Example function:

Code: Select all

1::RelativeClick(0,0,30,0)


RelativeClick(currX,currY,relX,relY)
{
	MouseGetPos, pX, pY
	if (pX = currX && pY = currY)
		Click, %relX%, %relY%, Relative
}
Guest

Re: Send clicks relative to cursor position?

21 Nov 2017, 21:00

Thanks guys.... I cant seem to work out two other points now though lol . I want to bind scripts to the NumPad numbers, I tried this for NumPad 1 for example..

Code: Select all

MouseGetPos, X, Y
MouseMove, 30, 0,, R
NumPad1::Click
MouseMove, %X%, %Y%,
But that doesnt work. How do I format it ? And also, how do I declare left and up movement ?
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Send clicks relative to cursor position?

21 Nov 2017, 21:05

@Guest Have you run through the tutorial? Because you probably do not want code like that. If that is your only code in a script, it's going to move the mouse 30 pixels to the right as it's in the auto-execute section. And then you press Numpad1 to make a click wherever the mouse is. The second MouseMove is never executed because you have created a single-line hotkey.
fadetoblack
Posts: 14
Joined: 01 Aug 2014, 20:04

Re: Send clicks relative to cursor position?

21 Nov 2017, 21:10

Ok so I place the NumPad1 click at the end then ? I really need help with finalising this , the tutorial doesn't seem clear enough to me to get to the point I need to .
Last edited by fadetoblack on 21 Nov 2017, 21:35, edited 1 time in total.
User avatar
KuroiLight
Posts: 327
Joined: 12 Apr 2015, 20:24
Contact:

Re: Send clicks relative to cursor position?

21 Nov 2017, 21:34

What are you trying to do with Numpad1?
the code I posted was merely to get you started you'll want to put that in a hotkey or function.
Windows 10, Ryzen 1600, 16GB G.Skill DDR4, 8GB RX 480 | [MyScripts][MySublimeSettings] [Unlicense][MIT License]
01/24/18
[/color]
fadetoblack
Posts: 14
Joined: 01 Aug 2014, 20:04

Re: Send clicks relative to cursor position?

21 Nov 2017, 21:40

Ok, basically I am trying to bind all Numpad numbers to scripts that all vary slightly in their click coordinates . So Numpad 1 script must click say 90 pixels up from the current cursor position, Numpad 2 must click 90 pixels up and 20 to the right , etc etc ....
User avatar
KuroiLight
Posts: 327
Joined: 12 Apr 2015, 20:24
Contact:

Re: Send clicks relative to cursor position?

21 Nov 2017, 21:48

this might be what your looking for then:

Code: Select all

RelativeClick(rX := 0, rY := 0) {
    MouseGetPos, X, Y
    MouseMove, %rX%, %rY%,, R
    Click
    MouseMove, %X%, %Y%,
}

Numpad1::RelativeClick(0, -90)
Numpad2::RelativeClick(20, -90)
Windows 10, Ryzen 1600, 16GB G.Skill DDR4, 8GB RX 480 | [MyScripts][MySublimeSettings] [Unlicense][MIT License]
01/24/18
[/color]
fadetoblack
Posts: 14
Joined: 01 Aug 2014, 20:04

Re: Send clicks relative to cursor position?

21 Nov 2017, 21:53

Ok awesome thank you :) I will have a play around with that script ...
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Send clicks relative to cursor position?

21 Nov 2017, 22:11

To keep it simple by not using a function, but longer code, this is what you could do. I think it's easier to understand the long-form code and then seeing how it gets condensed with a function.

Code: Select all

Numpad1::
MouseGetPos, X, Y
MouseMove, 0, -90, , R
Click
MouseMove, %X%, %Y%
return

Numpad2::
MouseGetPos, X, Y
MouseMove, 20, -90, , R
Click
MouseMove, %X%, %Y%
return
By using a function like KuroiLight does there, you assign values to variable defined in the function.

RelativeClick(rX := 0, rY := 0){ means the function name is RelativeClick (it can be almost any name you want), the first parameter is rX, and the second parameter is rY. You can use rX and rY as variables inside the function. The := 0 gives a default value to rX and rY -- that is, if you do not explicitly fill in that parameter when you call the function, it will use 0 there. Which means in KuroiLight's code, instead of Numpad1::RelativeClick(0, -90) (which calls the function; this is not the definition because the { is not there), you could actually use Numpad1::RelativeClick( , -90). The blank value for the first parameter would be treated as a 0 by the definition of the function.

And KuroiLight is using the variables rX and rY in the MouseMove command because that is what you are changing between the Numpad1 and Numpad2 hotkeys.
User avatar
KuroiLight
Posts: 327
Joined: 12 Apr 2015, 20:24
Contact:

Re: Send clicks relative to cursor position?

21 Nov 2017, 22:19

Thank you Exaskryz, I'm not too good at explaining things so I skip this sometimes and point to the docs.
Windows 10, Ryzen 1600, 16GB G.Skill DDR4, 8GB RX 480 | [MyScripts][MySublimeSettings] [Unlicense][MIT License]
01/24/18
[/color]
fadetoblack
Posts: 14
Joined: 01 Aug 2014, 20:04

Re: Send clicks relative to cursor position?

22 Nov 2017, 15:42

Hi guys,

Thanks so much for your help , I have finalised it all now but I just wanted to ask one final thing . Would it be possible to have different "sets" of coordinates assgned to keys on the numberpad? Ideally i would prefer 3 different sets of numberpad keys coordinates and then have say down arrow for instance as a hotkey to toggle/cycle through them.. Is that possible ?
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Send clicks relative to cursor position?

22 Nov 2017, 17:21

Yes, that's possible. Assign your set changer hotkey to cycle through settings for a variable, then have a separate #If section of hotkeys for each variable setting. For example:

Code: Select all

KeySet := 1

Numpad9::
  KeySet++
  If KeySet > 3
    KeySet := 1
return

#If KeySet = 1
Numpad1::RelativeClick(0, -90)

#If KeySet = 2
Numpad1::RelativeClick(20, -90)

#If KeySet = 3
Numpad1::RelativeClick(30, -90)
Last edited by Osprey on 22 Nov 2017, 17:45, edited 1 time in total.
fadetoblack
Posts: 14
Joined: 01 Aug 2014, 20:04

Re: Send clicks relative to cursor position?

22 Nov 2017, 17:32

Awesome, thanks for the reply :D . I will get on with that and see how it goes
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Send clicks relative to cursor position?

22 Nov 2017, 17:42

You're welcome. BTW, you can also consolidate the toggle hotkey into a single line, if you wish:

Code: Select all

Numpad9::KeySet += KeySet < 3 ? 1 : -2    ; If KeySet is less than 3, add 1; otherwise, add -2 (i.e. subtract 2 from 3 to return to 1)
I'll leave the version above, though, since it may be easier for others to understand and modify.
fadetoblack
Posts: 14
Joined: 01 Aug 2014, 20:04

Re: Send clicks relative to cursor position?

22 Nov 2017, 18:06

OK thanks, upon mulling it over I think really I need to assign a key for each set rather than toggle. I want to use left arrow for keyset 1, down arrow for keyset 2 and right arrow for keyset 3. Could you possibly help me with that code ? How do I define that ?
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Send clicks relative to cursor position?

22 Nov 2017, 18:21

Code: Select all

^Numpad1::KeySet := 1

^Numpad2::KeySet := 2

^Numpad3::KeySet := 3

#If KeySet = 1
Numpad1::RelativeClick(0, -90)

#If KeySet = 2
Numpad1::RelativeClick(20, -90)

#If KeySet = 3
Numpad1::RelativeClick(30, -90)
I recommend that you do something like that (Ctrl + Numpad1-3) rather than use arrow keys, since you never know when you may need your arrow keys.
fadetoblack
Posts: 14
Joined: 01 Aug 2014, 20:04

Re: Send clicks relative to cursor position?

22 Nov 2017, 19:18

Its fine to use arrow keys for this , I know what you're saying but its fine in this case. I've run into a prob though, I'm getting Error: Call to nonexistent function Specifically : RelativeClick(40,-360) When I double click to execute.... Any ideas ? Complete code is below

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

Left::KeySet := 1

Down::KeySet := 2

Right::KeySet := 3


#If KeySet = 1
Numpad1::RelativeClick(40, -360)
Numpad2::RelativeClick(160, -360)
Numpad3::RelativeClick(240, -300)
Numpad4::RelativeClick(0, -200)
Numpad5::RelativeClick(200, -200)
Numpad6::RelativeClick(350, -200)
Numpad7::RelativeClick(0, -60)
Numpad8::RelativeClick(200, -60)
Numpad9::RelativeClick(350, -60)
NumpadDiv::RelativeClick(350, -60)
NumpadMult::RelativeClick(350, -60)
NumpadSub::RelativeClick(350, -60)
NumpadPgUp::RelativeClick(350, -60)
NumpadPgDn::RelativeClick(350, -60)
NumpadHome::RelativeClick(350, -60)
NumpadEnd::RelativeClick(350, -60)



#If KeySet = 2
Numpad1::RelativeClick(40, -360)
Numpad2::RelativeClick(160, -360)
Numpad3::RelativeClick(240, -300)
Numpad4::RelativeClick(0, -200)
Numpad5::RelativeClick(200, -200)
Numpad6::RelativeClick(350, -200)
Numpad7::RelativeClick(0, -60)
Numpad8::RelativeClick(200, -60)
Numpad9::RelativeClick(350, -60)



#If KeySet = 3
Numpad1::RelativeClick(40, -360)
Numpad2::RelativeClick(240, -360)
Numpad3::RelativeClick(0, -200)
Numpad4::RelativeClick(240, -200)
Numpad5::RelativeClick(0, -100)
Numpad6::RelativeClick(240, -100)
Numpad7::RelativeClick(0, -60)
Numpad8::RelativeClick(350, -60)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: JoeWinograd, Mannaia666 and 140 guests