Jump to content

Mouse Coordinate Mapper


ScottL
 Share

Recommended Posts

I had a need to map out mouse positions for something I was doing at work, and started building is script. Then it just became fun to work on.

It records mouse coordinates and writes them to a log file with a label you specify. Upon exiting, you can open the log file and see where you've been.

; Script Author: Scott Lyerly
; Email (h): scott dot c dot lyerly at hotmail dot com

; Includes the three required files for this script to run correctly.
#include <file.au3>
#include <GUIConstants.au3>
#Include <date.au3>

; Declares some global variable for use in any function
Global $input
Global $font
Global $windir
Global $secerror
Global $seclogerror
Global $delim

$font = "Verdana" ; Sets the font to Verdana
$secerror = "You must input a second value between 3 and 10."   ; Sets an error message
$seclogerror = "Error in selection of number of seconds."   ; Sets an error message
$delim = '"'    ; Sets a quote are a variable for use in messages

; Checks to see whether the Windows Folder on the local machine is WINNT or WINDOWS
If FileExists("C:\WINNT") Then
    $windir = "C:\WINNT"
Else
    $windir = "C:\WINDOWS"
EndIf

; Creates and/or overwrites the log file thatis used to track the program's progess.
_FileCreate($windir & "\MousePositionLog.txt")

; GUI CREATION
GUICreate("Mouse Position Mapper", 400, 125)  ; Creates a GUI that is centered when displayed 
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") ; Creates the function that will close the GUI is the Closed button is clicked

; GUI LABELS
GUISetFont (9, 400, 0, $font)   
GUICtrlCreateLabel ("Click CONTINUE to map the mouse coordinates." & @CRLF & _
        "Click FINISHED to end the program.",  10, 40, 300, 40) ; Creates a instructional label for the GUI

GUISetFont (9, 400, 0, $font)   
GUICtrlCreateLabel ("This program will map your mouse coordinates.",  10, 10, 300, 20) ; Creates an instructional label for the GUI

; GUI BUTTONS
GUISetFont (9, 400, 0, $font)
$okbutton = GUICtrlCreateButton("Continue", 10, 90, 70, 25) ; Creates an OK button to continue with the script
GUICtrlSetState(5, $GUI_DEFBUTTON)  ;Sets "Continue" button as the default
GUISetFont (9, 400, 0, $font)
$donebutton = GUICtrlCreateButton("Finished", 90, 90, 70, 25) ; Creates a DONE button to continue with the script

; GUI BUTTON FUNCTIONS 
; Sets the buttons to call specific functions if selected
GUICtrlSetOnEvent($okbutton, "OKButton")
GUICtrlSetOnEvent($okbutton, "Done")

; Shows the GUI to the user.
GUISetState (@SW_SHOW)

; This WHILE...WEND runs until the user makes a choice with one of the buttons.
; Once pressed, it retrieves the GUI message and acts upon it.
While 1
    ; Check if the user clicked something in the GUI window
    $msg = GUIGetMsg()

    ; This SELECT lists the choices (CASEs) that are available depending on the user input.
    Select
        ; Checks if user clicked on the Windows CLOSE button
        Case $msg = $GUI_EVENT_CLOSE
            GUIDelete() ; Destroys the GUI including the controls
            Exit    ; Exits the script
        ; Checks if user clicked on the "OK" button
        Case $msg = $okbutton
            GUISetState (@SW_HIDE)  ; Hides the GUI from the user
            OKButton()
        ; Checks if user clicked the "Done" button
        Case $msg = $donebutton
            Done()
    EndSelect   ; Ends SELECT
WEnd    ; Ends WHILE

; "OKButton" function
Func OKButton()
    ; GUI CREATION
    GUISetFont (9, 400, 0, $font)
    GUICreate("Mouse Position Mapper", 320, 155)  ; Creates a GUI that is centered when displayed 
    GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") ; Creates the function that will close the GUI is the Closed button is clicked
    
    ; GUI INPUT BOX
    GUISetFont (9, 400, 0, $font)
    GUICtrlCreateLabel ("Please enter a name for this position",  10, 10, 300, 20)  ; Creates a label for the input box
    GUISetFont (9, 400, 0, $font)
    $input = GUICtrlCreateInput ( "", 10, 35, 300, 20)  ; Creates an input box for user input
    GUISetFont (9, 400, 0, $font)
    
    ; GUI INPUT BOX
    GUISetFont (9, 400, 0, $font)
    GUICtrlCreateLabel ("Enter No. Seconds Delay",  10, 65, 300, 20)    ; Creates a label for the next input box
    GUISetFont (9, 400, 0, $font)
    $inputud = GUICtrlCreateInput ("3",10 ,90, 40, 20)  ; Creates a second input box
    GUISetFont (9, 400, 0, $font)
    $updown = GUICtrlCreateUpdown($inputud) ; Creates an Up Down control
    GUICtrlSetLimit(4105,2,1)   ; Sets character limit on the second input box
    GUICtrlSetLimit(4106,10,3)  ; Sets a min/max on the Up Down control
    
    ; GUI BUTTONS
    GUISetFont (9, 400, 0, $font)
    $contbutton = GUICtrlCreateButton("Map", 10, 120, 70, 25)   ; Creates the "Map" button
    GUICtrlSetState(4107,$GUI_DEFBUTTON)    ; Sets "Map" button as the default
    GUISetFont (9, 400, 0, $font)
    $donebutton = GUICtrlCreateButton("Finished", 85, 120, 70, 25)  ; Creates the "Finished" button
    
    ; GUI BUTTON FUNCTIONS
    GUICtrlSetOnEvent($contbutton, "ContButton")
    GUICtrlSetOnEvent($donebutton, "Done")

    ; Shows the GUI to the user.
    GUISetState (@SW_SHOW)

    ; This WHILE...WEND runs until the user makes a choice with one of the buttons
    ; Once pressed, it retrieves the GUI message and acts upon it.
    While 1
        ; Check if the user clicked something in the GUI window
        $msg = GUIGetMsg()

        ; This SELECT lists the choices (CASEs) that are available depending on the user input.
        Select
            ; Checks if user clicked on the close button
            Case $msg = $GUI_EVENT_CLOSE
                GUIDelete() ; Destroys the GUI including the controls
                Exit    ; Exits the script
            ; Check if user clicked on the "OK" button
            Case $msg = $contbutton
                GUISetState (@SW_HIDE)  ; Hides the GUI from the user
                ; This declares $logfile be a local variables for use in this function only
                Dim $logfile
                    ; IF statement validating second input box data
                    If GUICtrlRead($inputud) > 10 Then
                        MsgBox(0, "Input Error", $secerror)
                        $logfile = FileOpen($windir & "\MousePositionLog.txt", 1)   ; Opens the log file
                        ; IF statement checking if file opened for writing OK with error handling
                        If $logfile = -1 Then
                            MsgBox(0, "Error", "Unable to open file.")
                            Exit
                        EndIf
                        ; Writes to the log file
                        FileWrite($logfile, _Now() & ": Position labeled '" & GUICtrlRead($input) & ": " & $seclogerror & ": " & $delim & $secerror & $delim & @CRLF)
                        FileClose($logfile) ; Closes the log file
                        OKButton()  ; Calls OKButton function again
                    ElseIf GUICtrlRead($inputud) < 3 Then
                        MsgBox(0, "Input Error", $secerror)
                        $logfile = FileOpen($windir & "\MousePositionLog.txt", 1)   ; Opens the log file
                        ; IF statement checking if file opened for writing OK with error handling
                        If $logfile = -1 Then
                            MsgBox(0, "Error", "Unable to open file.")
                            Exit
                        EndIf
                        ; Writes to the log file
                        FileWrite($logfile, _Now() & ": Position labeled '" & GUICtrlRead($input) & ": " & $seclogerror & ": " & $delim & $secerror & $delim & @CRLF)
                        FileClose($logfile) ; Closes the log file
                        OKButton()  ; Calls OKButton function again
                    Else
                        ; GUI created as a "buttonless" message box 
                        $wait = GUICreate("Please Wait", 700, 50)
                        GUISetFont (9, 400, 0, $font)
                        ; Informational label created
                        GUICtrlCreateLabel("Please move your mouse to a location and wait " & GUICtrlRead($inputud) & " seconds for the mouse coordinates to return.", 20, 10)  
                        GUISetState(@SW_SHOW)   ; Shows GUI to user
                        Dim $sleepytime ; Sets a variable
                        $sleepytime = GUICtrlRead($inputud) *1000   ; Calaculates variable based on second input box
                        Sleep($sleepytime)  ; Pauses GUI for calculated variable
                        GUISetState(@SW_HIDE)   ; Hides the GUI
                        $logfile = FileOpen($windir & "\MousePositionLog.txt", 1)   ; Opens the log file
                        ; Check if file opened for writing OK with error handling
                            If $logfile = -1 Then
                                MsgBox(0, "Error", "Unable to open file.")
                                Exit
                            EndIf   
                        $pos = MouseGetPos()    ; Gets the mouse coordinates
                        ; Writes to the log file
                        FileWrite($logfile, _Now() & ": Position labeled '" & GUICtrlRead($input) & "' mapped to X = " & $pos[0] & " and Y = " & $pos[1] & @CRLF)
                        FileClose($logfile) ; Closes the log file
                        ; GUI created as a "buttonless" message box 
                        $donemsg = GUICreate("Please Wait", 400, 50)
                        GUISetFont (9, 400, 0, $font)
                        GUICtrlCreateLabel("Mapping complete.  You may now map another.", 20, 10)   ; Creates an informational label
                        GUISetState(@SW_SHOW); Shows the GUI
                        Sleep(1500) ; Pauses the GUI for 1.5 seconds
                        GUISetState(@SW_HIDE)   ; Hides the GUI
                        ContButton() ; Calls the ContButton Function
                    EndIf
            ; ; Check if user clicked on the "Finished" button
            Case $msg = $donebutton
                Done()  ; Calls the Done function
        EndSelect   ; End of the SELECT
    WEnd    ; End of the WHILE
EndFunc ; End of the OK Button FUNCTION

; "ContButton" Function
Func ContButton()
    ; This function calls the OKButton function.
    ; This is designed to loop through the program based on user input.  It allows the user
    ; to continue to map additional mouse coordinates if they have an especially long list of
    ; mouse positions to map.
    OKButton()
EndFunc ; End of ContButton FUNCTION

; "CLOSEClick" Function
Func CLOSEClicked()
    ; GUI created as a "buttonless" message box 
    ; GUI CREATION
    $handle = GUICreate("Please Wait", 250, 50)
    GUISetFont (9, 400, 0, $font)
    GUICtrlCreateLabel("Exiting ...", 20, 10)   ; Creates informational label
    GUISetState(@SW_SHOW)   ; Shows the GUI to the user
    Sleep(3000) ; Paused the GUI for 3 seconds
    GUISetState(@SW_HIDE)   ; Hides the GUI
    Exit ; Exits the program
EndFunc ; End of CLOSEClick FUNCTION

; "Done" Function
Func Done()
    ; MsgBox prompts the user to open the mosition position log for viewing
    $openlog = MsgBox(4, "Open Log?", "Do you want to open the mouse poistion log file?")
    If $openlog = 6 Then  ; If the yes clicks "Yes"
        ; The next few Send keys open the mouse position log
        Send("#r")
        WinWaitActive("Run")
        Send($windir & "\MousePositionLog.txt{Enter}")
    EndIf
    
    ; GUI created as a "buttonless" message box 
    ; GUI CREATION
    $handle = GUICreate("Please Wait", 250, 50)
    GUISetFont (9, 400, 0, $font)
    GUICtrlCreateLabel("Exiting ...", 20, 10)   ; Creates informational label
    GUISetState(@SW_SHOW)   ; Shows the GUI to the user
    Sleep(3000) ; Paused the GUI for 3 seconds
    GUISetState(@SW_HIDE)   ; Hides the GUI
    Exit ; Exits the program
EndFunc ; End of Done Function

; END OF SCRIPT
Edited by ScottL
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...