mirror of
https://github.com/CharaChorder/DeviceManager.git
synced 2026-01-19 08:22:53 +00:00
feat: change cloud icon to history, fixes #15 fix: action search items overlap, fixes #16 feat: show tooltips immediately
40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
import {persistentWritable} from "$lib/storage"
|
|
import {get} from "svelte/store"
|
|
|
|
export const osLayout = persistentWritable<Record<string, string>>("os-layout", {})
|
|
|
|
const keysCurrentlyDown = new Set<string>()
|
|
|
|
function keydown({code, key}: KeyboardEvent) {
|
|
const keys = [...keysCurrentlyDown]
|
|
keysCurrentlyDown.add(code)
|
|
|
|
const keyString = JSON.stringify([...keys.sort(), code])
|
|
if (keyString in get(osLayout) || get(osLayout)[JSON.stringify([code])] === key) return
|
|
|
|
osLayout.update(layout => {
|
|
layout[keyString] = key
|
|
return layout
|
|
})
|
|
}
|
|
|
|
function keyup({code}: KeyboardEvent) {
|
|
keysCurrentlyDown.delete(code)
|
|
}
|
|
|
|
export function runLayoutDetection() {
|
|
if ("keyboard" in navigator) {
|
|
;(navigator.keyboard as any).getLayoutMap().then((layout: Map<string, string>) => {
|
|
osLayout.update(osLayout => {
|
|
Object.assign(
|
|
osLayout,
|
|
Object.fromEntries([...layout.entries()].map(([key, value]) => [JSON.stringify([key]), value])),
|
|
)
|
|
return osLayout
|
|
})
|
|
})
|
|
}
|
|
window.addEventListener("keydown", keydown)
|
|
window.addEventListener("keyup", keyup)
|
|
}
|