Tutorial: Using hotkeys

Using hotkeys

This example shows the example of using the hotkeys option on Windows/Views and the corresponding hotkey event emitted when a specified hotkey is pressed.

Defining the hotkey

const myMagicWindow = await fin.Window.create({
        name: 'magicWin',
        hotkeys: [
            {
                keys: 'Ctrl+M',
            }
        ]
});

Listening to the hotkey

myMagicWindow.on('hotkey', (hotkeyEvent) => {
    console.log(`A hotkey was pressed in the magic window!: ${JSON.stringify(hotkeyEvent)}`);
});

Removing a hotkey

After the following change, the hotkey event will no longer be emitted when Ctrl+M is pressed:

const currentHotkeys = (await myMagicWindow.getOptions()).hotkeys;
const newHotkeys = currentHotkeys.filter(hotkey => hotkey.keys !== 'Ctrl+M');
myMagicWindow.updateOptions({
    hotkeys: newHotkeys
});

Note: The hotkeys option is configured per-instance and isn't passed down to the children of Window/View. Therefore, if you want a Window/View and all of its children to support hotkeys, you must configure the hotkeys option for every created child.