Skip to content

HotkeyComponent

A component that records a hotkey: click the button, then press the key combination. Escape cancels the recording, and the clear button sets the value to null.

The value is a Hotkey, the shape Obsidian stores in hotkeys.json, so it can be handed to Scope.register.

You can add this component using SettingEx.addHotkey.

In order to add the styles for the component, use initPluginContext in your plugin’s onload() function.

Alternatively, you can copy styles from https://unpkg.com/@mnaoumov/obsidian-lib/dist/styles.css.

Import:

import { HotkeyComponent } from '@mnaoumov/obsidian-lib/setting-components/hotkey-component';

Signature:

export class HotkeyComponent extends ValueComponent<Hotkey | null> implements ValueComponentWithChangeTracking<Hotkey | null>

Extends: ValueComponent<Hotkey | null>

Implements: ValueComponentWithChangeTracking<Hotkey | null>

Constructor

new HotkeyComponent(containerEl: HTMLElement)

Creates a new hotkey component.

Properties

PropertyTypeDescription
buttonElHTMLButtonElementThe button that shows the hotkey and starts the recording.
clearButtonExtraButtonComponentThe button that clears the hotkey.
onChange(callback: (newValue: Hotkey | null) => void) => thisSets a callback function to be called when the value of the component changes.
(Inherited from ValueComponentWithChangeTracking)

Methods

MethodReturnsDescription
getValue()Hotkey | nullGets the hotkey.
onChange(callback)thisSets the callback to be called when the user records or clears a hotkey.
setDisabled(isDisabled)thisSets the disabled state of the component.
setValue(value)thisSets the hotkey.
startRecording()voidStarts waiting for a key press.
stopRecording()voidStops waiting for a key press, keeping the current hotkey.

Links to this page: