-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[puzzles] Support puzzle-specific keyboard mappings.
Also make keyboard mapping independent of keyboard layout.
- Loading branch information
Showing
10 changed files
with
138 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,22 @@ | ||
import { Pause } from "./alg-nodes"; | ||
import type { KeyMapping } from "cubing/puzzles/PuzzleLoader"; | ||
import type { AlgLeaf } from "./alg-nodes/AlgNode"; | ||
import { Move } from "./alg-nodes/leaves/Move"; | ||
|
||
const cubeKeyMapping: { [key: number]: AlgLeaf } = { | ||
73: new Move("R"), | ||
75: new Move("R'"), | ||
87: new Move("B"), | ||
79: new Move("B'"), | ||
83: new Move("D"), | ||
76: new Move("D'"), | ||
68: new Move("L"), | ||
69: new Move("L'"), | ||
74: new Move("U"), | ||
70: new Move("U'"), | ||
72: new Move("F"), | ||
71: new Move("F'"), | ||
|
||
78: new Move("x'"), | ||
67: new Move("l"), | ||
82: new Move("l'"), | ||
85: new Move("r"), | ||
77: new Move("r'"), | ||
|
||
88: new Move("d"), | ||
188: new Move("d'"), | ||
|
||
84: new Move("x"), | ||
89: new Move("x"), | ||
66: new Move("x'"), | ||
186: new Move("y"), | ||
59: new Move("y"), | ||
65: new Move("y'"), // 186 is WebKit, 59 is Mozilla; see http://unixpapa.com/js/key.html | ||
80: new Move("z"), | ||
81: new Move("z'"), | ||
|
||
90: new Move("M'"), | ||
190: new Move("M'"), | ||
|
||
192: new Pause(), | ||
}; | ||
|
||
// TODO: options about whether to ignore modifier keys (e.g. alt, ctrl). | ||
// TODO: Support different mappings. | ||
// TODO: Return BaseMove instead? | ||
export function keyToMove(e: KeyboardEvent): AlgLeaf | null { | ||
export function keyToMove( | ||
keyMapping: KeyMapping | undefined, | ||
e: KeyboardEvent, | ||
): AlgLeaf | null { | ||
if (e.altKey || e.ctrlKey) { | ||
return null; | ||
} | ||
|
||
return cubeKeyMapping[e.keyCode] || null; | ||
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code | ||
// > The KeyboardEvent.code property represents a physical key on the keyboard | ||
// > (as opposed to the character generated by pressing the key). In other | ||
// > words, this property returns a value that isn't altered by keyboard | ||
// > layout or the state of the modifier keys. | ||
const layoutIndepdendentCode = e.code; | ||
return keyMapping?.[layoutIndepdendentCode] ?? null; | ||
} |
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
41 changes: 41 additions & 0 deletions
41
src/cubing/puzzles/implementations/3x3x3/cube3x3x3KeyMapping.ts
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,41 @@ | ||
import { Move, Pause, type AlgLeaf } from "../../../alg"; | ||
|
||
// See: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code | ||
|
||
export const cube3x3x3KeyMapping: { [key: number | string]: AlgLeaf } = { | ||
KeyI: new Move("R"), | ||
KeyK: new Move("R'"), | ||
KeyW: new Move("B"), | ||
KeyO: new Move("B'"), | ||
KeyS: new Move("D"), | ||
KeyL: new Move("D'"), | ||
KeyD: new Move("L"), | ||
KeyE: new Move("L'"), | ||
KeyJ: new Move("U"), | ||
KeyF: new Move("U'"), | ||
KeyH: new Move("F"), | ||
KeyG: new Move("F'"), | ||
|
||
KeyN: new Move("x'"), | ||
KeyC: new Move("l"), | ||
KeyR: new Move("l'"), | ||
KeyU: new Move("r"), | ||
KeyM: new Move("r'"), | ||
|
||
KeyX: new Move("d"), | ||
Comma: new Move("d'"), | ||
|
||
KeyT: new Move("x"), | ||
KeyY: new Move("x"), | ||
KeyV: new Move("x'"), | ||
Semicolon: new Move("y"), | ||
KeyA: new Move("y'"), | ||
KeyP: new Move("z"), | ||
KeyQ: new Move("z'"), | ||
|
||
KeyZ: new Move("M'"), | ||
KeyB: new Move("M"), | ||
Period: new Move("M'"), | ||
|
||
Backquote: new Pause(), | ||
}; |
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,39 @@ | ||
import { Move, Pause, type AlgLeaf } from "../../../alg"; | ||
|
||
export const ftoKeyMapping: { [key: number | string]: AlgLeaf } = { | ||
KeyI: new Move("R"), | ||
KeyK: new Move("R'"), | ||
KeyW: new Move("B"), | ||
KeyO: new Move("B'"), | ||
KeyS: new Move("D"), | ||
KeyL: new Move("D'"), | ||
KeyD: new Move("L"), | ||
KeyE: new Move("L'"), | ||
KeyJ: new Move("U"), | ||
KeyF: new Move("U'"), | ||
KeyH: new Move("F"), | ||
KeyG: new Move("F'"), | ||
|
||
KeyN: new Move("Rv'"), | ||
KeyC: new Move("l"), | ||
KeyR: new Move("l'"), | ||
KeyU: new Move("r"), | ||
KeyM: new Move("r'"), | ||
|
||
KeyX: new Move("d"), | ||
Comma: new Move("d'"), | ||
|
||
KeyT: new Move("Lv'"), | ||
KeyY: new Move("Rv"), | ||
KeyV: new Move("Lv"), | ||
Semicolon: new Move("Uv"), | ||
KeyA: new Move("Uv'"), | ||
KeyP: new Move("BR'"), | ||
KeyQ: new Move("BL"), | ||
|
||
KeyZ: new Move("BL'"), | ||
KeyB: new Move("T"), | ||
Period: new Move("BR"), | ||
|
||
Backquote: new Pause(), | ||
}; |
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