Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ControlOrSuper modifier #13

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ In the `Global Hotkeys` settings, you can enter a system-wide key combination th
will perform the specified command. The key combination is specified following
[Electron's Accelerators](https://www.electronjs.org/docs/api/accelerator).

The plugin also defines the following special keys:

`ControlOrSuper`(or `CtrlOrSuper` for short): Represents `Control` on macOS and `Windows` on Linxu and Windows.

## To-Do

- Improve hotkey settings
24 changes: 22 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
import { App, Modal, Notice, Platform, Plugin, PluginSettingTab, Setting } from 'obsidian';
import * as os from 'node:os'
const remote = require('electron').remote;
const globalShortcut = remote.globalShortcut;

const KEY_MAP_MACOS = {
'ControlOrSuper': 'Control',
'CtrlOrSuper': 'Ctrl',
};

const KEY_MAP_PC = {
'ControlOrSuper': 'Super',
'CtrlOrSuper': 'Super',
};

const KEY_MAP: Record<string, string> = os.platform() === 'darwin' ? KEY_MAP_MACOS : KEY_MAP_PC

function remapHotkey(hotkey: string) {
return hotkey.split('+')
.map(it => it.trim())
.map(it => KEY_MAP[it] ?? it)
.join('+')
}

interface GlobalHotkeysPluginSettings {
accelerators: { [key: string]: string };
}
Expand All @@ -22,7 +42,7 @@ export default class GlobalHotkeysPlugin extends Plugin {

let success = (() => {
try {
return globalShortcut.register(accelerator, () => {
return globalShortcut.register(remapHotkey(accelerator), () => {
const command = app.commands.commands[command_id];
if (!command) return;
this.app.setting.close(); // Ensure all modals are closed?
Expand Down Expand Up @@ -56,7 +76,7 @@ export default class GlobalHotkeysPlugin extends Plugin {
async unregisterGlobalShortcut(command_id:string) {
const accelerator = this.currentlyMapped[command_id];
if (accelerator) {
globalShortcut.unregister(accelerator);
globalShortcut.unregister(remapHotkey(accelerator));
delete this.currentlyMapped[command_id];
}
}
Expand Down