diff --git a/docs/general/resources.md b/docs/general/resources.md
index e0b186659..4bc295b78 100644
--- a/docs/general/resources.md
+++ b/docs/general/resources.md
@@ -6,7 +6,8 @@ A few resources that are helpful for building with Slate.
## Libraries
-- [`react-broadcast`] works well when you need to have your custom node components re-render based on state that lives outside the `document`. It's the same pattern that `react-router` uses to update `` components.
+- [`is-hotkey`](https://github.com/ianstormtaylor/is-hotkey) is a simple way to check whether an `onKeyDown` handler should fire for a given hotkey, handling cross-platform concerns like cmd vs. ctrl keys for you automatically.
+- [`react-broadcast`](https://github.com/ReactTraining/react-broadcast) works well when you need to have your custom node components re-render based on state that lives outside the `document`. It's the same pattern that `react-router` uses to update `` components.
## Tooling
diff --git a/docs/reference/slate-react/plugins.md b/docs/reference/slate-react/plugins.md
index 6b7917296..8872db2c6 100644
--- a/docs/reference/slate-react/plugins.md
+++ b/docs/reference/slate-react/plugins.md
@@ -115,30 +115,6 @@ If no other plugin handles this event, it will be handled by the [Core plugin](.
This handler is called when any key is pressed in the `contenteditable` element, before any action is taken.
-The `data` object contains the `key` which is a string name of the key that was pressed, as well as it's `code`. It also contains a series of helpful utility properties for determining hotkey logic. For example, `isCtrl` is true if the `control` key was pressed before.
-
-```js
-{
- key: String,
- code: Number,
- isAlt: Boolean,
- isCmd: Boolean,
- isCtrl: Boolean,
- isLine: Boolean,
- isMeta: Boolean,
- isMod: Boolean,
- isModAlt: Boolean,
- isShift: Boolean,
- isWord: Boolean
-}
-```
-
-The `isMod` boolean is `true` if the `control` key was pressed on Windows or the `command` key was pressed on Mac _without_ the `alt/option` key also being pressed. This is a convenience for adding hotkeys like `command+b`.
-
-The `isModAlt` boolean is `true` if the `control` key was pressed on Windows or the `command` key was pressed on Mac _and_ the `alt/option` key was also being pressed. This is a convenience for secondary hotkeys like `command+option+1`.
-
-The `isLine` and `isWord` booleans represent whether the "line modifier" or "word modifier" hotkeys are pressed when deleting or moving the cursor. For example, on a Mac `option + right` moves the cursor to the right one word at a time.
-
Make sure to `event.preventDefault()` if you do not want the default insertion behavior to occur! If no other plugin handles this event, it will be handled by the [Core plugin](./core.md).
### `onKeyUp`
@@ -146,8 +122,6 @@ Make sure to `event.preventDefault()` if you do not want the default insertion b
This handler is called when any key is released in the `contenteditable` element.
-The `data` object contains the same information as the `data` object of `onKeyDown`.
-
### `onPaste`
`Function onPaste(event: Event, data: Object, change: Change, editor: Editor) => Change || Void`
diff --git a/docs/walkthroughs/adding-event-handlers.md b/docs/walkthroughs/adding-event-handlers.md
index 7c9662bcf..f1e89cea1 100644
--- a/docs/walkthroughs/adding-event-handlers.md
+++ b/docs/walkthroughs/adding-event-handlers.md
@@ -49,9 +49,9 @@ class App extends React.Component {
this.setState({ state })
}
- // Define a new handler which prints the key code that was pressed.
+ // Define a new handler which prints the key that was pressed.
onKeyDown = (event, data, change) => {
- console.log(event.which)
+ console.log(event.key)
}
render() {
@@ -85,8 +85,8 @@ class App extends React.Component {
}
onKeyDown = (event, data, change) => {
- // Return with no changes if it's not the "7" key with shift pressed.
- if (event.which != 55 || !event.shiftKey) return
+ // Return with no changes if it's not the "&" key.
+ if (event.key != '&') return
// Prevent the ampersand character from being inserted.
event.preventDefault()
diff --git a/docs/walkthroughs/applying-custom-formatting.md b/docs/walkthroughs/applying-custom-formatting.md
index f01ee9331..c70dc5255 100644
--- a/docs/walkthroughs/applying-custom-formatting.md
+++ b/docs/walkthroughs/applying-custom-formatting.md
@@ -28,7 +28,7 @@ class App extends React.Component {
}
onKeyDown = (event, data, change) => {
- if (event.which != 67 || !event.metaKey || !event.altKey) return
+ if (event.key != '`' || !event.metaKey) return
event.preventDefault()
const isCode = change.state.blocks.some(block => block.type == 'code')
@@ -72,16 +72,15 @@ class App extends React.Component {
if (!event.metaKey) return
// Decide what to do based on the key code...
- switch (event.which) {
+ switch (event.key) {
// When "B" is pressed, add a "bold" mark to the text.
- case 66: {
+ case 'b': {
event.preventDefault()
change.addMark('bold')
return true
}
// When "`" is pressed, keep our existing code block logic.
- case 67: {
- if (!event.altKey) return
+ case '`': {
const isCode = change.state.blocks.some(block => block.type == 'code')
event.preventDefault()
change.setBlock(isCode ? 'paragraph' : 'code')
@@ -148,14 +147,13 @@ class App extends React.Component {
onKeyDown = (event, data, change) => {
if (!event.metaKey) return
- switch (event.which) {
- case 66: {
+ switch (event.key) {
+ case 'b': {
event.preventDefault()
change.toggleMark('bold')
return true
}
- case 67: {
- if (!event.altKey) return
+ case '`': {
const isCode = change.state.blocks.some(block => block.type == 'code')
event.preventDefault()
state.setBlock(isCode ? 'paragraph' : 'code')
diff --git a/docs/walkthroughs/defining-custom-block-nodes.md b/docs/walkthroughs/defining-custom-block-nodes.md
index ed3fbbe1c..a1cd9aacd 100644
--- a/docs/walkthroughs/defining-custom-block-nodes.md
+++ b/docs/walkthroughs/defining-custom-block-nodes.md
@@ -23,10 +23,8 @@ class App extends React.Component {
}
onKeyDown = (event, data, change) => {
- if (event.which != 55 || !event.shiftKey) return
-
+ if (event.key != '&') return
event.preventDefault()
-
change.insertText('and');
return true
}
@@ -87,10 +85,8 @@ class App extends React.Component {
}
onKeyDown = (event, data, change) => {
- if (event.which != 55 || !event.shiftKey) return
-
+ if (event.key != '&') return
event.preventDefault()
-
change.insertText('and')
return true
}
@@ -110,7 +106,7 @@ class App extends React.Component {
}
```
-Okay, but now we'll need a way for the user to actually turn a block into a code block. So let's change our `onKeyDown` function to add a `⌘-Alt-C` shortcut that does just that:
+Okay, but now we'll need a way for the user to actually turn a block into a code block. So let's change our `onKeyDown` function to add a `⌘-\`` shortcut that does just that:
```js
function CodeNode(props) {
@@ -134,7 +130,7 @@ class App extends React.Component {
onKeyDown = (event, data, change) => {
// Return with no changes if it's not the "`" key with cmd/ctrl pressed.
- if (event.which != 67 || !event.metaKey || !event.altKey) return
+ if (event.key != '`' || !event.metaKey) return
// Prevent the "`" from being inserted by default.
event.preventDefault()
@@ -158,9 +154,9 @@ class App extends React.Component {
}
```
-Now, if you press `⌘-Alt-C`, the block your cursor is in should turn into a code block! Magic!
+Now, if you press `⌘-\`` the block your cursor is in should turn into a code block! Magic!
-But we forgot one thing. When you hit `⌘-Alt-C` again, it should change the code block back into a paragraph. To do that, we'll need to add a bit of logic to change the type we set based on whether any of the currently selected blocks are already a code block:
+But we forgot one thing. When you hit `⌘-\`` again, it should change the code block back into a paragraph. To do that, we'll need to add a bit of logic to change the type we set based on whether any of the currently selected blocks are already a code block:
```js
function CodeNode(props) {
@@ -183,7 +179,7 @@ class App extends React.Component {
}
onKeyDown = (event, data, change) => {
- if (event.which != 67 || !event.metaKey || !event.altKey) return
+ if (event.key != '`' || !event.metaKey) return
event.preventDefault()
@@ -209,7 +205,7 @@ class App extends React.Component {
}
```
-And there you have it! If you press `⌘-Alt-C` while inside a code block, it should turn back into a paragraph!
+And there you have it! If you press `⌘-\`` while inside a code block, it should turn back into a paragraph!