-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: tweak doc comments and add content to README
- Loading branch information
1 parent
1aa9ce6
commit 456a555
Showing
7 changed files
with
96 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,52 @@ | ||
# @laserware/keytar | ||
|
||
Handle keyboard and mouse events in the browser. | ||
Check out the [documentation](https://laserware.github.io/keytar/) for the API. | ||
|
||
## Overview | ||
|
||
This library uses bitflags to check for mouse and keyboard event conditions. | ||
If you're interested in how I implemented it and the reasoning behind it, check out [this blog post I wrote](https://mikerourke.dev/blog/bitwisdom-keyboard-shortcuts/). | ||
|
||
Here's what this looks like in practice: | ||
|
||
```ts | ||
import { handleChords, Key, Modifier } from "@laserware/keytar"; | ||
|
||
function handleKeyDown(event: KeyboardEvent): void { | ||
const isShiftDown = (event: KeyboardEvent): boolean => { | ||
return event.shiftKey; | ||
}; | ||
|
||
const isCtrlDown = (event: KeyboardEvent): boolean => { | ||
return event.ctrlKey; | ||
}; | ||
|
||
handleChords(event, (handler) => { | ||
handler | ||
.on(Key.LetterE, () => { | ||
// This is fired _only_ when the letter "E" is pressed. | ||
// If Shift + E is pressed, this condition won't be hit. | ||
}) | ||
.on(Modifier.Ctrl | Key.LetterK, () => { | ||
// This is fired _only_ when Ctrl + K is pressed. | ||
}) | ||
.on([Modifier.Ctrl | Key.LetterP, Modifier.Ctrl | Key.LetterQ], () => { | ||
// This is fired _only_ when Ctrl + P _or_ Ctrl + Q is pressed. | ||
}) | ||
// Pass a function reference that returns a boolean | ||
// (first arg is the Event): | ||
.when(isShiftDown, () => { | ||
// This is fired whenever the Shift key is pressed. | ||
}) | ||
// Call the function directly and return a boolean: | ||
.when(isCtrlDown(event), () => { | ||
// This is fired whenever the Ctrl key is pressed. | ||
}) | ||
// Or just specify a boolean condition: | ||
.when(event.altKey, () => { | ||
// This is fired whenever the Alt key is pressed. | ||
}) | ||
}); | ||
} | ||
``` |
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
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