Keycodes
Keycodes utilities for WordPress, used to check the key pressed in events like onKeyDown
. Contains keycodes constants for keyboard keys like DOWN
, UP
, ENTER
, etc.
Installation
Install the module
npm install @wordpress/keycodes --save
Usage
Check which key was used in an onKeyDown
event:
import { DOWN, ENTER } from '@wordpress/keycodes';
onKeyDown( event ) {
const { keyCode } = event;
if ( keyCode === DOWN ) {
alert( 'You pressed the down arrow!' );
} else if ( keyCode === ENTER ) {
alert( 'You pressed the enter key!' );
} else {
alert( 'You pressed another key.' );
}
}
