UNPKG

@wordpress/keycodes

Version:

Keycodes utilities for WordPress. Used to check for keyboard events across browsers/operating systems.

182 lines 4.61 kB
/** * Note: The order of the modifier keys in many of the [foo]Shortcut() * functions in this file are intentional and should not be changed. They're * designed to fit with the standard menu keyboard shortcuts shown in the * user's platform. * * For example, on MacOS menu shortcuts will place Shift before Command, but * on Windows Control will usually come first. So don't provide your own * shortcut combos directly to keyboardShortcut(). */ /** * Internal dependencies */ import { isAppleOS } from './platform'; /** * External dependencies */ import type { KeyboardEvent as ReactKeyboardEvent } from 'react'; export type WPModifierPart = typeof ALT | typeof CTRL | typeof COMMAND | typeof SHIFT; export type WPKeycodeModifier = 'primary' | 'primaryShift' | 'primaryAlt' | 'secondary' | 'access' | 'ctrl' | 'alt' | 'ctrlShift' | 'shift' | 'shiftAlt' | 'undefined'; /** * An object of handler functions for each of the possible modifier * combinations. A handler will return a value for a given key. */ export type WPModifierHandler<T> = Record<WPKeycodeModifier, T>; export type WPKeyHandler<T> = (character: string, isApple?: () => boolean) => T; export type WPEventKeyHandler = (event: ReactKeyboardEvent<HTMLElement> | KeyboardEvent, character: string, isApple?: () => boolean) => boolean; export type WPModifier = (isApple: () => boolean) => WPModifierPart[]; /** * Keycode for BACKSPACE key. */ export declare const BACKSPACE = 8; /** * Keycode for TAB key. */ export declare const TAB = 9; /** * Keycode for ENTER key. */ export declare const ENTER = 13; /** * Keycode for ESCAPE key. */ export declare const ESCAPE = 27; /** * Keycode for SPACE key. */ export declare const SPACE = 32; /** * Keycode for PAGEUP key. */ export declare const PAGEUP = 33; /** * Keycode for PAGEDOWN key. */ export declare const PAGEDOWN = 34; /** * Keycode for END key. */ export declare const END = 35; /** * Keycode for HOME key. */ export declare const HOME = 36; /** * Keycode for LEFT key. */ export declare const LEFT = 37; /** * Keycode for UP key. */ export declare const UP = 38; /** * Keycode for RIGHT key. */ export declare const RIGHT = 39; /** * Keycode for DOWN key. */ export declare const DOWN = 40; /** * Keycode for DELETE key. */ export declare const DELETE = 46; /** * Keycode for F10 key. */ export declare const F10 = 121; /** * Keycode for ALT key. */ export declare const ALT = "alt"; /** * Keycode for CTRL key. */ export declare const CTRL = "ctrl"; /** * Keycode for COMMAND/META key. */ export declare const COMMAND = "meta"; /** * Keycode for SHIFT key. */ export declare const SHIFT = "shift"; /** * Keycode for ZERO key. */ export declare const ZERO = 48; export { isAppleOS }; /** * Object that contains functions that return the available modifier * depending on platform. */ export declare const modifiers: WPModifierHandler<WPModifier>; /** * An object that contains functions to get raw shortcuts. * * These are intended for user with the KeyboardShortcuts. * * @example * ```js * // Assuming macOS: * rawShortcut.primary( 'm' ) * // "meta+m"" * ``` */ export declare const rawShortcut: WPModifierHandler<WPKeyHandler<string>>; /** * Return an array of the parts of a keyboard shortcut chord for display. * * @example * ```js * // Assuming macOS: * displayShortcutList.primary( 'm' ); * // [ "⌘", "M" ] * ``` * * Keyed map of functions to shortcut sequences. */ export declare const displayShortcutList: WPModifierHandler<WPKeyHandler<string[]>>; /** * An object that contains functions to display shortcuts. * * @example * ```js * // Assuming macOS: * displayShortcut.primary( 'm' ); * // "⌘M" * ``` * * Keyed map of functions to display shortcuts. */ export declare const displayShortcut: WPModifierHandler<WPKeyHandler<string>>; /** * An object that contains functions to return an aria label for a keyboard * shortcut. * * @example * ```js * // Assuming macOS: * shortcutAriaLabel.primary( '.' ); * // "Command + Period" * ``` * * Keyed map of functions to shortcut ARIA labels. */ export declare const shortcutAriaLabel: WPModifierHandler<WPKeyHandler<string>>; /** * An object that contains functions to check if a keyboard event matches a * predefined shortcut combination. * * @example * ```js * // Assuming an event for ⌘M key press: * isKeyboardEvent.primary( event, 'm' ); * // true * ``` * * Keyed map of functions to match events. */ export declare const isKeyboardEvent: WPModifierHandler<WPEventKeyHandler>; //# sourceMappingURL=index.d.ts.map