parse-hotkey.ts 874 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const KEY_MAP: Record<string, string> = {
  2. '"': "'",
  3. ":": ";",
  4. "?": "/",
  5. ">": ".",
  6. "<": ",",
  7. "{": "[",
  8. "}": "]",
  9. "|": "\\",
  10. "!": "1",
  11. "@": "2",
  12. "#": "3",
  13. $: "4",
  14. "%": "5",
  15. "^": "6",
  16. "&": "7",
  17. "*": "8",
  18. "(": "9",
  19. ")": "0",
  20. "~": "`",
  21. };
  22. export const parseHotkey = (key: string) => {
  23. let temp = key.toUpperCase();
  24. if (temp.startsWith("ARROW")) {
  25. temp = temp.slice(5);
  26. } else if (temp.startsWith("DIGIT")) {
  27. temp = temp.slice(5);
  28. } else if (temp.startsWith("KEY")) {
  29. temp = temp.slice(3);
  30. } else if (temp.endsWith("LEFT")) {
  31. temp = temp.slice(0, -4);
  32. } else if (temp.endsWith("RIGHT")) {
  33. temp = temp.slice(0, -5);
  34. }
  35. switch (temp) {
  36. case "CONTROL":
  37. return "CTRL";
  38. case "META":
  39. return "CMD";
  40. case " ":
  41. return "SPACE";
  42. default:
  43. return KEY_MAP[temp] || temp;
  44. }
  45. };