-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Shortcuts.svelte
41 lines (37 loc) · 1.21 KB
/
Shortcuts.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<script>
import { writable } from 'svelte/store'
import { registerShortcut, unregisterShortcut } from '@tauri-apps/api/globalShortcut'
export let onMessage
const shortcuts = writable([])
let shortcut = 'CTRL+X'
function register() {
const shortcut_ = shortcut
registerShortcut(shortcut_, () => {
onMessage(`Shortcut ${shortcut_} triggered`)
}).then(() => {
shortcuts.update(shortcuts_ => [...shortcuts_, shortcut_])
onMessage(`Shortcut ${shortcut_} registered successfully`)
}).catch(onMessage)
}
function unregister(shortcut) {
const shortcut_ = shortcut
unregisterShortcut(shortcut_).then(() => {
shortcuts.update(shortcuts_ => shortcuts_.filter(s => s !== shortcut_))
onMessage(`Shortcut ${shortcut_} unregistered`)
}).catch(onMessage)
}
</script>
<div style="margin-top: 24px">
<div>
<input placeholder="Type a shortcut with '+' as separator..." bind:value={shortcut}>
<button type="button" on:click={register}>Register</button>
</div>
<div>
{#each $shortcuts as savedShortcut}
<div>
{savedShortcut}
<button type="button" on:click={()=> unregister(savedShortcut)}>Unregister</button>
</div>
{/each}
</div>
</div>