|
947 | 947 | }); |
948 | 948 |
|
949 | 949 | // Arrow key navigation state |
950 | | - let lastKeyPress = null; |
951 | | - let lastKeyTime = 0; |
952 | 950 | let lastActionTime = 0; |
953 | | - const DOUBLE_TAP_WINDOW = 300; // ms to detect double tap |
954 | 951 | const ACTION_COOLDOWN = 1000; // ms between actions |
955 | 952 |
|
956 | 953 | const sendDirection = (direction) => { |
|
1007 | 1004 | } |
1008 | 1005 | }); |
1009 | 1006 |
|
1010 | | - // Global arrow key listener for directional movement (when input not focused) |
| 1007 | + // Global key listener for directional movement (when input not focused) |
1011 | 1008 | document.addEventListener("keydown", (e) => { |
1012 | | - // Only handle arrows when input is not focused |
| 1009 | + // Only handle navigation when input is not focused |
1013 | 1010 | if (document.activeElement === input) return; |
1014 | | - if (!e.key.startsWith("Arrow")) return; |
1015 | | - |
1016 | | - const now = Date.now(); |
1017 | | - const timeSinceLastKey = now - lastKeyTime; |
1018 | 1011 |
|
1019 | 1012 | if (e.key === "ArrowUp") { |
1020 | 1013 | e.preventDefault(); |
1021 | | - // Check for double-tap |
1022 | | - if (lastKeyPress === "ArrowUp" && timeSinceLastKey < DOUBLE_TAP_WINDOW) { |
1023 | | - sendDirection("n"); // double-tap = up/north shorthand |
1024 | | - lastKeyPress = null; // Reset to prevent triple-taps |
1025 | | - } else { |
1026 | | - sendDirection("north"); |
1027 | | - lastKeyPress = "ArrowUp"; |
1028 | | - } |
1029 | | - lastKeyTime = now; |
| 1014 | + sendDirection("north"); |
1030 | 1015 | } else if (e.key === "ArrowDown") { |
1031 | 1016 | e.preventDefault(); |
1032 | | - // Check for double-tap |
1033 | | - if (lastKeyPress === "ArrowDown" && timeSinceLastKey < DOUBLE_TAP_WINDOW) { |
1034 | | - sendDirection("s"); // double-tap = down/south shorthand |
1035 | | - lastKeyPress = null; // Reset to prevent triple-taps |
1036 | | - } else { |
1037 | | - sendDirection("south"); |
1038 | | - lastKeyPress = "ArrowDown"; |
1039 | | - } |
1040 | | - lastKeyTime = now; |
| 1017 | + sendDirection("south"); |
1041 | 1018 | } else if (e.key === "ArrowLeft") { |
1042 | 1019 | e.preventDefault(); |
1043 | 1020 | sendDirection("west"); |
1044 | | - lastKeyPress = "ArrowLeft"; |
1045 | | - lastKeyTime = now; |
1046 | 1021 | } else if (e.key === "ArrowRight") { |
1047 | 1022 | e.preventDefault(); |
1048 | 1023 | sendDirection("east"); |
1049 | | - lastKeyPress = "ArrowRight"; |
1050 | | - lastKeyTime = now; |
| 1024 | + } else if (e.key === "u" || e.key === "U") { |
| 1025 | + e.preventDefault(); |
| 1026 | + sendDirection("u"); |
| 1027 | + } else if (e.key === "d" || e.key === "D") { |
| 1028 | + e.preventDefault(); |
| 1029 | + sendDirection("d"); |
1051 | 1030 | } |
1052 | 1031 | }); |
1053 | 1032 |
|
|
0 commit comments