类: FShortcut
The Facade API object to handle shortcuts in Univer
方法
disableShortcut()
disableShortcut(): this;Disable shortcuts of Univer.
返回
this
The Facade API instance itself for chaining.
示例
const fShortcut = univerAPI.getShortcut();
fShortcut.disableShortcut();dispatchShortcutEvent()
dispatchShortcutEvent(e): any;Dispatch a KeyboardEvent to the shortcut service and return the matched shortcut item.
参数
| 参数 | 类型 | 描述 | 
|---|---|---|
e | KeyboardEvent | The KeyboardEvent to dispatch. | 
返回
any
The matched shortcut item.
示例
const fShortcut = univerAPI.getShortcut();
const pseudoEvent = new KeyboardEvent('keydown', { key: 's', ctrlKey: true });
const ifShortcutItem = fShortcut.dispatchShortcutEvent(pseudoEvent);
if (ifShortcutItem) {
  const commandId = ifShortcutItem.id;
  // Do something with the commandId.
}enableShortcut()
enableShortcut(): this;Enable shortcuts of Univer.
返回
this
The Facade API instance itself for chaining.
示例
fShortcut.enableShortcut(); // Use the FShortcut instance used by disableShortcut before, do not create a new instancetriggerShortcut()
triggerShortcut(e): any;Trigger shortcut of Univer by a KeyboardEvent and return the matched shortcut item.
参数
| 参数 | 类型 | 描述 | 
|---|---|---|
e | KeyboardEvent | The KeyboardEvent to trigger. | 
返回
any
The matched shortcut item.
示例
// Assum the current sheet is empty sheet.
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const fRange = fWorksheet.getRange('A1');
 
// Set A1 cell active and set value to 'Hello Univer'.
fRange.activate();
fRange.setValue('Hello Univer');
console.log(fRange.getCellStyle().bold); // false
 
// Set A1 cell bold by shortcut.
const fShortcut = univerAPI.getShortcut();
const pseudoEvent = new KeyboardEvent('keydown', {
  key: 'b',
  ctrlKey: true,
  keyCode: univerAPI.Enum.KeyCode.B
});
const ifShortcutItem = fShortcut.triggerShortcut(pseudoEvent);
if (ifShortcutItem) {
  const commandId = ifShortcutItem.id;
  console.log(fRange.getCellStyle().bold); // true
}