keys.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. export const isDarwin = /Mac|iPod|iPhone|iPad/.test(window.navigator.platform);
  2. export const isWindows = /^Win/.test(window.navigator.platform);
  3. export const CODES = {
  4. EQUAL: "Equal",
  5. MINUS: "Minus",
  6. NUM_ADD: "NumpadAdd",
  7. NUM_SUBTRACT: "NumpadSubtract",
  8. NUM_ZERO: "Numpad0",
  9. BRACKET_RIGHT: "BracketRight",
  10. BRACKET_LEFT: "BracketLeft",
  11. ONE: "Digit1",
  12. TWO: "Digit2",
  13. NINE: "Digit9",
  14. QUOTE: "Quote",
  15. ZERO: "Digit0",
  16. C: "KeyC",
  17. G: "KeyG",
  18. F: "KeyF",
  19. H: "KeyH",
  20. V: "KeyV",
  21. X: "KeyX",
  22. Z: "KeyZ",
  23. R: "KeyR",
  24. } as const;
  25. export const KEYS = {
  26. ARROW_DOWN: "ArrowDown",
  27. ARROW_LEFT: "ArrowLeft",
  28. ARROW_RIGHT: "ArrowRight",
  29. ARROW_UP: "ArrowUp",
  30. BACKSPACE: "Backspace",
  31. ALT: "Alt",
  32. CTRL_OR_CMD: isDarwin ? "metaKey" : "ctrlKey",
  33. DELETE: "Delete",
  34. ENTER: "Enter",
  35. ESCAPE: "Escape",
  36. QUESTION_MARK: "?",
  37. SPACE: " ",
  38. TAB: "Tab",
  39. A: "a",
  40. D: "d",
  41. E: "e",
  42. L: "l",
  43. O: "o",
  44. P: "p",
  45. Q: "q",
  46. R: "r",
  47. S: "s",
  48. T: "t",
  49. V: "v",
  50. X: "x",
  51. Y: "y",
  52. Z: "z",
  53. } as const;
  54. export type Key = keyof typeof KEYS;
  55. export const isArrowKey = (key: string) =>
  56. key === KEYS.ARROW_LEFT ||
  57. key === KEYS.ARROW_RIGHT ||
  58. key === KEYS.ARROW_DOWN ||
  59. key === KEYS.ARROW_UP;
  60. export const getResizeCenterPointKey = (event: MouseEvent | KeyboardEvent) =>
  61. event.altKey;
  62. export const getResizeWithSidesSameLengthKey = (
  63. event: MouseEvent | KeyboardEvent,
  64. ) => event.shiftKey;
  65. export const getRotateWithDiscreteAngleKey = (
  66. event: MouseEvent | KeyboardEvent,
  67. ) => event.shiftKey;