onKeyPress( )
Adds an event handler that runs when a keyboard key is pressed while the container or one of its child elements is focused.
Description
A container receives a keyPress
event when a site visitor presses a keyboard key on
the container or any of its child elements. A keyboard event is triggered for any child elements inside the container that support onKeyPress()
.
A keyPress
event is fired for printable and non-printable characters.
You can also define an event handler using the Properties and Events panel.
Notes:
Some browsers don't issue a key press event for certain keys, such as arrow keys or the shift key.
When you retrieve the
value
from thetarget
property of aKeyboardEvent
, you get the value of the target element before the key was pressed.
Syntax
function onKeyPress(eventHandler: KeyboardEventHandler): ElementeventHandler: function KeyboardEventHandler(event: KeyboardEvent): void
onKeyPress Parameters
NAME
TYPE
DESCRIPTION
The name of the function or the function expression to run when a key is pressed.
KeyboardEventHandler Parameters
NAME
TYPE
DESCRIPTION
Returns
This function does not return anything.
Return Type:
Was this helpful?
1$w('#myContainer').onKeyPress( (event) => {2 let keyboardKey = event.key; // "a"3 } );
1$w('#myContainer').onKeyPress( (event) => {2 let key = event.key; // "A"3 let shift = event.shiftKey; // true4 let meta = event.metaKey; // false5 let alt = event.altKey; // false6 let ctrl = event.ctrlKey; // false7 } );