-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from Nerimity/Push-To-talk
Prepare event handling
- Loading branch information
Showing
8 changed files
with
428 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { onCleanup, onMount } from "solid-js"; | ||
import { electronWindowAPI } from "./Electron"; | ||
import { createStore, reconcile } from "solid-js/store"; | ||
|
||
export const [downKeys, setDownKeys] = createStore<(string | number)[]>([]); | ||
|
||
const onMouseDown = (e: MouseEvent) => { | ||
if (e.button === 0) return; | ||
const code = `MOUSE ${e.button}`; | ||
if (!downKeys.includes(code)) { | ||
setDownKeys([...downKeys, code]); | ||
} | ||
}; | ||
const onMouseUp = (e: MouseEvent) => { | ||
if (e.button === 0) return; | ||
const code = `MOUSE ${e.button}`; | ||
setDownKeys(downKeys.filter((k) => k !== code)); | ||
}; | ||
|
||
const onKeyDown = (e: KeyboardEvent) => { | ||
let code = e.code || e.key; | ||
if (code.startsWith("Key")) { | ||
code = code.slice(3); | ||
} | ||
if (!downKeys.includes(code)) { | ||
setDownKeys([...downKeys, code]); | ||
} | ||
}; | ||
|
||
const onKeyUp = (e: KeyboardEvent) => { | ||
let code = e.code || e.key; | ||
if (code.startsWith("Key")) { | ||
code = code.slice(3); | ||
} | ||
setDownKeys(downKeys.filter((k) => k !== code)); | ||
}; | ||
|
||
if (electronWindowAPI()?.isElectron) { | ||
electronWindowAPI()?.onGlobalKey(({ event }) => { | ||
const key = event.name || event.vKey; | ||
if (event.name === "MOUSE LEFT") return; | ||
if (event.state === "DOWN") { | ||
if (!downKeys.includes(key)) { | ||
setDownKeys([...downKeys, key]); | ||
} | ||
} else { | ||
setDownKeys(downKeys.filter((k) => k !== key)); | ||
} | ||
}); | ||
} | ||
|
||
export const useGlobalKey = () => { | ||
let started = false; | ||
const start = () => { | ||
started = true; | ||
if (!electronWindowAPI()?.isElectron) { | ||
document.addEventListener("mousedown", onMouseDown); | ||
document.addEventListener("mouseup", onMouseUp); | ||
document.addEventListener("keydown", onKeyDown); | ||
document.addEventListener("keyup", onKeyUp); | ||
return; | ||
} | ||
electronWindowAPI()?.startGlobalKeyListener(); | ||
}; | ||
|
||
const stop = () => { | ||
if (!started) return; | ||
setDownKeys(reconcile([])); | ||
if (!electronWindowAPI()?.isElectron) { | ||
document.removeEventListener("mousedown", onMouseDown); | ||
document.removeEventListener("mouseup", onMouseUp); | ||
document.removeEventListener("keydown", onKeyDown); | ||
document.removeEventListener("keyup", onKeyUp); | ||
return; | ||
} | ||
electronWindowAPI()?.stopGlobalKeyListener(); | ||
}; | ||
|
||
onCleanup(() => { | ||
stop(); | ||
}); | ||
|
||
return { start, stop }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export function arrayEquals<A extends unknown[], B extends unknown[]>( | ||
a: A, | ||
b: B | ||
) { | ||
return a.length === b.length && a.every((v, i) => v === b[i]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.