Record a mouse position to a key

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
arbibarbarona
Posts: 63
Joined: 05 May 2021, 10:19

Record a mouse position to a key

Post by arbibarbarona » 15 May 2021, 11:23

Hi everyone. I have a question if this is possible. and I may need help on these.

I wanted to record an x/y location of the mouse pointer.

Example:
use case scenario. I hover a mouse position then press Ctrl + Numpad1 to record that mouse position to bind in Numpad1, Then hover again another location and press Ctrl + Numpad2 saves that mouse location in Numpad 2. when I press Numpad1 mouse move immediately to the recorded location and numpad2 and so on. by doing so consecutively I can have many mouse locator bound to the keys I want. Is this possible or can somebody help me code this?

Thank you so much.
User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Record a mouse position to a key

Post by mikeyww » 15 May 2021, 11:33

Code: Select all

pos := {}
Numpad1::
Numpad2::
Numpad3::
Numpad4::
Numpad5::
Numpad6::
Numpad7::
Numpad8::
Numpad9::MouseMove, pos[A_ThisHotkey, 1], pos[A_ThisHotkey, 2]
^Numpad1::
^Numpad2::
^Numpad3::
^Numpad4::
^Numpad5::
^Numpad6::
^Numpad7::
^Numpad8::
^Numpad9::
MouseGetPos, x, y
item := SubStr(A_ThisHotkey, 2), pos[item, 1] := x, pos[item, 2] := y
SoundBeep, 1500
Return
arbibarbarona
Posts: 63
Joined: 05 May 2021, 10:19

Re: Record a mouse position to a key

Post by arbibarbarona » 15 May 2021, 12:00

mikeyww wrote:
15 May 2021, 11:33

Code: Select all

pos := {}
Numpad1::
Numpad2::
Numpad3::
Numpad4::
Numpad5::
Numpad6::
Numpad7::
Numpad8::
Numpad9::MouseMove, pos[A_ThisHotkey, 1], pos[A_ThisHotkey, 2]
^Numpad1::
^Numpad2::
^Numpad3::
^Numpad4::
^Numpad5::
^Numpad6::
^Numpad7::
^Numpad8::
^Numpad9::
MouseGetPos, x, y
item := SubStr(A_ThisHotkey, 2), pos[item, 1] := x, pos[item, 2] := y
SoundBeep, 1500
Return
Thank you so much. This is a great Help. I can't thank you enough.
arbibarbarona
Posts: 63
Joined: 05 May 2021, 10:19

Re: Record a mouse position to a key

Post by arbibarbarona » 15 May 2021, 12:15

mikeyww wrote:
15 May 2021, 11:33

Code: Select all

pos := {}
Numpad1::
Numpad2::
Numpad3::
Numpad4::
Numpad5::
Numpad6::
Numpad7::
Numpad8::
Numpad9::MouseMove, pos[A_ThisHotkey, 1], pos[A_ThisHotkey, 2]
^Numpad1::
^Numpad2::
^Numpad3::
^Numpad4::
^Numpad5::
^Numpad6::
^Numpad7::
^Numpad8::
^Numpad9::
MouseGetPos, x, y
item := SubStr(A_ThisHotkey, 2), pos[item, 1] := x, pos[item, 2] := y
SoundBeep, 1500
Return
Thank you so much.
User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Record a mouse position to a key

Post by mikeyww » 15 May 2021, 13:44

You are welcome. Have fun with it!
arbibarbarona
Posts: 63
Joined: 05 May 2021, 10:19

Re: Record a mouse position to a key

Post by arbibarbarona » 16 May 2021, 05:01

Is it possible to add to the code while pressing the arrow left or right it will cycle through the selected numpad?
User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Record a mouse position to a key

Post by mikeyww » 16 May 2021, 06:51

Code: Select all

pos := {}
Numpad1::
Numpad2::
Numpad3::
Numpad4::
Numpad5::
Numpad6::
Numpad7::
Numpad8::
Numpad9::move(pos, SubStr(A_ThisHotkey, 0))
^Numpad1::
^Numpad2::
^Numpad3::
^Numpad4::
^Numpad5::
^Numpad6::
^Numpad7::
^Numpad8::
^Numpad9::
MouseGetPos, x, y
item := SubStr(A_ThisHotkey, 0), pos[item, 1] := x, pos[item, 2] := y
SoundBeep, 1500
Return

Left::
Right::
If !item
 Return
Loop
 item := A_ThisHotkey = "Left" ? (--item ? item : 9) : (item++ - 9 ? item : 1)
Until (pos[item].1 > "")
move(pos, item)
Return

move(array, item) {
 MouseMove, array[item].1, array[item].2
}
arbibarbarona
Posts: 63
Joined: 05 May 2021, 10:19

Re: Record a mouse position to a key

Post by arbibarbarona » 16 May 2021, 07:58

Is the Arrow selector limits to 9 selection only?
arbibarbarona
Posts: 63
Joined: 05 May 2021, 10:19

Re: Record a mouse position to a key

Post by arbibarbarona » 16 May 2021, 07:59

Say I wanted to add keys...

Code: Select all

pos := {}
F1::
F2::
F3::
F4::
F5::
F6::
F7::
F8::
F9::
F10::
F11::
F12::
Numpad0::
Numpad1::
Numpad2::
Numpad3::
Numpad4::
Numpad5::
Numpad6::
Numpad7::
Numpad8::
Numpad9::move(pos, SubStr(A_ThisHotkey, 0))
^F1::
^F2::
^F3::
^F4::
^F5::
^F6::
^F7::
^F8::
^F9::
^F10::
^F11::
^F12::
^Numpad0::
^Numpad1::
^Numpad2::
^Numpad3::
^Numpad4::
^Numpad5::
^Numpad6::
^Numpad7::
^Numpad8::
^Numpad9::
MouseGetPos, x, y
item := SubStr(A_ThisHotkey, 0), pos[item, 1] := x, pos[item, 2] := y
SoundBeep, 1500
Return

Left::
Right::
If !item
 Return
Loop
 item := A_ThisHotkey = "Left" ? (--item ? item : 9) : (item++ - 9 ? item : 1)
Until (pos[item].1 > "")
move(pos, item)
Return

move(array, item) {
 MouseMove, array[item].1, array[item].2
}
User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Record a mouse position to a key

Post by mikeyww » 16 May 2021, 08:44

I will leave this third round of changes to you. Perhaps you can see some patterns here in the script. The idea is that you have an array with elements that reflect each of the hotkeys. You store the mouse position in the array element that corresponds to the (Ctrl) hotkey that was pressed. You can then retrieve that position from the array when the non-Ctrl hotkey is pressed. You could use hotkey names for the element names. You could then have a separate master array that names the keys, so that the script could cycle through them as needed.
arbibarbarona
Posts: 63
Joined: 05 May 2021, 10:19

Re: Record a mouse position to a key

Post by arbibarbarona » 16 May 2021, 08:53

mikeyww wrote:
16 May 2021, 08:44
I will leave this third round of changes to you. Perhaps you can see some patterns here in the script. The idea is that you have an array with elements that reflect each of the hotkeys. You store the mouse position in the array element that corresponds to the (Ctrl) hotkey that was pressed. You can then retrieve that position from the array when the non-Ctrl hotkey is pressed. You could use hotkey names for the element names. You could then have a separate master array that names the keys, so that the script could cycle through them as needed.
Thank you so much.
arbibarbarona
Posts: 63
Joined: 05 May 2021, 10:19

Re: Record a mouse position to a key

Post by arbibarbarona » 29 May 2021, 09:49

Code: Select all

pos := {}
F1::
F2::
F3::
F4::
F5::
F6::
F7::
F8::
F9::
F10::
F11::
F12::
Numpad0::
Numpad1::
Numpad2::
Numpad3::
Numpad4::
Numpad5::
Numpad6::
Numpad7::
Numpad8::
Numpad9::MouseMove, pos[A_ThisHotkey, 1], pos[A_ThisHotkey, 2]
^F1::
^F2::
^F3::
^F4::
^F5::
^F6::
^F7::
^F8::
^F9::
^F10::
^F11::
^F12::
^Numpad0::
^Numpad1::
^Numpad2::
^Numpad3::
^Numpad4::
^Numpad5::
^Numpad6::
^Numpad7::
^Numpad8::
^Numpad9::
MouseGetPos, x, y
item := SubStr(A_ThisHotkey, 2), pos[item, 1] := x, pos[item, 2] := y
SoundBeep, 1500
Return
I have a question regarding the Above Code.
Pressing CTRl (^) + key above will allow me to store into 9 key positions only. If I exceed 9 keys, the first Keybind will be override by the another keybind, Thus giving me only total of 9 Key positions. How can I save all the Key positions indicated more than 9 keys?
Thank you.
User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Record a mouse position to a key

Post by mikeyww » 29 May 2021, 11:20

Code: Select all

pos := {}
*F1::
*F2::
*F3::
*F4::
*F5::
*F6::
*F7::
*F8::
*F9::
*F10::
*F11::
*F12::
*Numpad0::
*Numpad1::
*Numpad2::
*Numpad3::
*Numpad4::
*Numpad5::
*Numpad6::
*Numpad7::
*Numpad8::
*Numpad9::
If GetKeyState("Ctrl") {
 MouseGetPos, x, y
 pos[A_ThisHotkey, 1] := x, pos[A_ThisHotkey, 2] := y
 SoundBeep, 1500
} Else MouseMove, pos[A_ThisHotkey, 1], pos[A_ThisHotkey, 2]
Return
arbibarbarona
Posts: 63
Joined: 05 May 2021, 10:19

Re: Record a mouse position to a key

Post by arbibarbarona » 30 May 2021, 00:17

Thank you.
arbibarbarona
Posts: 63
Joined: 05 May 2021, 10:19

Re: Record a mouse position to a key

Post by arbibarbarona » 14 Nov 2022, 13:28

Need Help.

Code: Select all

Global pos := {}, ini := StrReplace(A_ScriptFullPath, ".ahk", ".ini")
OnExit("save")
IniRead, keyNames, %ini%
For each, key in StrSplit(keyNames, "`n") {
 IniRead, section, %ini%, %key%
 For each2, line in StrSplit(section, "`n")
  part := StrSplit(line, "="), pos[key, part.1] := part.2
}

*F1::
*F2::
*F3::
*F4::
*F5::
*F6::
*F7::
*F8::
*F9::
*F10::
*F11::
*F12::
*Numpad0::
*Numpad1::
*Numpad2::
*Numpad3::
*Numpad4::
*Numpad5::
*Numpad6::
*Numpad7::
*Numpad8::
*Numpad9::
If GetKeyState("Ctrl") {
 MouseGetPos, x, y
 pos[A_ThisHotkey, 1] := x, pos[A_ThisHotkey, 2] := y
 SoundBeep, 1500
} Else MouseMove, pos[A_ThisHotkey, 1], pos[A_ThisHotkey, 2]
Return

Is it still possible to Add these lines to the Script above?

Code: Select all

Left::
Right::
If !item
 Return
Loop
 item := A_ThisHotkey = "Left" ? (--item ? item : 9) : (item++ - 9 ? item : 1)
Until (pos[item].1 > "")
move(pos, item)
Return

move(array, item) {
 MouseMove, array[item].1, array[item].2
}
My intention is if I press Arrow Left / right I can tab or select and go to the next saved Mouse coordinate or back.
I can't seem to get it work.
User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Record a mouse position to a key

Post by mikeyww » 14 Nov 2022, 15:11

You may want to revert to a previous version. viewtopic.php?p=399979#p399979

This uses an array key corresponding to the digit of the keyboard key.
arbibarbarona
Posts: 63
Joined: 05 May 2021, 10:19

Re: Record a mouse position to a key

Post by arbibarbarona » 14 Nov 2022, 18:30

Thanks Mike, but I wanted to save the Coordinate in a separate ini file... is this possible with the old script?
User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Record a mouse position to a key

Post by mikeyww » 14 Nov 2022, 18:55

Below is an example that you can adjust.

Code: Select all

Global ini := StrReplace(A_ScriptFullPath, ".ahk", ".ini"), pos := {}
IniRead, sectionList, %ini%
For each, digit in StrSplit(sectionList, "`n")
 Loop, 2 {
  IniRead, coord, %ini%, %digit%, %A_Index%
  pos[digit, XY := A_Index] := coord
 }
OnExit("save")

Numpad1::
Numpad2::
Numpad3::
Numpad4::
Numpad5::
Numpad6::
Numpad7::
Numpad8::
Numpad9::move(pos, SubStr(A_ThisHotkey, 0))
^Numpad1::
^Numpad2::
^Numpad3::
^Numpad4::
^Numpad5::
^Numpad6::
^Numpad7::
^Numpad8::
^Numpad9::
MouseGetPos, x, y
item := SubStr(A_ThisHotkey, 0), pos[item, 1] := x, pos[item, 2] := y
SoundBeep, 1500
Return

Left::
Right::
If !item
 Return
Loop
 item := A_ThisHotkey = "Left" ? (--item ? item : 9) : (item++ - 9 ? item : 1)
Until (pos[item].1 > "")
move(pos, item)
Return

move(array, item) {
 MouseMove, array[item].1, array[item].2
}

save(exitReason, exitCode) {
 For digit, set in pos
  For xy, coord in set
   IniWrite, %coord%, %ini%, %digit%, %xy%
}
Post Reply

Return to “Ask for Help (v1)”