-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
752e92a
commit 01df885
Showing
5 changed files
with
104 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# `useKeyPress` | ||
|
||
React UI sensor hook that detects when the user is pressing a specific | ||
key on their keyboard. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import { useKeyPress } from "react-use"; | ||
|
||
const Demo = () => { | ||
const hasPressedQ = useKeyPress("q"); | ||
const hasPressedW = useKeyPress("w"); | ||
const hasPressedE = useKeyPress("e"); | ||
const hasPressedR = useKeyPress("r"); | ||
const hasPressedT = useKeyPress("t"); | ||
const hasPressedY = useKeyPress("y"); | ||
return ( | ||
<div> | ||
Try pressing one of these: <code>Q W E R T Y</code> | ||
<div> | ||
{hasPressedQ && "Q"} | ||
{hasPressedW && "W"} | ||
{hasPressedE && "E"} | ||
{hasPressedR && "R"} | ||
{hasPressedT && "T"} | ||
{hasPressedY && "Y"} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
## Reference | ||
|
||
```js | ||
const hasPressed = useKeyPress('key'); | ||
``` |
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,30 @@ | ||
import { storiesOf } from "@storybook/react"; | ||
import * as React from "react"; | ||
import { useKeyPress } from ".."; | ||
import ShowDocs from "../util/ShowDocs"; | ||
|
||
const Demo = () => { | ||
const hasPressedQ = useKeyPress("q"); | ||
const hasPressedW = useKeyPress("w"); | ||
const hasPressedE = useKeyPress("e"); | ||
const hasPressedR = useKeyPress("r"); | ||
const hasPressedT = useKeyPress("t"); | ||
const hasPressedY = useKeyPress("y"); | ||
return ( | ||
<div> | ||
Try pressing one of these: <code>Q W E R T Y</code> | ||
<div> | ||
{hasPressedQ && "Q"} | ||
{hasPressedW && "W"} | ||
{hasPressedE && "E"} | ||
{hasPressedR && "R"} | ||
{hasPressedT && "T"} | ||
{hasPressedY && "Y"} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf("Sensors/useKeyPress", module) | ||
.add("Docs", () => <ShowDocs md={require("../../docs/useKeyPress.md")} />) | ||
.add("Demo", () => <Demo />); |
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,32 @@ | ||
import * as React from 'react'; | ||
|
||
const {useState, useEffect} = React; | ||
|
||
// kudos: https://usehooks.com | ||
const useKeyPress = (targetKey: string) => { | ||
const [state, setState] = useState(false); | ||
|
||
const downHandler = ({key}: KeyboardEvent) => { | ||
if (key === targetKey) { | ||
setState(true); | ||
} | ||
} | ||
const upHandler = ({key}: KeyboardEvent) => { | ||
if (key === targetKey) { | ||
setState(false); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
window.addEventListener('keydown', downHandler); | ||
window.addEventListener('keyup', upHandler); | ||
return () => { | ||
window.removeEventListener('keydown', downHandler); | ||
window.removeEventListener('keyup', upHandler); | ||
}; | ||
}, []); | ||
|
||
return state; | ||
} | ||
|
||
export default useKeyPress; |
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