### 注销快捷键 返回用于注销键盘快捷键的操作对象。 _用法_ ```js import { useEffect } from 'react'; import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts'; import { useSelect, useDispatch } from '@wordpress/data'; import { __ } from '@wordpress/i18n'; const 示例组件 = () => { const { 注销快捷键 } = useDispatch( keyboardShortcutsStore ); useEffect( () => { 注销快捷键( 'core/editor/next-region' ); }, [] ); const 快捷键 = useSelect( ( select ) => select( keyboardShortcutsStore ).获取快捷键组合( 'core/editor/next-region' ), [] ); return 快捷键 ? (

{ __( '快捷键未注销。' ) }

) : (

{ __( '快捷键已注销。' ) }

); }; ``` _参数_ - _名称_ `字符串`: 快捷键名称。 _返回值_ - `对象`: 操作对象。 # 键盘快捷键数据 命名空间:`core/keyboard-shortcuts` ## 选择器 ### getAllShortcutKeyCombinations 返回包含指定快捷键名称别名的所有快捷键组合。 _使用方法_ ```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 && ( ) ); }; ``` _参数_ - _state_ `Object`: 全局状态 - _name_ `string`: 快捷键名称 _返回值_ - `WPShortcutKeyCombination[]`: 按键组合数组 ### 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 && ( ) ); }; ``` _参数_ - _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 && ( ) ); }; ``` _参数_ - _state_ `Object`: 全局状态 - _name_ `string`: 分类名称 _返回值_ - `string[]`: 快捷键名称数组 ### getShortcutAliases 返回指定快捷键名称的别名。 _使用方法_ ```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 && ( ) ); }; ``` _参数_ - _state_ `Object`: 全局状态 - _name_ `string`: 快捷键名称 _返回值_ - `WPShortcutKeyCombination[]`: 按键组合数组 ### 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 ? (
{ shortcutDescription }
) : (
{ __( '无描述信息。' ) }
); }; ``` _参数_ - _state_ `Object`: 全局状态。 - _name_ `string`: 快捷键名称。 _返回值_ - `?string`: 快捷键描述。 ### getShortcutKeyCombination 返回指定快捷键名称的主键组合。 _使用方法_ ```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 (
{ createInterpolateElement( sprintf( '字符:%s / 修饰键:%s', character, modifier ), { code: , } ) }
); }; ``` _参数_ - _state_ `Object`: 全局状态。 - _name_ `string`: 快捷键名称。 _返回值_ - `WPShortcutKeyCombination?`: 键位组合。 ### 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 (
  • { sprintf( '显示字符串:%s', display ) }
  • { sprintf( '原始字符串:%s', raw ) }
  • { sprintf( '无障碍标签字符串:%s', ariaLabel ) }
); }; ``` _参数_ - _state_ `Object`: 全局状态。 - _name_ `string`: 快捷键名称。 - _representation_ `keyof FORMATTING_METHODS`: 表示类型(display, raw, ariaLabel)。 _返回值_ - `?string`: 快捷键表示形式。 ## 操作 ### registerShortcut 返回用于注册新键盘快捷键的操作对象。 _使用方法_ ```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: __( '我的自定义快捷键' ), keyCombination: { modifier: 'primary', character: 'j', }, } ); }, [] ); const shortcut = useSelect( ( select ) => select( keyboardShortcutsStore ).getShortcutKeyCombination( 'custom/my-custom-shortcut' ), [] ); return shortcut ? (

{ __( '快捷键已注册。' ) }

) : (

{ __( '快捷键未注册。' ) }

); }; ``` _参数_ - _config_ `WPShortcutConfig`: 快捷键配置。 _返回值_ - `Object`: 操作对象。