feat(kbd): simulate key press

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-09-12 23:08:44 +08:00
parent a386fd6e97
commit db2cd99f71
2 changed files with 93 additions and 18 deletions
+69 -15
View File
@@ -73,6 +73,60 @@ export const KbdCombined: FC<{
</div>
)
}
function simulateKeyPress(key: string) {
const keyCodes = {
"0": 48,
"1": 49,
"2": 50,
"3": 51,
"4": 52,
"5": 53,
"6": 54,
"7": 55,
"8": 56,
"9": 57,
a: 65,
b: 66,
c: 67,
d: 68,
e: 69,
f: 70,
g: 71,
h: 72,
i: 73,
j: 74,
k: 75,
l: 76,
m: 77,
n: 78,
o: 79,
p: 80,
q: 81,
r: 82,
s: 83,
t: 84,
u: 85,
v: 86,
w: 87,
x: 88,
y: 89,
z: 90,
}
const code = keyCodes[key] || key.codePointAt(0)
const event = new KeyboardEvent("keydown", {
key,
code: `Key${key.toUpperCase()}`,
keyCode: code,
which: code,
bubbles: true,
cancelable: true,
})
document.dispatchEvent(event)
}
export const Kbd: FC<{ children: string; className?: string }> = memo(({ children, className }) => {
let specialKeys = (SpecialKeys as any)[os] as Record<string, string>
specialKeys = { ...SharedKeys, ...specialKeys }
@@ -91,16 +145,16 @@ export const Kbd: FC<{ children: string; className?: string }> = memo(({ childre
}
}, [children])
const handleClick = React.useCallback(() => {
setIsKeyPressed(true)
setTimeout(() => {
setIsKeyPressed(false)
}, 100)
simulateKeyPress(children.trim())
}, [children])
return (
<button
type="button"
onClick={() => {
setIsKeyPressed(true)
setTimeout(() => {
setIsKeyPressed(false)
}, 100)
}}
>
<button type="button" onClick={handleClick}>
<kbd
className={cn(
"kbd h-5 space-x-1 text-[0.7rem]",
@@ -164,7 +218,7 @@ function MaterialSymbolsKeyboardCommandKey(props: React.SVGProps<SVGSVGElement>)
<path
fill="currentColor"
d="M6.5 21q-1.45 0-2.475-1.025T3 17.5t1.025-2.475T6.5 14H8v-4H6.5q-1.45 0-2.475-1.025T3 6.5t1.025-2.475T6.5 3t2.475 1.025T10 6.5V8h4V6.5q0-1.45 1.025-2.475T17.5 3t2.475 1.025T21 6.5t-1.025 2.475T17.5 10H16v4h1.5q1.45 0 2.475 1.025T21 17.5t-1.025 2.475T17.5 21t-2.475-1.025T14 17.5V16h-4v1.5q0 1.45-1.025 2.475T6.5 21m0-2q.625 0 1.063-.437T8 17.5V16H6.5q-.625 0-1.062.438T5 17.5t.438 1.063T6.5 19m11 0q.625 0 1.063-.437T19 17.5t-.437-1.062T17.5 16H16v1.5q0 .625.438 1.063T17.5 19M10 14h4v-4h-4zM6.5 8H8V6.5q0-.625-.437-1.062T6.5 5t-1.062.438T5 6.5t.438 1.063T6.5 8M16 8h1.5q.625 0 1.063-.437T19 6.5t-.437-1.062T17.5 5t-1.062.438T16 6.5z"
/>
/>
</svg>
)
}
@@ -191,7 +245,7 @@ function MaterialSymbolsShiftOutlineRounded(props: React.SVGProps<SVGSVGElement>
<path
fill="currentColor"
d="M8 20v-7H5.1q-.65 0-.912-.562t.137-1.063l6.9-8.425q.3-.375.775-.375t.775.375l6.9 8.425q.4.5.138 1.063T18.9 13H16v7q0 .425-.288.713T15 21H9q-.425 0-.712-.288T8 20m2-1h4v-8h2.775L12 5.15L7.225 11H10zm2-8"
/>
/>
</svg>
)
}
@@ -202,7 +256,7 @@ function MaterialSymbolsKeyboardTabRounded(props: React.SVGProps<SVGSVGElement>)
<path
fill="currentColor"
d="M21 18q-.425 0-.712-.288T20 17V7q0-.425.288-.712T21 6t.713.288T22 7v10q0 .425-.288.713T21 18m-6.825-5H3q-.425 0-.712-.288T2 12t.288-.712T3 11h11.175L11.3 8.1q-.275-.275-.288-.687T11.3 6.7q.275-.275.7-.275t.7.275l4.6 4.6q.15.15.213.325t.062.375t-.062.375t-.213.325l-4.6 4.6q-.275.275-.687.275T11.3 17.3q-.3-.3-.3-.712t.3-.713z"
/>
/>
</svg>
)
}
@@ -213,7 +267,7 @@ function MaterialSymbolsSpaceBarRounded(props: React.SVGProps<SVGSVGElement>) {
<path
fill="currentColor"
d="M6 15q-.825 0-1.412-.587T4 13v-3q0-.425.288-.712T5 9t.713.288T6 10v3h12v-3q0-.425.288-.712T19 9t.713.288T20 10v3q0 .825-.587 1.413T18 15z"
/>
/>
</svg>
)
}
@@ -223,7 +277,7 @@ function MaterialSymbolsBackspace(props: React.SVGProps<SVGSVGElement>) {
<path
fill="currentColor"
d="M7.95 19L3 12l4.95-7H21v14zm3.45-3l2.6-2.6l2.6 2.6l1.4-1.4l-2.6-2.6L18 9.4L16.6 8L14 10.6L11.4 8L10 9.4l2.6 2.6l-2.6 2.6z"
/>
/>
</svg>
)
}
@@ -234,7 +288,7 @@ function MaterialSymbolsWindowOutlineSharp(props: React.SVGProps<SVGSVGElement>)
<path
fill="currentColor"
d="M21 21H3V3h18zm-8-8v6h6v-6zm0-2h6V5h-6zm-2 0V5H5v6zm0 2H5v6h6z"
/>
/>
</svg>
)
}
+24 -3
View File
@@ -1,3 +1,4 @@
import { useUISettingKey } from "@renderer/atoms/settings/ui"
import { MotionButtonBase } from "@renderer/components/ui/button"
import { KbdCombined } from "@renderer/components/ui/kbd/Kbd"
import { useCurrentModal, useModalStack } from "@renderer/components/ui/modal"
@@ -5,19 +6,39 @@ import { NoopChildren } from "@renderer/components/ui/modal/stacked/utils"
import { ScrollArea } from "@renderer/components/ui/scroll-area"
import { shortcuts } from "@renderer/constants/shortcuts"
import { cn } from "@renderer/lib/utils"
import { m } from "framer-motion"
import clsx from "clsx"
import { m, useDragControls } from "framer-motion"
import { useCallback } from "react"
const ShortcutModalContent = () => {
const { dismiss } = useCurrentModal()
const modalOverlay = useUISettingKey("modalOverlay")
const dragControls = useDragControls()
return (
<m.div
drag
dragListener={false}
dragControls={dragControls}
dragMomentum={false}
dragElastic={0}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className="center absolute inset-0 m-auto flex max-h-[80vh] w-[60ch] max-w-[90vw] flex-col rounded-md border bg-theme-modal-background-opaque"
whileDrag={{
cursor: "grabbing",
}}
className={clsx(
"center absolute inset-0 m-auto flex max-h-[80vh] w-[60ch] max-w-[90vw] flex-col rounded-xl border bg-theme-modal-background-opaque",
!modalOverlay && "shadow-modal",
)}
>
<h2 className="mb-2 mt-6 font-medium">Shortcuts Guideline</h2>
<h2
onPointerDownCapture={dragControls.start.bind(dragControls)}
className="mb-2 pt-6 font-medium"
>
Shortcuts Guideline
</h2>
<MotionButtonBase onClick={dismiss} className="absolute right-3 top-5 p-2">
<i className="i-mgc-close-cute-re" />
</MotionButtonBase>