Keyborad input release time


Author
Message
Tingyu
Tingyu
New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)
Group: Forum Members
Posts: 4, Visits: 14
I am doing a research based on keystroke dynamics which needs to collect the keyboard input, press time and release time of every key input.

I wonder is there any method to collect the release time of a key input?

From previous discussion in the forum, I suppose I need to do by <trial> and now I can collect keyboard input and latency in a <trial>. But release time of each key input is still needed.

Dave
Dave
Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)
Group: Administrators
Posts: 12K, Visits: 89K
Tingyu - 11/22/2022
I am doing a research based on keystroke dynamics which needs to collect the keyboard input, press time and release time of every key input.

I wonder is there any method to collect the release time of a key input?

From previous discussion in the forum, I suppose I need to do by <trial> and now I can collect keyboard input and latency in a <trial>. But release time of each key input is still needed.

<values>
/ space_presslatency = -1
/ space_releaselatency = -1
</values>

<block myblock>
/ trials = [1-4 = example]
</block>

<trial example>
/ pretrialpause = 500
/ stimulusframes = [1=mytext]
/ validresponse = (57, -57)
/ isvalidresponse = [
    if (trial.example.response == 57) {
        // capture latency of spacebar press
        values.space_presslatency = trial.example.latency;
        // don't end the trial
        return false;
    } else if (trial.example.response == -57) {
        // capture latency of spacebar release
        values.space_releaselatency = trial.example.latency;
        // end the trial
        return true;
    };
]
</trial>

<text mytext>
/ items = ("Press and then release the spacebar.")
</text>

<data>
/ columns = (date time subject group session blocknum blockcode trialnum trialcode response latency correct
    values.space_presslatency values.space_releaselatency)
</data>



is how you can capture press and key release times. A key release is indicated by a negative keyboard scancode ( https://www.millisecond.com/support/docs/current/html/language/scancodes.htm ).

To add: Generalizing the above to capture an arbitrary key's press and corresponding release would go along the following lines:

<values>
/ key = 0 // scancode of the pressed key
/ char = "" // corresponding character
/ key_presslatency = -1
/ key_releaselatency = -1
/ key_pressduration = -1
</values>

<block myblock>
/ trials = [1-4 = example]
</block>

<trial example>
/ ontrialbegin = [
    values.key = 0; // reset key variable
]
/ pretrialpause = 500
/ stimulusframes = [1=mytext]
/ isvalidresponse = [
    // check if we have a key press
    if (trial.example.response > 0 && values.key == 0) {
        // capture which key was pressed (scancode)
        values.key = trial.example.response;
        // capture which key was pressed (character)
        values.char = trial.example.responsetext;
        // capture latency of key press
        values.key_presslatency = trial.example.latency;
        // don't end the trial
        return false;
    }
    // check for key's release
    else if (trial.example.response == -1*values.key) {
        // capture latency of key release
        values.key_releaselatency = trial.example.latency;
        // calculate key press duration (release time - press time)
        values.key_pressduration = values.key_releaselatency - values.key_presslatency;
        // end the trial
        return true;
    };
]
</trial>

<text mytext>
/ items = ("Press any key and then release it.")
</text>

<data>
/ columns = (date time subject group session blocknum blockcode trialnum trialcode response latency correct
    values.char values.key values.key_presslatency values.key_releaselatency values.key_pressduration)
</data>


Edited 3 Months Ago by Dave
Tingyu
Tingyu
New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)
Group: Forum Members
Posts: 4, Visits: 14
Dave - 11/22/2022
Tingyu - 11/22/2022
I am doing a research based on keystroke dynamics which needs to collect the keyboard input, press time and release time of every key input.

I wonder is there any method to collect the release time of a key input?

From previous discussion in the forum, I suppose I need to do by <trial> and now I can collect keyboard input and latency in a <trial>. But release time of each key input is still needed.

<values>
/ space_presslatency = -1
/ space_releaselatency = -1
</values>

<block myblock>
/ trials = [1-4 = example]
</block>

<trial example>
/ pretrialpause = 500
/ stimulusframes = [1=mytext]
/ validresponse = (57, -57)
/ isvalidresponse = [
    if (trial.example.response == 57) {
        // capture latency of spacebar press
        values.space_presslatency = trial.example.latency;
        // don't end the trial
        return false;
    } else if (trial.example.response == -57) {
        // capture latency of spacebar release
        values.space_releaselatency = trial.example.latency;
        // end the trial
        return true;
    };
]
</trial>

<text mytext>
/ items = ("Press and then release the spacebar.")
</text>

<data>
/ columns = (date time subject group session blocknum blockcode trialnum trialcode response latency correct
    values.space_presslatency values.space_releaselatency)
</data>



is how you can capture press and key release times. A key release is indicated by a negative keyboard scancode ( https://www.millisecond.com/support/docs/current/html/language/scancodes.htm ).

To add: Generalizing the above to capture an arbitrary key's press and corresponding release would go along the following lines:

<values>
/ key = 0 // scancode of the pressed key
/ char = "" // corresponding character
/ key_presslatency = -1
/ key_releaselatency = -1
/ key_pressduration = -1
</values>

<block myblock>
/ trials = [1-4 = example]
</block>

<trial example>
/ ontrialbegin = [
    values.key = 0; // reset key variable
]
/ pretrialpause = 500
/ stimulusframes = [1=mytext]
/ isvalidresponse = [
    // check if we have a key press
    if (trial.example.response > 0 && values.key == 0) {
        // capture which key was pressed (scancode)
        values.key = trial.example.response;
        // capture which key was pressed (character)
        values.char = trial.example.responsetext;
        // capture latency of key press
        values.key_presslatency = trial.example.latency;
        // don't end the trial
        return false;
    }
    // check for key's release
    else if (trial.example.response == -1*values.key) {
        // capture latency of key release
        values.key_releaselatency = trial.example.latency;
        // calculate key press duration (release time - press time)
        values.key_pressduration = values.key_releaselatency - values.key_presslatency;
        // end the trial
        return true;
    };
]
</trial>

<text mytext>
/ items = ("Press any key and then release it.")
</text>

<data>
/ columns = (date time subject group session blocknum blockcode trialnum trialcode response latency correct
    values.char values.key values.key_presslatency values.key_releaselatency values.key_pressduration)
</data>


Thank you David, it really helps!

By the way, if the input is expected to be in the flexible length, should I try to use /branch to control instead of setting the block(/trials = [1-4 = example])? I am working on it but have not made it work.

Dave
Dave
Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)
Group: Administrators
Posts: 12K, Visits: 89K
Tingyu - 12/4/2022
Dave - 11/22/2022
Tingyu - 11/22/2022
I am doing a research based on keystroke dynamics which needs to collect the keyboard input, press time and release time of every key input.

I wonder is there any method to collect the release time of a key input?

From previous discussion in the forum, I suppose I need to do by <trial> and now I can collect keyboard input and latency in a <trial>. But release time of each key input is still needed.

<values>
/ space_presslatency = -1
/ space_releaselatency = -1
</values>

<block myblock>
/ trials = [1-4 = example]
</block>

<trial example>
/ pretrialpause = 500
/ stimulusframes = [1=mytext]
/ validresponse = (57, -57)
/ isvalidresponse = [
    if (trial.example.response == 57) {
        // capture latency of spacebar press
        values.space_presslatency = trial.example.latency;
        // don't end the trial
        return false;
    } else if (trial.example.response == -57) {
        // capture latency of spacebar release
        values.space_releaselatency = trial.example.latency;
        // end the trial
        return true;
    };
]
</trial>

<text mytext>
/ items = ("Press and then release the spacebar.")
</text>

<data>
/ columns = (date time subject group session blocknum blockcode trialnum trialcode response latency correct
    values.space_presslatency values.space_releaselatency)
</data>



is how you can capture press and key release times. A key release is indicated by a negative keyboard scancode ( https://www.millisecond.com/support/docs/current/html/language/scancodes.htm ).

To add: Generalizing the above to capture an arbitrary key's press and corresponding release would go along the following lines:

<values>
/ key = 0 // scancode of the pressed key
/ char = "" // corresponding character
/ key_presslatency = -1
/ key_releaselatency = -1
/ key_pressduration = -1
</values>

<block myblock>
/ trials = [1-4 = example]
</block>

<trial example>
/ ontrialbegin = [
    values.key = 0; // reset key variable
]
/ pretrialpause = 500
/ stimulusframes = [1=mytext]
/ isvalidresponse = [
    // check if we have a key press
    if (trial.example.response > 0 && values.key == 0) {
        // capture which key was pressed (scancode)
        values.key = trial.example.response;
        // capture which key was pressed (character)
        values.char = trial.example.responsetext;
        // capture latency of key press
        values.key_presslatency = trial.example.latency;
        // don't end the trial
        return false;
    }
    // check for key's release
    else if (trial.example.response == -1*values.key) {
        // capture latency of key release
        values.key_releaselatency = trial.example.latency;
        // calculate key press duration (release time - press time)
        values.key_pressduration = values.key_releaselatency - values.key_presslatency;
        // end the trial
        return true;
    };
]
</trial>

<text mytext>
/ items = ("Press any key and then release it.")
</text>

<data>
/ columns = (date time subject group session blocknum blockcode trialnum trialcode response latency correct
    values.char values.key values.key_presslatency values.key_releaselatency values.key_pressduration)
</data>


Thank you David, it really helps!

By the way, if the input is expected to be in the flexible length, should I try to use /branch to control instead of setting the block(/trials = [1-4 = example])? I am working on it but have not made it work.

Yeah, running as many trials as needed per /branch would be the way to go.
Tingyu
Tingyu
New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)
Group: Forum Members
Posts: 4, Visits: 14

Dave - 12/5/2022
Tingyu - 12/4/2022
Dave - 11/22/2022
Tingyu - 11/22/2022
I am doing a research based on keystroke dynamics which needs to collect the keyboard input, press time and release time of every key input.

I wonder is there any method to collect the release time of a key input?

From previous discussion in the forum, I suppose I need to do by <trial> and now I can collect keyboard input and latency in a <trial>. But release time of each key input is still needed.

<values>
/ space_presslatency = -1
/ space_releaselatency = -1
</values>

<block myblock>
/ trials = [1-4 = example]
</block>

<trial example>
/ pretrialpause = 500
/ stimulusframes = [1=mytext]
/ validresponse = (57, -57)
/ isvalidresponse = [
    if (trial.example.response == 57) {
        // capture latency of spacebar press
        values.space_presslatency = trial.example.latency;
        // don't end the trial
        return false;
    } else if (trial.example.response == -57) {
        // capture latency of spacebar release
        values.space_releaselatency = trial.example.latency;
        // end the trial
        return true;
    };
]
</trial>

<text mytext>
/ items = ("Press and then release the spacebar.")
</text>

<data>
/ columns = (date time subject group session blocknum blockcode trialnum trialcode response latency correct
    values.space_presslatency values.space_releaselatency)
</data>



is how you can capture press and key release times. A key release is indicated by a negative keyboard scancode ( https://www.millisecond.com/support/docs/current/html/language/scancodes.htm ).

To add: Generalizing the above to capture an arbitrary key's press and corresponding release would go along the following lines:

<values>
/ key = 0 // scancode of the pressed key
/ char = "" // corresponding character
/ key_presslatency = -1
/ key_releaselatency = -1
/ key_pressduration = -1
</values>

<block myblock>
/ trials = [1-4 = example]
</block>

<trial example>
/ ontrialbegin = [
    values.key = 0; // reset key variable
]
/ pretrialpause = 500
/ stimulusframes = [1=mytext]
/ isvalidresponse = [
    // check if we have a key press
    if (trial.example.response > 0 && values.key == 0) {
        // capture which key was pressed (scancode)
        values.key = trial.example.response;
        // capture which key was pressed (character)
        values.char = trial.example.responsetext;
        // capture latency of key press
        values.key_presslatency = trial.example.latency;
        // don't end the trial
        return false;
    }
    // check for key's release
    else if (trial.example.response == -1*values.key) {
        // capture latency of key release
        values.key_releaselatency = trial.example.latency;
        // calculate key press duration (release time - press time)
        values.key_pressduration = values.key_releaselatency - values.key_presslatency;
        // end the trial
        return true;
    };
]
</trial>

<text mytext>
/ items = ("Press any key and then release it.")
</text>

<data>
/ columns = (date time subject group session blocknum blockcode trialnum trialcode response latency correct
    values.char values.key values.key_presslatency values.key_releaselatency values.key_pressduration)
</data>


Thank you David, it really helps!

By the way, if the input is expected to be in the flexible length, should I try to use /branch to control instead of setting the block(/trials = [1-4 = example])? I am working on it but have not made it work.

Yeah, running as many trials as needed per /branch would be the way to go.
Thank you David!

I met another problem. When people key in a sequence, sometimes the press of the second key is before the release of the first key. Considering this situation, I think collecting press and release of keys seperately (in 2 trials instead of 1 as what you show above) might be better. And I am trying to do that.

Besides, Could I consider the time interval between two adjacent trials as 0? Then I can generate a complete keyboard input in time series.

Dave
Dave
Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)
Group: Administrators
Posts: 12K, Visits: 89K
Tingyu - 1/13/2023

Dave - 12/5/2022
Tingyu - 12/4/2022
Dave - 11/22/2022
Tingyu - 11/22/2022
I am doing a research based on keystroke dynamics which needs to collect the keyboard input, press time and release time of every key input.

I wonder is there any method to collect the release time of a key input?

From previous discussion in the forum, I suppose I need to do by <trial> and now I can collect keyboard input and latency in a <trial>. But release time of each key input is still needed.

<values>
/ space_presslatency = -1
/ space_releaselatency = -1
</values>

<block myblock>
/ trials = [1-4 = example]
</block>

<trial example>
/ pretrialpause = 500
/ stimulusframes = [1=mytext]
/ validresponse = (57, -57)
/ isvalidresponse = [
    if (trial.example.response == 57) {
        // capture latency of spacebar press
        values.space_presslatency = trial.example.latency;
        // don't end the trial
        return false;
    } else if (trial.example.response == -57) {
        // capture latency of spacebar release
        values.space_releaselatency = trial.example.latency;
        // end the trial
        return true;
    };
]
</trial>

<text mytext>
/ items = ("Press and then release the spacebar.")
</text>

<data>
/ columns = (date time subject group session blocknum blockcode trialnum trialcode response latency correct
    values.space_presslatency values.space_releaselatency)
</data>



is how you can capture press and key release times. A key release is indicated by a negative keyboard scancode ( https://www.millisecond.com/support/docs/current/html/language/scancodes.htm ).

To add: Generalizing the above to capture an arbitrary key's press and corresponding release would go along the following lines:

<values>
/ key = 0 // scancode of the pressed key
/ char = "" // corresponding character
/ key_presslatency = -1
/ key_releaselatency = -1
/ key_pressduration = -1
</values>

<block myblock>
/ trials = [1-4 = example]
</block>

<trial example>
/ ontrialbegin = [
    values.key = 0; // reset key variable
]
/ pretrialpause = 500
/ stimulusframes = [1=mytext]
/ isvalidresponse = [
    // check if we have a key press
    if (trial.example.response > 0 && values.key == 0) {
        // capture which key was pressed (scancode)
        values.key = trial.example.response;
        // capture which key was pressed (character)
        values.char = trial.example.responsetext;
        // capture latency of key press
        values.key_presslatency = trial.example.latency;
        // don't end the trial
        return false;
    }
    // check for key's release
    else if (trial.example.response == -1*values.key) {
        // capture latency of key release
        values.key_releaselatency = trial.example.latency;
        // calculate key press duration (release time - press time)
        values.key_pressduration = values.key_releaselatency - values.key_presslatency;
        // end the trial
        return true;
    };
]
</trial>

<text mytext>
/ items = ("Press any key and then release it.")
</text>

<data>
/ columns = (date time subject group session blocknum blockcode trialnum trialcode response latency correct
    values.char values.key values.key_presslatency values.key_releaselatency values.key_pressduration)
</data>


Thank you David, it really helps!

By the way, if the input is expected to be in the flexible length, should I try to use /branch to control instead of setting the block(/trials = [1-4 = example])? I am working on it but have not made it work.

Yeah, running as many trials as needed per /branch would be the way to go.
Thank you David!

I met another problem. When people key in a sequence, sometimes the press of the second key is before the release of the first key. Considering this situation, I think collecting press and release of keys seperately (in 2 trials instead of 1 as what you show above) might be better. And I am trying to do that.

Besides, Could I consider the time interval between two adjacent trials as 0? Then I can generate a complete keyboard input in time series.

Not sure what you mean here. Be more specific about what, exactly, you want to do please

Tingyu
Tingyu
New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)New Member (16 reputation)
Group: Forum Members
Posts: 4, Visits: 14
Dave - 1/13/2023
Tingyu - 1/13/2023

Dave - 12/5/2022
Tingyu - 12/4/2022
Dave - 11/22/2022
Tingyu - 11/22/2022
I am doing a research based on keystroke dynamics which needs to collect the keyboard input, press time and release time of every key input.

I wonder is there any method to collect the release time of a key input?

From previous discussion in the forum, I suppose I need to do by <trial> and now I can collect keyboard input and latency in a <trial>. But release time of each key input is still needed.

<values>
/ space_presslatency = -1
/ space_releaselatency = -1
</values>

<block myblock>
/ trials = [1-4 = example]
</block>

<trial example>
/ pretrialpause = 500
/ stimulusframes = [1=mytext]
/ validresponse = (57, -57)
/ isvalidresponse = [
    if (trial.example.response == 57) {
        // capture latency of spacebar press
        values.space_presslatency = trial.example.latency;
        // don't end the trial
        return false;
    } else if (trial.example.response == -57) {
        // capture latency of spacebar release
        values.space_releaselatency = trial.example.latency;
        // end the trial
        return true;
    };
]
</trial>

<text mytext>
/ items = ("Press and then release the spacebar.")
</text>

<data>
/ columns = (date time subject group session blocknum blockcode trialnum trialcode response latency correct
    values.space_presslatency values.space_releaselatency)
</data>



is how you can capture press and key release times. A key release is indicated by a negative keyboard scancode ( https://www.millisecond.com/support/docs/current/html/language/scancodes.htm ).

To add: Generalizing the above to capture an arbitrary key's press and corresponding release would go along the following lines:

<values>
/ key = 0 // scancode of the pressed key
/ char = "" // corresponding character
/ key_presslatency = -1
/ key_releaselatency = -1
/ key_pressduration = -1
</values>

<block myblock>
/ trials = [1-4 = example]
</block>

<trial example>
/ ontrialbegin = [
    values.key = 0; // reset key variable
]
/ pretrialpause = 500
/ stimulusframes = [1=mytext]
/ isvalidresponse = [
    // check if we have a key press
    if (trial.example.response > 0 && values.key == 0) {
        // capture which key was pressed (scancode)
        values.key = trial.example.response;
        // capture which key was pressed (character)
        values.char = trial.example.responsetext;
        // capture latency of key press
        values.key_presslatency = trial.example.latency;
        // don't end the trial
        return false;
    }
    // check for key's release
    else if (trial.example.response == -1*values.key) {
        // capture latency of key release
        values.key_releaselatency = trial.example.latency;
        // calculate key press duration (release time - press time)
        values.key_pressduration = values.key_releaselatency - values.key_presslatency;
        // end the trial
        return true;
    };
]
</trial>

<text mytext>
/ items = ("Press any key and then release it.")
</text>

<data>
/ columns = (date time subject group session blocknum blockcode trialnum trialcode response latency correct
    values.char values.key values.key_presslatency values.key_releaselatency values.key_pressduration)
</data>


Thank you David, it really helps!

By the way, if the input is expected to be in the flexible length, should I try to use /branch to control instead of setting the block(/trials = [1-4 = example])? I am working on it but have not made it work.

Yeah, running as many trials as needed per /branch would be the way to go.
Thank you David!

I met another problem. When people key in a sequence, sometimes the press of the second key is before the release of the first key. Considering this situation, I think collecting press and release of keys seperately (in 2 trials instead of 1 as what you show above) might be better. And I am trying to do that.

Besides, Could I consider the time interval between two adjacent trials as 0? Then I can generate a complete keyboard input in time series.

Not sure what you mean here. Be more specific about what, exactly, you want to do please
There are actually two questions.

Firstly, I also want to collect how long it takes between press of two keyboard input which are recorded by two trials in your example. For example, when typing in 'A' and 'B' seperately, now we have 'key_presslatency', 'key_releaselatency', 'key_pressduration' of A, B. Could I compute the time between two press just by ('key_presslatenccy' of B + 'key_pressduration' of A)? That's why I ask if there is no time interval or fixed time interval between trials because I think time between two press should also consider the time interval between two trials.

Next question is from my testing. I found when typing 'AB', if I press 'B' before release of  'A' then this 'B' can not be recorded. So I adjuasted the script to collect press and release of a keyboard input seperately. Now what I have is a series of latency. If I want to calculate the press duration, it comes back to the first question -- I need to take time interval between two trials into consideration.
Dave
Dave
Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)
Group: Administrators
Posts: 12K, Visits: 89K
Tingyu - 1/15/2023
Dave - 1/13/2023
Tingyu - 1/13/2023

Dave - 12/5/2022
Tingyu - 12/4/2022
Dave - 11/22/2022
Tingyu - 11/22/2022
I am doing a research based on keystroke dynamics which needs to collect the keyboard input, press time and release time of every key input.

I wonder is there any method to collect the release time of a key input?

From previous discussion in the forum, I suppose I need to do by <trial> and now I can collect keyboard input and latency in a <trial>. But release time of each key input is still needed.

<values>
/ space_presslatency = -1
/ space_releaselatency = -1
</values>

<block myblock>
/ trials = [1-4 = example]
</block>

<trial example>
/ pretrialpause = 500
/ stimulusframes = [1=mytext]
/ validresponse = (57, -57)
/ isvalidresponse = [
    if (trial.example.response == 57) {
        // capture latency of spacebar press
        values.space_presslatency = trial.example.latency;
        // don't end the trial
        return false;
    } else if (trial.example.response == -57) {
        // capture latency of spacebar release
        values.space_releaselatency = trial.example.latency;
        // end the trial
        return true;
    };
]
</trial>

<text mytext>
/ items = ("Press and then release the spacebar.")
</text>

<data>
/ columns = (date time subject group session blocknum blockcode trialnum trialcode response latency correct
    values.space_presslatency values.space_releaselatency)
</data>



is how you can capture press and key release times. A key release is indicated by a negative keyboard scancode ( https://www.millisecond.com/support/docs/current/html/language/scancodes.htm ).

To add: Generalizing the above to capture an arbitrary key's press and corresponding release would go along the following lines:

<values>
/ key = 0 // scancode of the pressed key
/ char = "" // corresponding character
/ key_presslatency = -1
/ key_releaselatency = -1
/ key_pressduration = -1
</values>

<block myblock>
/ trials = [1-4 = example]
</block>

<trial example>
/ ontrialbegin = [
    values.key = 0; // reset key variable
]
/ pretrialpause = 500
/ stimulusframes = [1=mytext]
/ isvalidresponse = [
    // check if we have a key press
    if (trial.example.response > 0 && values.key == 0) {
        // capture which key was pressed (scancode)
        values.key = trial.example.response;
        // capture which key was pressed (character)
        values.char = trial.example.responsetext;
        // capture latency of key press
        values.key_presslatency = trial.example.latency;
        // don't end the trial
        return false;
    }
    // check for key's release
    else if (trial.example.response == -1*values.key) {
        // capture latency of key release
        values.key_releaselatency = trial.example.latency;
        // calculate key press duration (release time - press time)
        values.key_pressduration = values.key_releaselatency - values.key_presslatency;
        // end the trial
        return true;
    };
]
</trial>

<text mytext>
/ items = ("Press any key and then release it.")
</text>

<data>
/ columns = (date time subject group session blocknum blockcode trialnum trialcode response latency correct
    values.char values.key values.key_presslatency values.key_releaselatency values.key_pressduration)
</data>


Thank you David, it really helps!

By the way, if the input is expected to be in the flexible length, should I try to use /branch to control instead of setting the block(/trials = [1-4 = example])? I am working on it but have not made it work.

Yeah, running as many trials as needed per /branch would be the way to go.
Thank you David!

I met another problem. When people key in a sequence, sometimes the press of the second key is before the release of the first key. Considering this situation, I think collecting press and release of keys seperately (in 2 trials instead of 1 as what you show above) might be better. And I am trying to do that.

Besides, Could I consider the time interval between two adjacent trials as 0? Then I can generate a complete keyboard input in time series.

Not sure what you mean here. Be more specific about what, exactly, you want to do please
There are actually two questions.

Firstly, I also want to collect how long it takes between press of two keyboard input which are recorded by two trials in your example. For example, when typing in 'A' and 'B' seperately, now we have 'key_presslatency', 'key_releaselatency', 'key_pressduration' of A, B. Could I compute the time between two press just by ('key_presslatenccy' of B + 'key_pressduration' of A)? That's why I ask if there is no time interval or fixed time interval between trials because I think time between two press should also consider the time interval between two trials.

Next question is from my testing. I found when typing 'AB', if I press 'B' before release of  'A' then this 'B' can not be recorded. So I adjuasted the script to collect press and release of a keyboard input seperately. Now what I have is a series of latency. If I want to calculate the press duration, it comes back to the first question -- I need to take time interval between two trials into consideration.

Don't use latency then, but script.elapsedtime. I.e., take script.elapsedtime when key "A" is pressed. Take script.elapsedtime when "B" is pressed. Same for releases. Time between keypresses A and B is keypress time B minus keypress time A. Key press duration of A is key release time of A minus key press time of A, and so forth. The script.elapsedtime is measure from the start of script execution, so it includes any "time interval between trials."

https://www.millisecond.com/support/docs/current/html/language/properties/elapsedtime.htm
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Reading This Topic

Explore
Messages
Mentions
Search