Skip to content
New issue

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

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

Already on GitHub? # to your account

feat: 키보드 동작 트리거를 위한 custom event #6

Merged
merged 3 commits into from
Feb 24, 2023
Merged
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
35 changes: 31 additions & 4 deletions src/components/Keyboard/Key/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ThreeEvent, useFrame } from '@react-three/fiber'
import React, { useRef, useState } from 'react'
import React, { useEffect, useRef, useState } from 'react'
import KeyCap from './KeyCap'
import KeyLegend from './KeyLegend'
import { IkeyConfig } from '../../../types/KeyboardType'
import { KEY_DOWN_DURATION, KEY_DOWN_POSITION } from './consts'
import { Howl } from 'howler'
import { ThreeEventType } from '../../../types/ThreeEvent'

type KeyProps = { keyConfig: IkeyConfig }

Expand All @@ -13,19 +14,31 @@ export default (props: KeyProps) => {
const [keyDownPosition, setKeyDownPosition] = useState<number>(0)
const howl = useRef<Howl>(new Howl({ src: 'sample_audio.ogg' }))

useEffect(() => {
document.addEventListener('threekeyboardevent', keyboardEventHander)

return () => {
document.removeEventListener('threekeyboardevent', keyboardEventHander)
}
}, [])

useFrame(() => {
keyDownPosition < 0 &&
setKeyDownPosition((prev) => prev + KEY_DOWN_DURATION)
})

const onKeyClick = (evt: ThreeEvent<MouseEvent>) => {
evt.stopPropagation()
const onKeyClick = (evt?: ThreeEvent<MouseEvent>) => {
evt?.stopPropagation()
setKeyDownPosition(KEY_DOWN_POSITION)
howl.current?.play()
}

const keyboardEventHander = (evt: CustomEvent<ThreeEventType>) => {
if (evt.detail?.keyId === keyConfig.legend?.text.toLowerCase()) onKeyClick()
}

return (
<group onClick={onKeyClick} position={[0, keyDownPosition, 0]}>
<group position={[0, keyDownPosition, 0]}>
<KeyCap
config={keyConfig}
position={[keyConfig.column, 0, keyConfig.row]}
Expand All @@ -37,3 +50,17 @@ export default (props: KeyProps) => {
</group>
)
}

export const genCustomKeyEventFromCharacter = (
char: string,
): CustomEvent<ThreeEventType> => {
if (char.length !== 1) {
throw new Error(
`Invalid input for custom key event. Input should be single charater. Input is ${char}, though.`,
)
}

return new CustomEvent<ThreeEventType>('threekeyboardevent', {
detail: { keyId: char.toLowerCase() },
})
}
20 changes: 12 additions & 8 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
import React from 'react'
import ReactDOM from 'react-dom/client'
import './index.css'
import App from './App'
import { genCustomKeyEventFromCharacter } from './components/Keyboard/Key'

const root = ReactDOM.createRoot(
document.getElementById("root") as HTMLElement
);
root.render(<App />);
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement)
root.render(<App />)

document.addEventListener('keydown', (evt) => {
const event = genCustomKeyEventFromCharacter(evt.key)
document.dispatchEvent(event)
})
24 changes: 24 additions & 0 deletions src/types/ThreeEvent.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export type ThreeEventType = {
keyId?: string
}

interface ThreeCustomEvent {
threekeyboardevent: CustomEvent<ThreeEventType>
}
declare global {
interface Document {
//adds definition to Document, but you can do the same with HTMLElement
addEventListener<K extends keyof ThreeCustomEvent>(
type: K,
listener: (this: Document, ev: ThreeCustomEvent[K]) => void,
): void
removeEventListener<K extends keyof ThreeCustomEvent>(
type: K,
listener: (this: Document, ev: ThreeCustomEvent[K]) => void,
): void
dispatchEvent<K extends keyof ThreeCustomEvent>(
ev: ThreeCustomEvent[K],
): void
}
}
export {}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
"jsx": "react-jsx",
"typeRoots": ["src/types/ThreeEvent"]
},
"include": [
"src"
Expand Down