You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

useKeyboardEvents.ts 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { useEffect } from "react"
  2. import state from "state"
  3. import { getKeyboardEventInfo, isDarwin, metaKey } from "utils/utils"
  4. export default function useKeyboardEvents() {
  5. useEffect(() => {
  6. function handleKeyDown(e: KeyboardEvent) {
  7. if (e.key === "Escape") {
  8. state.send("CANCELLED")
  9. } else if (e.key === "z" && metaKey(e)) {
  10. if (e.shiftKey) {
  11. state.send("REDO")
  12. } else {
  13. state.send("UNDO")
  14. }
  15. }
  16. if (e.key === "Shift") {
  17. state.send("PRESSED_SHIFT_KEY", getKeyboardEventInfo(e))
  18. }
  19. if (e.key === "Alt") {
  20. state.send("PRESSED_ALT_KEY", getKeyboardEventInfo(e))
  21. }
  22. if (e.key === "Backspace" && !(metaKey(e) || e.shiftKey || e.altKey)) {
  23. state.send("DELETED", getKeyboardEventInfo(e))
  24. }
  25. if (e.key === "s" && metaKey(e)) {
  26. e.preventDefault()
  27. state.send("SAVED")
  28. }
  29. if (e.key === "a" && metaKey(e)) {
  30. e.preventDefault()
  31. state.send("SELECTED_ALL")
  32. }
  33. if (e.key === "v" && !(metaKey(e) || e.shiftKey || e.altKey)) {
  34. state.send("SELECTED_SELECT_TOOL", getKeyboardEventInfo(e))
  35. }
  36. if (e.key === "d" && !(metaKey(e) || e.shiftKey || e.altKey)) {
  37. state.send("SELECTED_DOT_TOOL", getKeyboardEventInfo(e))
  38. }
  39. if (e.key === "c" && !(metaKey(e) || e.shiftKey || e.altKey)) {
  40. state.send("SELECTED_CIRCLE_TOOL", getKeyboardEventInfo(e))
  41. }
  42. if (e.key === "i" && !(metaKey(e) || e.shiftKey || e.altKey)) {
  43. state.send("SELECTED_ELLIPSE_TOOL", getKeyboardEventInfo(e))
  44. }
  45. if (e.key === "l" && !(metaKey(e) || e.shiftKey || e.altKey)) {
  46. state.send("SELECTED_LINE_TOOL", getKeyboardEventInfo(e))
  47. }
  48. if (e.key === "y" && !(metaKey(e) || e.shiftKey || e.altKey)) {
  49. state.send("SELECTED_RAY_TOOL", getKeyboardEventInfo(e))
  50. }
  51. if (e.key === "p" && !(metaKey(e) || e.shiftKey || e.altKey)) {
  52. state.send("SELECTED_POLYLINE_TOOL", getKeyboardEventInfo(e))
  53. }
  54. if (e.key === "r" && !(metaKey(e) || e.shiftKey || e.altKey)) {
  55. state.send("SELECTED_RECTANGLE_TOOL", getKeyboardEventInfo(e))
  56. }
  57. }
  58. function handleKeyUp(e: KeyboardEvent) {
  59. if (e.key === "Shift") {
  60. state.send("RELEASED_SHIFT_KEY", getKeyboardEventInfo(e))
  61. }
  62. if (e.key === "Alt") {
  63. state.send("RELEASED_ALT_KEY", getKeyboardEventInfo(e))
  64. }
  65. state.send("RELEASED_KEY", getKeyboardEventInfo(e))
  66. }
  67. document.body.addEventListener("keydown", handleKeyDown)
  68. document.body.addEventListener("keyup", handleKeyUp)
  69. return () => {
  70. document.body.removeEventListener("keydown", handleKeyDown)
  71. document.body.removeEventListener("keyup", handleKeyUp)
  72. }
  73. }, [])
  74. }