~james/core

core/web/src/lib/shiftKey.svelte.ts -rw-r--r-- 586 bytes
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
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
import { on } from "svelte/events";
import { createSubscriber } from "svelte/reactivity";

let held = false;

const subscribe = createSubscriber((update) => {
	const set = (next: boolean) => {
		if (held === next) return;
		held = next;
		update();
	};
	const track = (event: KeyboardEvent) => set(event.shiftKey);
	const offs = [
		on(window, "keydown", track),
		on(window, "keyup", track),
		on(window, "blur", () => set(false))
	];
	return () => {
		for (const off of offs) off();
		held = false;
	};
});

export const shiftKey = {
	get held() {
		subscribe();
		return held;
	}
};