Skip to content

Commit 5b6f4ba

Browse files
committed
Allow cancelling gamepad binding
Can be cancelled by pressing the Esc key. Also fix when polling gets triggered (whoops).
1 parent eac9919 commit 5b6f4ba

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

http/button-mapper.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,12 @@ export function ButtonMapper({
6262

6363
const [target, setTarget] = useState(/** @type {[string, keyof Mapping]|null} */ (null));
6464
useEffect(() => {
65+
if (!target) {
66+
return;
67+
}
6568
return waitForGamepadInput(padInput => {
66-
if (!target) {
69+
setTarget(null);
70+
if (!padInput) {
6771
return;
6872
}
6973
setNewMappings((/** @type {Mappings} */ m) => {
@@ -81,7 +85,6 @@ export function ButtonMapper({
8185
};
8286
return updated;
8387
});
84-
setTarget(null);
8588
});
8689
}, [target, setTarget, setNewMappings]);
8790

http/mapping.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ function ignoreDeadzone(val) {
257257
}
258258

259259
/**
260-
* @param {function(PadInput): void} callback
260+
* @param {function(PadInput|null): void} callback
261261
* @returns {function(): void} a cancel/cleanup function
262262
*/
263263
export function waitForGamepadInput(callback) {
@@ -290,8 +290,18 @@ export function waitForGamepadInput(callback) {
290290
}
291291
const interval = setInterval(findPressedInput, 100);
292292

293+
/** @type {function(KeyboardEvent): void} */
294+
const cancelHandler = e => {
295+
if (e.key === 'Escape') {
296+
e.preventDefault();
297+
callback(null);
298+
}
299+
}
300+
document.addEventListener('keydown', cancelHandler);
301+
293302
return function cleanup() {
294303
clearInterval(interval);
304+
document.removeEventListener('keydown', cancelHandler);
295305
};
296306
}
297307

0 commit comments

Comments
 (0)