FShortcut

GitHubEdit on GitHub
packages@univerjs/ui

The Facade API object to handle shortcuts in Univer

APIs

disableShortcut

Disable shortcuts of Univer.

Signature

disableShortcut(): this

Returns

  • (this) — The Facade API instance itself for chaining.

Examples

const fShortcut = univerAPI.getShortcut()
fShortcut.disableShortcut()

dispatchShortcutEvent

Dispatch a KeyboardEvent to the shortcut service and return the matched shortcut item.

Signature

dispatchShortcutEvent(e: KeyboardEvent): IShortcutItem<object> | undefined

Parameters

  • e (KeyboardEvent) — - The KeyboardEvent to dispatch.

Returns

  • (IShortcutItem<object> | undefined) — The matched shortcut item.

Examples

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

Enable shortcuts of Univer.

Signature

enableShortcut(): this

Returns

  • (this) — The Facade API instance itself for chaining.

Examples

fShortcut.enableShortcut() // Use the FShortcut instance used by disableShortcut before, do not create a new instance

triggerShortcut

Trigger shortcut of Univer by a KeyboardEvent and return the matched shortcut item.

Signature

triggerShortcut(e: KeyboardEvent): IShortcutItem<object> | undefined

Parameters

  • e (KeyboardEvent) — - The KeyboardEvent to trigger.

Returns

  • (IShortcutItem<object> | undefined) — The matched shortcut item.

Examples

// 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
}