This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 94f3db6bff4 sim/keyboard: Translate X11 key codes to NuttX codec
94f3db6bff4 is described below

commit 94f3db6bff4cd929c8940e78c766daf6fd8c5c18
Author: Matteo Golin <[email protected]>
AuthorDate: Sun May 24 13:29:58 2026 -0400

    sim/keyboard: Translate X11 key codes to NuttX codec
    
    The previous implementation just passed X11 key codes to the NuttX
    keyboard driver, which only really worked correctly for the letter keys.
    Now, special keys are translated into the NuttX codec (like Enter, arrow
    keys, etc.) so that they can be used properly by NuttX applications
    relying on keyboard input.
    
    Signed-off-by: Matteo Golin <[email protected]>
---
 arch/sim/src/sim/sim_keyboard.c | 151 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 150 insertions(+), 1 deletion(-)

diff --git a/arch/sim/src/sim/sim_keyboard.c b/arch/sim/src/sim/sim_keyboard.c
index d56575ae76c..d08556ae8e0 100644
--- a/arch/sim/src/sim/sim_keyboard.c
+++ b/arch/sim/src/sim/sim_keyboard.c
@@ -32,6 +32,7 @@
 #include <string.h>
 
 #include <nuttx/input/keyboard.h>
+#include <nuttx/input/kbd_codec.h>
 
 #include "sim_internal.h"
 
@@ -61,6 +62,152 @@ struct sim_dev_s
 
 static struct sim_dev_s g_simkeyboard;
 
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: translate_x11_key
+ *
+ * Description:
+ *   Translate X11 key codes into the NuttX keyboard codec so that
+ *   applications can actually rely on the output of the keyboard driver.
+ *   Some X11 key codes do not need translation.
+ *
+ * Returns: The translated key code.
+ ****************************************************************************/
+
+static uint32_t translate_x11_key(uint32_t code)
+{
+  switch (code)
+    {
+    case XK_Delete:
+      return KEYCODE_FWDDEL;
+    case XK_BackSpace:
+      return KEYCODE_BACKDEL;
+
+      /* Cursor movement */
+
+    case XK_Home:
+      return KEYCODE_HOME;
+    case XK_End:
+      return KEYCODE_END;
+    case XK_Left:
+      return KEYCODE_LEFT;
+    case XK_Right:
+      return KEYCODE_RIGHT;
+    case XK_Up:
+      return KEYCODE_UP;
+    case XK_Down:
+      return KEYCODE_DOWN;
+    case XK_Page_Up:
+      return KEYCODE_PAGEUP;
+    case XK_Page_Down:
+      return KEYCODE_PAGEDOWN;
+
+      /* Edit commands */
+
+    case XK_Insert:
+      return KEYCODE_INSERT;
+    case XK_Undo:
+      return KEYCODE_UNDO;
+    case XK_Redo:
+      return KEYCODE_REDO;
+    case XK_Find:
+      return KEYCODE_FIND;
+
+      /* Selection codes */
+
+    case XK_Return:
+    case XK_KP_Enter:
+      return KEYCODE_ENTER;
+    case XK_Select:
+      return KEYCODE_SELECT;
+    case XK_Execute:
+      return KEYCODE_EXECUTE;
+
+      /* Keyboard modes */
+
+    case XK_Caps_Lock:
+      return KEYCODE_CAPSLOCK;
+    case XK_Scroll_Lock:
+      return KEYCODE_SCROLLLOCK;
+    case XK_Num_Lock:
+      return KEYCODE_NUMLOCK;
+
+      /* Misc control codes */
+
+    case XK_Help:
+      return KEYCODE_HELP;
+    case XK_Menu:
+      return KEYCODE_MENU;
+    case XK_Pause:
+      return KEYCODE_PAUSE;
+    case XK_Break:
+      return KEYCODE_BREAK;
+    case XK_Cancel:
+      return KEYCODE_CANCEL;
+    case XK_Print:
+      return KEYCODE_PRTSCRN;
+    case XK_Sys_Req:
+      return KEYCODE_SYSREQ;
+
+      /* Context-specific function keys */
+
+    case XK_F1:
+      return KEYCODE_F1;
+    case XK_F2:
+      return KEYCODE_F2;
+    case XK_F3:
+      return KEYCODE_F3;
+    case XK_F4:
+      return KEYCODE_F4;
+    case XK_F5:
+      return KEYCODE_F5;
+    case XK_F6:
+      return KEYCODE_F6;
+    case XK_F7:
+      return KEYCODE_F7;
+    case XK_F8:
+      return KEYCODE_F8;
+    case XK_F9:
+      return KEYCODE_F9;
+    case XK_F10:
+      return KEYCODE_F10;
+    case XK_F11:
+      return KEYCODE_F11;
+    case XK_F12:
+      return KEYCODE_F12;
+    case XK_F13:
+      return KEYCODE_F13;
+    case XK_F14:
+      return KEYCODE_F14;
+    case XK_F15:
+      return KEYCODE_F15;
+    case XK_F16:
+      return KEYCODE_F16;
+    case XK_F17:
+      return KEYCODE_F17;
+    case XK_F18:
+      return KEYCODE_F18;
+    case XK_F19:
+      return KEYCODE_F19;
+    case XK_F20:
+      return KEYCODE_F20;
+    case XK_F21:
+      return KEYCODE_F21;
+    case XK_F22:
+      return KEYCODE_F22;
+    case XK_F23:
+      return KEYCODE_F23;
+    case XK_F24:
+      return KEYCODE_F24;
+
+    default:
+      return code;
+    }
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -98,6 +245,7 @@ int sim_kbd_initialize(void)
 
 void sim_kbdevent(uint32_t key, bool is_press)
 {
+  uint32_t trans_key;
   struct sim_dev_s *priv = (struct sim_dev_s *) &g_simkeyboard;
   uint32_t types[2] =
     {
@@ -109,9 +257,10 @@ void sim_kbdevent(uint32_t key, bool is_press)
       return;
     }
 
+  trans_key = translate_x11_key(key);
   iinfo("key=%04x\n", key);
 
   /* Report data changes */
 
-  keyboard_event(&priv->lower, key, types[is_press]);
+  keyboard_event(&priv->lower, trans_key, types[is_press]);
 }

Reply via email to