Reply To: Multi Character Key Press

PennController for IBEX Forums Support Multi Character Key Press Reply To: Multi Character Key Press

#5179
Jeremy
Keymaster

Hi Sam,

EDIT: actually you don’t have to use the Function element as I suggest below, but you’ll still need a little bit of javascript when setting and testing your Var element:

Template( "fulldesign.csv" , variable =>
  newTrial( "task" ,
    newVar("fullstring", "")
    ,
    newText("Please type your guess").print()
    ,
    newKey("guess", "")
        .callback( 
            getVar("fullstring").set( v=>v+getKey("guess").value )
        )
    ,
    newKey("Enter").wait()
    ,
    getVar("fullstring").test.is( v=>v.match(new RegExp(variable.answer+"$","i")) )
        .success( newText("Correct!").print() )
        .failure( newText("Incorrect").print() )
    ,
    newButton("next").print().wait()
  )
)

End of edit

I think you’ll have to inject some javascript in there, using the Function element:

Template( "fulldesign.csv" , variable =>
  newTrial( "task" ,
    newText("Please type your guess").print()
    ,
    newFunction(function(){
        this.fullstring = "";
        let oldOnkeydown = document.onkeydown;
        document.onkeydown = e=>{
            if (oldOnkeydown instanceof Function) oldOnkeydown.call(document, e);
            this.fullstring += e.key;
        };
    }).call()
    ,
    newKey("Enter").wait()
    ,
    newFunction(function(){
        return this.fullstring.match(new RegExp(variable.answer+"$","i"))!=null; 
    }).test.is( true )
        .success( newText("Correct!").print() )
        .failure( newText("Incorrect").print() )
    ,
    newButton("next").print().wait()
  )
)

Two comments on your code:

  • The setVar command (as all PennController commands) is executed immediately when the script encounters it, so unless it is in a callback command, it will only be executed once. In your case, you call it immediately after creating the Key element, so your Var element will remain empty.
  • The after command adds an element to the right of another element when printed on the page, but it does not change the value of either element

Jeremy

  • This reply was modified 4 years, 7 months ago by Jeremy. Reason: Added a Function-less script