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
| Property | Type | Description |
|---|---|---|
| buttonEl | HTMLButtonElement | The button that shows the hotkey and starts the recording. |
| clearButton | ExtraButtonComponent | The button that clears the hotkey. |
| onChange | (callback: (newValue: Hotkey | null) => void) => this | Sets a callback function to be called when the value of the component changes. (Inherited from ValueComponentWithChangeTracking) |
Methods
| Method | Returns | Description |
|---|---|---|
| getValue() | Hotkey | null | Gets the hotkey. |
| onChange(callback) | this | Sets the callback to be called when the user records or clears a hotkey. |
| setDisabled(isDisabled) | this | Sets the disabled state of the component. |
| setValue(value) | this | Sets the hotkey. |
| startRecording() | void | Starts waiting for a key press. |
| stopRecording() | void | Stops waiting for a key press, keeping the current hotkey. |
Links to this page: