Search code examples
autohotkeyjoystick

Time-stamped file recording joystick button presses


This is for an experiment we are running in our lab. First the script asks for a participant number and creates a file in C: . During the experiment a participant with a gamepad presses a single button (8), and each time the button is pressed the file is written with the current time. The q button returns a quit message and ends the script.

And it does not work. I think the issue surrounds the joystick. Any help much appreciated.

FormatTime, timestamp, , yyyy_MM_dd_HH_mm
JoystickNumber = 1      
#SingleInstance
#persistent

InputBox, ssnum, Subject Number, Please enter the participant number
if ErrorLevel
    ExitApp
else
thefilename=%ssnum%_T_%timestamp%
    FileAppend,
        (
            %ssnum%%timestamp%
        ), C:\%thefilename%.txt
    return

Loop
{
GetKeyState, state, Joy8
    if state = D
        FileAppend,
        (
            %timestamp%, 
        ), C:\%thefilename%.txt
}

q::
MsgBox Exiting.  Your data is saved in the C drive as %thefilename%.txt
ExitApp

Solution

  • The main issue is that you are hitting a return before it even reaches your loop.

    I've cleaned up your code a bit as well as turned your loop into a hotkey. This will prevent multiple writes to the file if the key is held. If you need to count holding of the button, you can use the loop you had.

    FormatTime, timestamp, , yyyy_MM_dd_HH_mm
    JoystickNumber = 1      
    #SingleInstance
    #Persistent
    
    InputBox, ssnum, Subject Number, Please enter the participant number
    if ErrorLevel
        ExitApp
    
    thefilename = C:\%ssnum%_T_%timestamp%.txt
    FileAppend, %ssnum%`t%timestamp%`n, %thefilename%
    
    ~Joy8::
        FormatTime, timestamp, , HH:mm:ss
        FileAppend, %timestamp%`n, %thefilename%
    Return
    
    q::
        MsgBox Exiting.  Your data is saved in the C drive as %thefilename%
        Run % thefilename
        ExitApp