How to move the mouse cursor ?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Marcosa1020
Posts: 168
Joined: 23 Sep 2015, 19:15

How to move the mouse cursor ?

23 Apr 2017, 09:52

Hi,
When I enter 100 in x and y then press F4, how to move the mouse cursor?
Thank you.

Code: Select all

Gui +AlwaysOnTop
Gui, Color, C000000
Gui +LastFound
WinSet, Transparent, 250
Gui, Font, S10 C000000 , Tahoma
Gui, Show, x131 y91 h90 w200,Mouse
Gui, Add, Text, x4 y50 c00FFFF, X:
Gui, Add, Edit, x27 y50 w30 h24 vcardx
Gui, Add, Text, x65 y50 c00FFFF, Y:
Gui, Add, Edit, x87 y50 w30 h24 vcardy

#Persistent
WinGet, a_ID, ID, A
SetTimer, aa, 500
 
return

aa:
WinGet, a1_ID, ID, A
If ( a_ID != a1_ID )
k_ID = %a1_ID%
return

F4::
MouseMove, %cardx%, %cardy%, 0
return

GuiClose:
ExitApp
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: How to move the mouse cursor ?

23 Apr 2017, 10:37

Hi,

You can save the content of each control to its associated variable (e.g. cardx) before calling MouseMove command using Gui, submit. Also, if you are using MouseMove command, you should set CoordMode(see: CoordMode) in order to make mouse coordinates to be relatives to the active window (or screen at your option according to your aim).

Code: Select all

CoordMode, Mouse, Window ; make mouse coordinates to be ralatives to the active window

Gui, Color, C000000
Gui +AlwaysOnTop +LastFound +HWNDa_ID ; strore in a_ID the GUI ID
WinSet, Transparent, 250
Gui, Font, S10 C000000 , Tahoma
Gui, Show, x131 y91 h90 w200,Mouse
Gui, Add, Text, x4 y50 c00FFFF, X:
Gui, Add, Edit, x27 y50 w30 h24 vcardx
Gui, Add, Text, x65 y50 c00FFFF, Y:
Gui, Add, Edit, x87 y50 w30 h24 vcardy
; #Persistent ; any script that uses the GUI command anywhere is automatically persistent 
SetTimer, aa, 500 
return

aa:
WinGet, a1_ID, ID, A
If ( a_ID != a1_ID )
k_ID = %a1_ID%
return

F4::
Gui, Submit, NoHide ; saves the contents of each control to its associated variable (ex: vcardx) and hides the window unless the NoHide option is present (see documentation)
if (cardx != "" and cardy != "")
	MouseMove, %cardx%, %cardy%, 0 ; R  -- uncomment the R if you want the X and Y coordinates to be be treated as offsets from the current mouse position
return

GuiClose:
ExitApp
Hope this helps.


EDIT:
you also can replace you timer subroutine by this one, more efficient:

Code: Select all

SetTimer, aa, -100 ; If timer period is negative, the timer will run only once (since the subroutine below specified a loop)
return

aa:
Loop {
WinGet, this_ID, ID, A ; get ID of active window 
WinWaitNotActive, ahk_id %this_ID% ; wait until the active window is not active.
if not (WinActive("ahk_id " . a_ID)) ; if it's not GUI
k_ID := WinExist() ; returns the ID of the last found window and stores it in k_ID
}
return
Last edited by A_AhkUser on 23 Apr 2017, 21:13, edited 1 time in total.
my scripts
Marcosa1020
Posts: 168
Joined: 23 Sep 2015, 19:15

Re: How to move the mouse cursor ?

23 Apr 2017, 20:35

A_AhkUser wrote:Hi,

You can save the content of each control to its associated variable (e.g. cardx) before calling MouseMove command using Gui, submit. Also, if you are using MouseMove command, you should set CoordMode(see: CoordMode) in order to make mouse coordinates to be relatives to the active window (or screen at your option according to your aim).

Code: Select all

CoordMode, Untitled - Notepad, Window ; make mouse coordinates to be ralatives to the active window

Gui, Color, C000000
Gui +AlwaysOnTop +LastFound +HWNDa_ID ; strore in a_ID the GUI ID
WinSet, Transparent, 250
Gui, Font, S10 C000000 , Tahoma
Gui, Show, x131 y91 h90 w200,Mouse
Gui, Add, Text, x4 y50 c00FFFF, X:
Gui, Add, Edit, x27 y50 w30 h24 vcardx
Gui, Add, Text, x65 y50 c00FFFF, Y:
Gui, Add, Edit, x87 y50 w30 h24 vcardy
; #Persistent ; any script that uses the GUI command anywhere is automatically persistent 
SetTimer, aa, 500 
return

aa:
WinGet, a1_ID, ID, A
If ( a_ID != a1_ID )
k_ID = %a1_ID%
return

F4::
Gui, Submit, NoHide ; saves the contents of each control to its associated variable (ex: vcardx) and hides the window unless the NoHide option is present (see documentation)
if (cardx != "" and cardy != "")
	MouseMove, %cardx%, %cardy%, 0 ; R  -- uncomment the R if you want the X and Y coordinates to be be treated as offsets from the current mouse position
return

GuiClose:
ExitApp
Hope this helps.


EDIT:
you also can replace you timer subroutine by this one, more efficient:

Code: Select all

SetTimer, aa, -100 ; If timer period is negative, the timer will run only once (since the subroutine below specified a loop)
return

aa:
Loop {
WinGet, this_ID, ID, A ; get ID of active window 
WinWaitNotActive, ahk_id %this_ID% ; wait until the active window is not active.
if not (WinActive("ahk_id " . a_ID)) ; if it's not GUI
k_ID := WinExist() ; returns the ID of the last found window and stores it in k_ID
}
return
Thank you so much.
If I have open the Mouse.exe(AHK) then enter 100 in x and y, press F4 then open the Notepad. How to move cursor in Notepad window only?

Code: Select all

CoordMode, Untitled - Notepad, Window ; make mouse coordinates to be ralatives to the active window

Gui, Color, C000000
Gui +AlwaysOnTop +LastFound +HWNDa_ID ; strore in a_ID the GUI ID
WinSet, Transparent, 250
Gui, Font, S10 C000000 , Tahoma
Gui, Show, x131 y91 h90 w200,Mouse
Gui, Add, Text, x4 y50 c00FFFF, X:
Gui, Add, Edit, x27 y50 w30 h24 vcardx
Gui, Add, Text, x65 y50 c00FFFF, Y:
Gui, Add, Edit, x87 y50 w30 h24 vcardy
; #Persistent ; any script that uses the GUI command anywhere is automatically persistent 
SetTimer, aa, 500 
return

aa:
WinGet, a1_ID, ID, A
If ( a_ID != a1_ID )
k_ID = %a1_ID%
return

F4::
Gui, Submit, NoHide ; saves the contents of each control to its associated variable (ex: vcardx) and hides the window unless the NoHide option is present (see documentation)
if (cardx != "" and cardy != "")
	MouseMove, %cardx%, %cardy%, 0 ; R  -- uncomment the R if you want the X and Y coordinates to be be treated as offsets from the current mouse position
return

GuiClose:
ExitApp
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: How to move the mouse cursor ?

23 Apr 2017, 21:05

Marcosa1020 wrote:How to move cursor in Notepad window only?
You can either make your hotkey context-sensitive:

Code: Select all

#IfWinActive ahk_class Notepad ; creates context-sensitive hotkey, which one depending now on whether or not Notepad is the active window
; run AU3_Spy (which is located in Authotkey.exe own directory) in order to retrieve windows' classes (like Notepad for Notepad.exe), just hovering them with your mouse
F4::
; ...
return
... or add a condition before actually execute the MouseMove command:

Code: Select all

if (WinActive("ahk_class Notepad") and cardx != "" and cardy != "")
	MouseMove, %cardx%, %cardy%, 0 ; R  -- uncomment the R if you want the X and Y coordinates to be be treated as offsets from the current mouse position
I suggest you - if you only intend to have the mousemove command within your subroutine -, using the first option since this one check if the window is active before even actually trigger the subroutine.


Also, I mean well by indicating you to replace you timer subroutine by another, more efficient, but my bad you should not since timers operate by temporarily interrupting the script's current activity, and their subroutines should be kept short.

Hope this helps.
my scripts
Marcosa1020
Posts: 168
Joined: 23 Sep 2015, 19:15

Re: How to move the mouse cursor ?

24 Apr 2017, 07:57

A_AhkUser wrote:
Marcosa1020 wrote:How to move cursor in Notepad window only?
You can either make your hotkey context-sensitive:

Code: Select all

#IfWinActive ahk_class Notepad ; creates context-sensitive hotkey, which one depending now on whether or not Notepad is the active window
; run AU3_Spy (which is located in Authotkey.exe own directory) in order to retrieve windows' classes (like Notepad for Notepad.exe), just hovering them with your mouse
F4::
; ...
return
... or add a condition before actually execute the MouseMove command:

Code: Select all

if (WinActive("ahk_class Notepad") and cardx != "" and cardy != "")
	MouseMove, %cardx%, %cardy%, 0 ; R  -- uncomment the R if you want the X and Y coordinates to be be treated as offsets from the current mouse position
I suggest you - if you only intend to have the mousemove command within your subroutine -, using the first option since this one check if the window is active before even actually trigger the subroutine.


Also, I mean well by indicating you to replace you timer subroutine by another, more efficient, but my bad you should not since timers operate by temporarily interrupting the script's current activity, and their subroutines should be kept short.

Hope this helps.

Hi A_AhkUser, Thank you for your reply.
So I summary the code, It looks like is below ?

Code: Select all

CoordMode, Mouse, Window ; make mouse coordinates to be ralatives to the active window
Gui, Color, C000000
Gui +AlwaysOnTop +LastFound +HWNDa_ID ; strore in a_ID the GUI ID
WinSet, Transparent, 250
Gui, Font, S10 C000000 , Tahoma
Gui, Show, x131 y91 h90 w200,Mouse
Gui, Add, Text, x4 y50 c00FFFF, X:
Gui, Add, Edit, x27 y50 w30 h24 vcardx
Gui, Add, Text, x65 y50 c00FFFF, Y:
Gui, Add, Edit, x87 y50 w30 h24 vcardy

SetTimer, aa, 500 
return

aa:
WinGet, a1_ID, ID, A
If ( a_ID != a1_ID )
k_ID = %a1_ID%
return

#IfWinActive ahk_class Notepad
F4::
Gui, Submit, NoHide
If (cardx != "" and cardy != "")
	MouseMove, %cardx%, %cardy%, 0
return

GuiClose:
ExitApp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 382 guests