Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(keyboard): parse keyboard input without nesting (#793)
- Loading branch information
1 parent
86860cc
commit fafa677
Showing
7 changed files
with
110 additions
and
121 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
73 changes: 36 additions & 37 deletions
73
src/keyboard/keyboardImplementation.ts → src/keyboard/keyboardAction.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
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,51 @@ | ||
import {readNextDescriptor} from '../utils' | ||
import {keyboardKey} from './types' | ||
|
||
/** | ||
* Parse key defintions per `keyboardMap` | ||
* | ||
* Keys can be referenced by `{key}` or `{special}` as well as physical locations per `[code]`. | ||
* Everything else will be interpreted as a typed character - e.g. `a`. | ||
* Brackets `{` and `[` can be escaped by doubling - e.g. `foo[[bar` translates to `foo[bar`. | ||
* Keeping the key pressed can be written as `{key>}`. | ||
* When keeping the key pressed you can choose how long (how many keydown and keypress) the key is pressed `{key>3}`. | ||
* You can then release the key per `{key>3/}` or keep it pressed and continue with the next key. | ||
*/ | ||
export function parseKeyDef(keyboardMap: keyboardKey[], text: string) { | ||
const defs: Array<{ | ||
keyDef: keyboardKey | ||
releasePrevious: boolean | ||
releaseSelf: boolean | ||
repeat: number | ||
}> = [] | ||
|
||
do { | ||
const { | ||
type, | ||
descriptor, | ||
consumedLength, | ||
releasePrevious, | ||
releaseSelf = true, | ||
repeat, | ||
} = readNextDescriptor(text) | ||
|
||
const keyDef = keyboardMap.find(def => { | ||
if (type === '[') { | ||
return def.code?.toLowerCase() === descriptor.toLowerCase() | ||
} else if (type === '{') { | ||
return def.key?.toLowerCase() === descriptor.toLowerCase() | ||
} | ||
return def.key === descriptor | ||
}) ?? { | ||
key: 'Unknown', | ||
code: 'Unknown', | ||
[type === '[' ? 'code' : 'key']: descriptor, | ||
} | ||
|
||
defs.push({keyDef, releasePrevious, releaseSelf, repeat}) | ||
|
||
text = text.slice(consumedLength) | ||
} while (text) | ||
|
||
return defs | ||
} |
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
File renamed without changes.
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