Key element

Action commands
Test commands

description

Creates a keypress detector.

syntax

newKey(ELEMENT_NAME: string, VALIDATING_KEYS: string)
  • ELEMENT_NAME: The name of the newly-created element.
  • VALIDATING_KEYS : A string of keyboard keys that validate the Key element.

note(s)

since

beta 0.3

example(s)

+ Pass the empty string `""` for a press on any keyboard key. + Pass a single space `" "` for a press of the spacebar key. + Since PennController 1.4, you can pass special keys like `"Enter"`, `"Shift"`, and `"Escape"`. + Since PennController 1.4, you can pass multiple arguments to named `Key` elements, for example `"newKey("answer", "FJ", "Escape")`.

@newKey("FJ")
@  .log()
@  .wait()
+ Waits until the key is pressed and logs which key was pressed

Action commands

key.callback

syntax

.callback()

description

Will execute the command(s) whenever a key corresponding to the element is pressed.

example

click to expand

@newText("sentence", "Hello world")
@    .print()
@,
@newVar("word", 0)
@,
@newKey("control", " \n\r")
$    .callback(
$        getText("sentence")
$            .color("red")
$        ,
$        newTimer(1000)
$            .start()
$            .wait()
$        ,
$        getText("sentence")
$            .color("black")
$    )
  • Prints Hello world onto the page and will highlight it in red for 1s whenever the Space or the Enter/Return key is pressed.
↑ back to top

key.log

syntax

.log()

description

A line will be added to the results file indicating which key was pressed when.

example

click to expand

@newText("instructions", "Please press any key")
@    .print()
@,
@newKey("anyKey", "")
$    .log()
@    .wait()
@,
@newText("thanks", "Thank you!")
@    .print()
  • Adds a text asking for a press on any key, after which the text Thank you! get printed below. The results file will contain a line indicating which key was pressed, and at what time(stamp) it was pressed.
↑ back to top

key.wait

syntax

.wait()

description

Waits until a key is pressed.

example

click to expand

@newText("instructions", "Please press the spacebar")
@    .print()
@,
@newKey("space", " ")
$    .wait()
@,
@newText("thanks", "Thank you!")
@    .print()
  • Adds a text asking for a press of the spacebar key. The text Thank you! get printed below it only after the spacebar was pressed.
↑ back to top


Test commands

key.test.pressed

syntax

.test.pressed()

description

Tests whether a key was pressed.

example

click to expand

@newText("instructions", "Press F if you think 0.999... = 1, press J otherwise.")
@    .print()
@,
@newKey("forj", "FJ")
@    .wait()
@,
@getKey("forj")
$    .test.pressed("F")
$    .success( newText("success", "You're right!").print() )
$    .failure( newText("failure", "You're wrong, 0.999... and 1 do refer to the same number").print() )
  • Will print the success message if the key that was pressed was F. Will print the failure message otherwise (i.e., if it was J).
↑ back to top


Table of contents