# 键盘快捷键数据

命名空间：`core/keyboard-shortcuts`。

## 选择器

<!-- START TOKEN(Autogenerated selectors|../../../packages/keyboard-shortcuts/src/store/selectors.js) -->

### getAllShortcutKeyCombinations

Returns the shortcuts that include aliases for a given shortcut name.

_Usage_

```js
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import { useSelect } from '@wordpress/data';
import { createInterpolateElement } from '@wordpress/element';
import { sprintf } from '@wordpress/i18n';

const ExampleComponent = () => {
	const allShortcutKeyCombinations = useSelect(
		( select ) =>
			select( keyboardShortcutsStore ).getAllShortcutKeyCombinations(
				'core/editor/next-region'
			),
		[]
	);

	return (
		allShortcutKeyCombinations.length > 0 && (
			<ul>
				{ allShortcutKeyCombinations.map(
					( { character, modifier }, index ) => (
						<li key={ index }>
							{ createInterpolateElement(
								sprintf(
									'Character: <code>%s</code> / Modifier: <code>%s</code>',
									character,
									modifier
								),
								{
									code: <code />,
								}
							) }
						</li>
					)
				) }
			</ul>
		)
	);
};
```

_Parameters_

-   _state_ `Object`: Global state.
-   _name_ `string`: Shortcut name.

_Returns_

-   `WPShortcutKeyCombination[]`: Key combinations.

### getAllShortcutRawKeyCombinations

返回给定快捷键名称的所有键盘组合的原始表示形式。

_用法_

```js
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import { useSelect } from '@wordpress/data';
import { createInterpolateElement } from '@wordpress/element';
import { sprintf } from '@wordpress/i18n';

const ExampleComponent = () => {
	const allShortcutRawKeyCombinations = useSelect(
		( select ) =>
			select( keyboardShortcutsStore ).getAllShortcutRawKeyCombinations(
				'core/editor/next-region'
			),
		[]
	);

	return (
		allShortcutRawKeyCombinations.length > 0 && (
			<ul>
				{ allShortcutRawKeyCombinations.map(
					( shortcutRawKeyCombination, index ) => (
						<li key={ index }>
							{ createInterpolateElement(
								sprintf(
									' <code>%s</code>',
									shortcutRawKeyCombination
								),
								{
									code: <code />,
								}
							) }
						</li>
					)
				) }
			</ul>
		)
	);
};
```

_参数_

-   _state_ `Object`: 全局状态。
-   _name_ `string`: 快捷键名称。

_返回值_

-   `string[]`: 快捷键数组。

### getCategoryShortcuts

返回给定分类名称的快捷键名称列表。

_用法_

```js
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import { useSelect } from '@wordpress/data';

const ExampleComponent = () => {
	const categoryShortcuts = useSelect(
		( select ) =>
			select( keyboardShortcutsStore ).getCategoryShortcuts( 'block' ),
		[]
	);

	return (
		categoryShortcuts.length > 0 && (
			<ul>
				{ categoryShortcuts.map( ( categoryShortcut ) => (
					<li key={ categoryShortcut }>{ categoryShortcut }</li>
				) ) }
			</ul>
		)
	);
};
```

_参数_

-   _state_ `Object`: 全局状态。
-   _name_ `string`: 分类名称。

_返回值_

-   `string[]`: 快捷键名称。

### getShortcutAliases

Returns the aliases for a given shortcut name.

_Usage_

```js
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import { useSelect } from '@wordpress/data';
import { createInterpolateElement } from '@wordpress/element';
import { sprintf } from '@wordpress/i18n';
const ExampleComponent = () => {
	const shortcutAliases = useSelect(
		( select ) =>
			select( keyboardShortcutsStore ).getShortcutAliases(
				'core/editor/next-region'
			),
		[]
	);

	return (
		shortcutAliases.length > 0 && (
			<ul>
				{ shortcutAliases.map( ( { character, modifier }, index ) => (
					<li key={ index }>
						{ createInterpolateElement(
							sprintf(
								'Character: <code>%s</code> / Modifier: <code>%s</code>',
								character,
								modifier
							),
							{
								code: <code />,
							}
						) }
					</li>
				) ) }
			</ul>
		)
	);
};
```

_Parameters_

-   _state_ `Object`: Global state.
-   _name_ `string`: Shortcut name.

_Returns_

-   `WPShortcutKeyCombination[]`: Key combinations.

### getShortcutDescription

根据快捷键名称返回其描述。

_用法_

```js
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
const ExampleComponent = () => {
	const shortcutDescription = useSelect(
		( select ) =>
			select( keyboardShortcutsStore ).getShortcutDescription(
				'core/editor/next-region'
			),
		[]
	);

	return shortcutDescription ? (
		<div>{ shortcutDescription }</div>
	) : (
		<div>{ __( 'No description.' ) }</div>
	);
};
```

_参数_

-   _state_ `Object`: 全局状态。
-   _name_ `string`: 快捷键名称。

_返回值_

-   `?string`: 快捷键描述。

### getShortcutKeyCombination

Returns the main key combination for a given shortcut name.

_Usage_

```js
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import { useSelect } from '@wordpress/data';
import { createInterpolateElement } from '@wordpress/element';
import { sprintf } from '@wordpress/i18n';
const ExampleComponent = () => {
	const { character, modifier } = useSelect(
		( select ) =>
			select( keyboardShortcutsStore ).getShortcutKeyCombination(
				'core/editor/next-region'
			),
		[]
	);

	return (
		<div>
			{ createInterpolateElement(
				sprintf(
					'Character: <code>%s</code> / Modifier: <code>%s</code>',
					character,
					modifier
				),
				{
					code: <code />,
				}
			) }
		</div>
	);
};
```

_Parameters_

-   _state_ `Object`: Global state.
-   _name_ `string`: Shortcut name.

_Returns_

-   `WPShortcutKeyCombination?`: Key combination.

### getShortcutRepresentation

返回一个字符串，表示给定快捷键名称的主要按键组合。

_用法_

```js
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import { useSelect } from '@wordpress/data';
import { sprintf } from '@wordpress/i18n';

const ExampleComponent = () => {
	const { display, raw, ariaLabel } = useSelect( ( select ) => {
		return {
			display: select( keyboardShortcutsStore ).getShortcutRepresentation(
				'core/editor/next-region'
			),
			raw: select( keyboardShortcutsStore ).getShortcutRepresentation(
				'core/editor/next-region',
				'raw'
			),
			ariaLabel: select(
				keyboardShortcutsStore
			).getShortcutRepresentation(
				'core/editor/next-region',
				'ariaLabel'
			),
		};
	}, [] );

	return (
		<ul>
			<li>{ sprintf( 'display string: %s', display ) }</li>
			<li>{ sprintf( 'raw string: %s', raw ) }</li>
			<li>{ sprintf( 'ariaLabel string: %s', ariaLabel ) }</li>
		</ul>
	);
};
```

_参数_

-   _state_ `Object`: 全局状态。
-   _name_ `string`: 快捷键名称。
-   _representation_ `keyof FORMATTING_METHODS`: 表示类型（display, raw, ariaLabel）。

_返回值_

-   `?string`: 快捷键表示字符串。

<!-- END TOKEN(Autogenerated selectors|../../../packages/keyboard-shortcuts/src/store/selectors.js) -->

## Actions

<!-- START TOKEN(Autogenerated actions|../../../packages/keyboard-shortcuts/src/store/actions.js) -->

### registerShortcut

Returns an action object used to register a new keyboard shortcut.

_Usage_

```js
import { useEffect } from 'react';
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

const ExampleComponent = () => {
	const { registerShortcut } = useDispatch( keyboardShortcutsStore );

	useEffect( () => {
		registerShortcut( {
			name: 'custom/my-custom-shortcut',
			category: 'my-category',
			description: __( 'My custom shortcut' ),
			keyCombination: {
				modifier: 'primary',
				character: 'j',
			},
		} );
	}, [] );

	const shortcut = useSelect(
		( select ) =>
			select( keyboardShortcutsStore ).getShortcutKeyCombination(
				'custom/my-custom-shortcut'
			),
		[]
	);

	return shortcut ? (
		<p>{ __( 'Shortcut is registered.' ) }</p>
	) : (
		<p>{ __( 'Shortcut is not registered.' ) }</p>
	);
};
```

_Parameters_

-   _config_ `WPShortcutConfig`: Shortcut config.

_Returns_

-   `Object`: action.

### unregisterShortcut

返回一个用于注销键盘快捷键的操作对象。

_用法_

```js
import { useEffect } from 'react';
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

const ExampleComponent = () => {
	const { unregisterShortcut } = useDispatch( keyboardShortcutsStore );

	useEffect( () => {
		unregisterShortcut( 'core/editor/next-region' );
	}, [] );

	const shortcut = useSelect(
		( select ) =>
			select( keyboardShortcutsStore ).getShortcutKeyCombination(
				'core/editor/next-region'
			),
		[]
	);

	return shortcut ? (
		<p>{ __( 'Shortcut is not unregistered.' ) }</p>
	) : (
		<p>{ __( 'Shortcut is unregistered.' ) }</p>
	);
};
```

_参数_

-   _name_ `string`: 快捷键名称。

_返回值_

-   `Object`: 操作对象。

<!-- END TOKEN(Autogenerated actions|../../../packages/keyboard-shortcuts/src/store/actions.js) -->