-
-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This commit adds support to spectator.keyboard.pressKey() for triggering key events with modifier keys. Example, spectator.keyboard.pressKey('ctrl.shift.a').
- Loading branch information
1 parent
20168ef
commit 34769bd
Showing
7 changed files
with
118 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { isNumber, isString } from '../types'; | ||
|
||
export interface ModifierKeys { | ||
alt?: boolean; | ||
control?: boolean; | ||
shift?: boolean; | ||
meta?: boolean; | ||
} | ||
|
||
export interface KeyOptions { | ||
key: string | false; | ||
keyCode: number | false; | ||
modifiers: ModifierKeys; | ||
} | ||
|
||
export const parseKeyOptions = (keyOrKeyCode: string | number): KeyOptions => { | ||
if (isNumber(keyOrKeyCode) && keyOrKeyCode) { | ||
return { key: false, keyCode: keyOrKeyCode, modifiers: {} }; | ||
} | ||
|
||
if (isString(keyOrKeyCode) && keyOrKeyCode) { | ||
return parseKey(keyOrKeyCode as string); | ||
} | ||
|
||
throw new Error('keyboard.pressKey() requires a valid key or keyCode'); | ||
}; | ||
|
||
const parseKey = (keyStr: string): KeyOptions => { | ||
if (keyStr.indexOf('.') < 0) { | ||
return { key: keyStr, keyCode: false, modifiers: {} }; | ||
} | ||
|
||
const keyParts = keyStr.split('.'); | ||
const key = keyParts.pop() as string; | ||
const modifiers = keyParts.reduce( | ||
(mods, part) => { | ||
switch (part) { | ||
case 'control': | ||
case 'ctrl': | ||
mods.control = true; | ||
|
||
return mods; | ||
case 'shift': | ||
mods.shift = true; | ||
|
||
return mods; | ||
case 'alt': | ||
mods.alt = true; | ||
|
||
return mods; | ||
case 'meta': | ||
case 'cmd': | ||
case 'win': | ||
mods.meta = true; | ||
|
||
return mods; | ||
default: | ||
throw new Error(`invalid key modifier: ${part}`); | ||
} | ||
}, | ||
{ alt: false, control: false, shift: false, meta: false } | ||
); | ||
|
||
return { key, keyCode: false, modifiers }; | ||
}; |
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,3 +1,4 @@ | ||
<h1>{{ event }}</h1> | ||
<input type="text" (blur)="onBlur()" (focus)="onFocus()" (keyup.a)="onPressA()"> | ||
<input type="text" (blur)="onBlur()" (focus)="onFocus()" (keyup.a)="onPressA()" (keyup.control.a)="onPressCtrlA()" | ||
(keyup.control.shift.a)="onPressCtrlShiftA()"> | ||
|
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