Capturing and saving mouse clicks

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Noel Pil
Posts: 5
Joined: 23 Feb 2020, 15:30

Capturing and saving mouse clicks

07 Mar 2020, 07:30

Hi.
This is my first code, and I'm trying to capture and save mouse clicks but the code below I tried, does not give just one mouse position at every click but many.
How can I solve this?
Thanks

Code: Select all

FileCreateDir, C:\mouse-click\
FileCreateDir, C:\mouse-click_drag\
FileCreateDir, C:\mouse-double_click\

loop{
KeyWait, LButton 
 {
  MouseGetPos, xdown, ydown 
  FileAppend, %xdown%`n, C:\mouse-click\mouse_click.txt
  FileAppend, %ydown%`n, C:\mouse-click\mouse_click.txt
 }
}
vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Re: Capturing and saving mouse clicks

07 Mar 2020, 08:48

Code: Select all

~LButton::
Keywait,LButton
MouseGetPos,X,Y%
FileAppend,% x "`n" y, C:\mouse-click\mouse_click.txt
Return
Your script is constantly writing to the text file because you don't have the keywait is waiting for LButton to be released
The above writes to the file only when you release the lbutton after clicking
Noel Pil
Posts: 5
Joined: 23 Feb 2020, 15:30

Re: Capturing and saving mouse clicks

07 Mar 2020, 12:25

Thank you.
The mouse_click code saves perfect the way I want to.
I am trying to solve the mouse drag now.
This is what I get until now (after some hours)
Hope you can help me with this last part.
Thanks again

Code: Select all

FileCreateDir, C:\mouse-click\
FileCreateDir, C:\mouse-click_drag\

~LButton::
Keywait,LButton
MouseGetPos,X,Y
FileAppend,%x%`n%y%`n, C:\mouse-click\mouse_click.txt

loop {
  if GetKeyState("LButton", "D") {
    MouseGetPos,x,y
    ToolTip,%x% %y%,x-50,y
    ;FileAppend,%x%`n%y%`n, C:\mouse-click_drag\mouse_click_drag.txt
  }
Return
}
vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Re: Capturing and saving mouse clicks

07 Mar 2020, 13:05

The added part with the loop will not work and you are just making your cpu do pointless work.

What exactly do you want to do with the mouse drag
Get the start position and the position when you release the mouse button or what and what do you want to save into the file...the mouse down and up position or only the up position?

You will probably ask for double click too

This saves double clicks position(the double click are determined as double clicks if the time between clicks is less than 200ms)
Single clicks position
Mouse click down and up position when dragging

Code: Select all

~LButton::
MouseGetPos,SX,SY
Keywait,LButton
MouseGetPos,X,Y
If ((SX != X) or (SY != SY))
FileAppend,% SX "`n" SY "`n" X "`n" Y "`n`n" , C:\mouse-click_drag\mouse_click_drag.txt
If ((A_TimeSincePriorHotkey < 200) & (A_PriorKey = "LButton"))
FileAppend,% x "`n" y "`n", C:\mouse-double_click\mouse_doubleclick.txt
Else
FileAppend,% x "`n" y "`n", C:\mouse-click\mouse_click.txt
Return
Not tested
Noel Pil
Posts: 5
Joined: 23 Feb 2020, 15:30

Re: Capturing and saving mouse clicks

07 Mar 2020, 15:47

I need all the dragged pixel coordinates saved to be used in a Processing sketch to form a path.
I think it's odd that AutoHotkey hasn't a mouseDragged function. Processing has, but only captures in its own window.
I know the loop takes up to 15% processor power, but it's only for a short time I need it.
I used your solution to differentiate a simple mouse click from drag, but the loop is too fast and duplicates many equal pixels.
Do you have an idea to solve that?

Code: Select all

loop {
  MouseGetPos,SX,SY
  if GetKeyState("LButton", "D") {
    MouseGetPos,X,Y
    If((SX != X) or (SY != Y)) 
    ;ToolTip,%x% %y%,x-50,y
    FileAppend,%x%`n%y%`n, C:\mouse-click_drag\mouse_click_drag.txt
  }
}
vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Re: Capturing and saving mouse clicks

07 Mar 2020, 17:12

Sorry but do you know what your change actually do?
If this is your whole script(without LButton as a hotkey to start the loop

If you don't have a hotey set,the loop will constantly get the mouse position(I think every 20ms)and do nothing until press LButton and after that since the next condition is "if GetKeyState("LButton", "D")" which means the left mouse button,the script will get the same coordinates as SX and SY even if you move the mouse and since you are getting the same values,the " If((SX != X) or (SY != Y)) " will never be true

AHK does have a click and drag feature but not in a single function(as far as I know)

Open paint and press F1 after you run this script and do a dragging once(click somewhere and move the mouse as many times as you want).
When you release the button,press F1 to stop the recording and the code with your last mouse drag will be in the clipboard,ready to be pasted into a ahk file and run it by pressing Esc when the paint window is active(while the repeating code is running,your mouse movements will be blocked until the script is done dragging)

Code: Select all

F1::
Keywait,F1
If Recording = 1
{
Tooltip,
Recording =
Return
}
Recording += 1
Tooltip,Recording,0,0
Return

#If (Recording = "1")
~LButton::
List =
MM =
WinGet,EXE,Processname,A
MouseGetPos,SX,SY
While GetKeyState("LButton","D")
{
MouseGetPos,X,Y
If ((X != X2) or (Y != Y2))
List .= X "|" Y "`n"
MouseGetPos,X2,Y2
}
Loop,Parse,List,`n
{
If A_LoopField =
Break
StringSplit,PoS,A_LoopField,|
If A_Index = 1
MM .= "#IfWinActive,ahk_exe " EXE "`nEsc::`nKeywait,Esc`nBlockInput,MouseMove`nMouseMove," Pos1 "," PoS2 ",0`nSend,{LButton Down}`n"
Else
MM .= "MouseMove," Pos1 "," PoS2 ",0`n"
}
MM .= "Send,{LButton Up}`nBlockInput,MouseMoveOff`nReturn"
Clipboard := MM
REturn
vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Re: Capturing and saving mouse clicks

07 Mar 2020, 17:43

This version will repeat everything you just did and create a ready code into the clipboard(clicking on a position,double clicking on a position and dragging from and to a position)

F1 to start the recording=>use lbutton as many times as you want=>F1 to stop the recording=>create an AHK file with the code(inside the clipboard)=>run the script and press Esc while the same window is active

Code: Select all

F1::
Keywait,F1
If Recording = 1
{
Tooltip,
Recording =
Return
}
Recording += 1
Tooltip,Recording,0,0
MM =
Return

#If (Recording = "1")
~LButton::
List =
; MM =
WinGet,EXE,Processname,A
MouseGetPos,SX,SY
While GetKeyState("LButton","D")
{
MouseGetPos,X,Y
If ((X != X2) or (Y != Y2))
List .= X "|" Y "`n"
MouseGetPos,X2,Y2
}

Loop,Parse,List,`n
{
If A_LoopField =
Break
StringSplit,PoS,A_LoopField,|
If A_Index = 1
{
If MM contains ahk_exe
{
StringTrimRight,MM,MM,30
MM .= "`nMouseMove," Pos1 "," PoS2 ",0`nSend,{LButton Down}`n"
}
Else
MM .= "#IfWinActive,ahk_exe " EXE "`nEsc::`nKeywait,Esc`nBlockInput,MouseMove`nMouseMove," Pos1 "," PoS2 ",0`nSend,{LButton Down}`n"
}
Else
MM .= "MouseMove," Pos1 "," PoS2 ",0`n"
}
MM .= "Send,{LButton Up}`nBlockInput,MouseMoveOff`nReturn"
Clipboard := MM
REturn
Pressing F1 again to start a new recording will clear the old one
It can literally redraw a whole picture if all elements of the window are at the exact place as before and the window size is at the same size(works best with maximized)
Noel Pil
Posts: 5
Joined: 23 Feb 2020, 15:30

Re: Capturing and saving mouse clicks

14 Mar 2020, 04:31

Hi. I've been ill for some days,, and was not able to thank you..
But yes, thank you very much for this nice code.
It works perfect, and I learned a lot.
If yoy want to see the reason and result of the code in Processing, see

discourse.processing.org/t/help-to-create-image-from-interaction-with-website/18315/7?u=noel

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Mateusz53, mikeyww and 299 guests