MouseMove doesn't work in function() Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
uNseen
Posts: 26
Joined: 16 Sep 2016, 06:12

MouseMove doesn't work in function()

01 Jan 2024, 13:01

Hi!

I have a basic script where mouse moves to certain location and then clicks. I use strings to set the coords. And the problem is that mouse doesn't move when i use strings in the code:

Code: Select all

x1 := 640
y1 := 355

$F1::Function()

Function()
{
BlockInput, MouseMove
  Send {RButton} 
  Send {ESC}
  Sleep, 50
  MouseMove, %x1%, %y1%, 0
  Send {click}
  Sleep, 50
BlockInput, MouseMoveOff
}
return
The script works fine when i don't use strings to set the coords:
How to pass string variables to function to make it work?
User avatar
andymbody
Posts: 906
Joined: 02 Jul 2017, 23:47

Re: MouseMove doesn't work in function()  Topic is solved

01 Jan 2024, 13:32

Declare the variables as global, so they are accessible in the function
global x1 := 640, y1 := 355

And just fyi, the % signs are not required
MouseMove x1, y1
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: MouseMove doesn't work in function()

01 Jan 2024, 13:38

How to pass string variables to function to make it work?
Use function parameters:

Code: Select all

x1 := 640
y1 := 355

$F1::Function(x1,y1)

Function(x1,y1)
{
BlockInput, MouseMove
  Send {RButton} 
  Send {ESC}
  Sleep, 50
  MouseMove, %x1%, %y1%, 0
  Send {click}
  Sleep, 50
BlockInput, MouseMoveOff
}
or:

Code: Select all

$F1::Function(640,355)

Function(x1,y1)
{
BlockInput, MouseMove
  Send {RButton} 
  Send {ESC}
  Sleep, 50
  MouseMove, %x1%, %y1%, 0
  Send {click}
  Sleep, 50
BlockInput, MouseMoveOff
}
User avatar
andymbody
Posts: 906
Joined: 02 Jul 2017, 23:47

Re: MouseMove doesn't work in function()

01 Jan 2024, 13:47

Sorry, I didn't see the final note about passing the values to the function. Global will work but the preferred method is shown by @Xtra.
uNseen
Posts: 26
Joined: 16 Sep 2016, 06:12

Re: MouseMove doesn't work in function()

01 Jan 2024, 14:12

andymbody wrote:
01 Jan 2024, 13:32
Declare the variables as global, so they are accessible in the function
global x1 := 640, y1 := 355

And just fyi, the % signs are not required
MouseMove x1, y1
This actually worked for me. Thank you.


Xtra wrote:
01 Jan 2024, 13:38
How to pass string variables to function to make it work?
Use function parameters:

Code: Select all

x1 := 640
y1 := 355

$F1::Function(x1,y1)

Function(x1,y1)
{
BlockInput, MouseMove
  Send {RButton} 
  Send {ESC}
  Sleep, 50
  MouseMove, %x1%, %y1%, 0
  Send {click}
  Sleep, 50
BlockInput, MouseMoveOff
}
I knew about this method, but how to implemented it when there are several coord positions? e.g. x1, y1; x2, y2; x3, y3 etc...


Also, as i saw in the post written by andymbody, you can set global param this way: global x1 := 700, y1 := 500, x2 := 600, y2 := 400, x3 := 500, y3 := 300 and so on (in a single row). Is there a way to set global param for strings written in a column? For example:

Code: Select all

Global start
x1 := 700
y1 := 500

x2 := 600
y2 := 400

x3 := 500
y3 := 300
Global end
(it's just an example to get an idea of what i'm looking for, i know it's not right)
User avatar
andymbody
Posts: 906
Joined: 02 Jul 2017, 23:47

Re: MouseMove doesn't work in function()

01 Jan 2024, 18:04

uNseen wrote:
01 Jan 2024, 14:12
Is there a way to set global param for strings variables written in a column?
Can do this...

Code: Select all

Global x1,y1,x2,y2,x3,y3	; Super-Global - all functions have access by default
x1 := 700
y1 := 500

x2 := 600
y2 := 400

x3 := 500
y3 := 300
OR this (same as above, just written differently)...

Code: Select all

Global    x1 := 700
		, y1 := 500

		, x2 := 600
		, y2 := 400

		, x3 := 500
		, y3 := 300

Another option would be... to not worry about declaring the main body variables as global (not make them Super-Global), and instead use a Global keyword in the function. Like so...

Code: Select all

; main body variables - not super-global, but can be accessed by declaring Global in function
x1 := 700
y1 := 500
x2 := 600
y2 := 400
return
function()
{
	Global	; makes vars in this function global scope by default (unless otherwise declared local or static)
	MsgBox % "[" . y2 . "]"	; will access global value set outside of function
}
OR... you can set each variable to Global individually within the function if you would rather keep the normal default for function vars (local by default unless otherwise declared).

Code: Select all

; main body variables - not super-global, but can be accessed by declaring Global in function
x1 := 700
y1 := 500
x2 := 600
y2 := 400
return
function()
{
	Global x1,y1			; these will access the values set outside function
	MsgBox % "[" . y1 . "]"	; will access global value
	MsgBox % "[" . y2 . "]"	; this is still a LOCAL variable, since not declared global above
}
uNseen
Posts: 26
Joined: 16 Sep 2016, 06:12

Re: MouseMove doesn't work in function()

01 Jan 2024, 20:27

@andymbody, tyvm

This one seems ideal:
andymbody wrote:
01 Jan 2024, 18:04
Another option would be... to not worry about declaring the main body variables as global (not make them Super-Global), and instead use a Global keyword in the function. Like so...

Code: Select all

; main body variables - not super-global, but can be accessed by declaring Global in function
x1 := 700
y1 := 500
x2 := 600
y2 := 400
return
function()
{
	Global	; makes vars in this function global scope by default (unless otherwise declared local or static)
	MsgBox % "[" . y2 . "]"	; will access global value set outside of function
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: KolaBorat and 79 guests