]> git.earman.xyz Git - sensor-watch.git/commitdiff
Simulator: Add keyboard arrows as buttons shortcuts
authorHugo Chargois <hugo.chargois@free.fr>
Sat, 18 Nov 2023 01:22:03 +0000 (02:22 +0100)
committerHugo Chargois <hugo.chargois@free.fr>
Sun, 19 Nov 2023 18:50:39 +0000 (19:50 +0100)
watch-library/simulator/watch/watch_extint.c

index 88e4f30ad1b3af427f4e1d71c3f4acb702817b50..b5894b9579dcae71f45673c527eba8cd28366b9d 100644 (file)
@@ -22,6 +22,8 @@
  * SOFTWARE.
  */
 
+#include <string.h>
+
 #include "watch_extint.h"
 #include "watch_main_loop.h"
 
@@ -47,25 +49,45 @@ static EM_BOOL watch_invoke_interrupt_callback(const uint8_t button_id, watch_in
 static EM_BOOL watch_invoke_key_callback(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData) {
     if (debug_console_focused || keyEvent->repeat) return EM_FALSE;
 
-    const char *key = keyEvent->key;
-    if (key[1] != 0) return EM_FALSE;
-
     uint8_t button_id;
-    switch (key[0]) {
-        case 'A':
-        case 'a':
-            button_id = BTN_ID_ALARM;
-            break;
-        case 'L':
-        case 'l':
-            button_id = BTN_ID_LIGHT;
-            break;
-        case 'M':
-        case 'm':
-            button_id = BTN_ID_MODE;
-            break;
-        default:
-            return EM_FALSE;
+    const char *key = keyEvent->key;
+    if (key[1] == 0) {
+        // event is from a plain letter key
+        switch (key[0]) {
+            case 'A':
+            case 'a':
+                button_id = BTN_ID_ALARM;
+                break;
+            case 'L':
+            case 'l':
+                button_id = BTN_ID_LIGHT;
+                break;
+            case 'M':
+            case 'm':
+                button_id = BTN_ID_MODE;
+                break;
+            default:
+                return EM_FALSE;
+        }
+    } else if (strncmp(key, "Arrow", 5) == 0) {
+        // event is from one of the arrow keys
+        switch(key[5]) {
+            case 'U': // ArrowUp
+                button_id = BTN_ID_LIGHT;
+                break;
+            case 'D': // ArrowDown
+            case 'L': // ArrowLeft
+                button_id = BTN_ID_MODE;
+                break;
+            case 'R': // ArrowRight
+                button_id = BTN_ID_ALARM;
+                break;
+            default:
+                return EM_FALSE;
+        }
+    } else {
+        // another kind of key
+        return EM_FALSE;
     }
 
     watch_interrupt_trigger trigger = eventType == EMSCRIPTEN_EVENT_KEYDOWN ? INTERRUPT_TRIGGER_RISING : INTERRUPT_TRIGGER_FALLING;