[APORTE] Implement KeyBoard in GLFW3

For help with anything that CEGUI doesn't offer straight out-of-the-box, e.g.:
- Implementation of new features, such as new Core classes, widgets, WindowRenderers, etc. ...
- Modification of any existing features for specific purposes
- Integration of CEGUI in new engines or frameworks and writing of new plugins (Renderer, Parser, ...) or modules

Moderators: CEGUI MVP, CEGUI Team

Lord Fers
Not too shy to talk
Not too shy to talk
Posts: 42
Joined: Sun Aug 30, 2015 21:49

[APORTE] Implement KeyBoard in GLFW3

Postby Lord Fers » Sun Sep 18, 2016 21:09

Sorry for my bad English;
I hope someone will serve you, I have taken the trouble to do it and come to contribute it:

Code: Select all

CEGUI::Key::Scan GlfwToCeguiKey(int glfwKey)
   switch (glfwKey)
      case GLFW_KEY_UNKNOWN: return CEGUI::Key::Unknown;
      case GLFW_KEY_ESCAPE: return CEGUI::Key::Escape;
      case GLFW_KEY_F1: return CEGUI::Key::F1;
      case GLFW_KEY_F2: return CEGUI::Key::F2;
      case GLFW_KEY_F3: return CEGUI::Key::F3;
      case GLFW_KEY_F4: return CEGUI::Key::F4;
      case GLFW_KEY_F5: return CEGUI::Key::F5;
      case GLFW_KEY_F6: return CEGUI::Key::F6;
      case GLFW_KEY_F7: return CEGUI::Key::F7;
      case GLFW_KEY_F8: return CEGUI::Key::F8;
      case GLFW_KEY_F9: return CEGUI::Key::F9;
      case GLFW_KEY_F10: return CEGUI::Key::F10;
      case GLFW_KEY_F11: return CEGUI::Key::F11;
      case GLFW_KEY_F12: return CEGUI::Key::F12;
      case GLFW_KEY_F13: return CEGUI::Key::F13;
      case GLFW_KEY_F14: return CEGUI::Key::F14;
      case GLFW_KEY_F15: return CEGUI::Key::F15;
      case GLFW_KEY_UP: return CEGUI::Key::ArrowUp;
      case GLFW_KEY_DOWN: return CEGUI::Key::ArrowDown;
      case GLFW_KEY_LEFT: return CEGUI::Key::ArrowLeft;
      case GLFW_KEY_RIGHT: return CEGUI::Key::ArrowRight;
      case GLFW_KEY_LEFT_SHIFT: return CEGUI::Key::LeftShift;
      case GLFW_KEY_RIGHT_SHIFT: return CEGUI::Key::RightShift;
      case GLFW_KEY_LEFT_CONTROL: return CEGUI::Key::LeftControl;
      case GLFW_KEY_RIGHT_CONTROL: return CEGUI::Key::RightControl;
      case GLFW_KEY_LEFT_ALT: return CEGUI::Key::LeftAlt;
      case GLFW_KEY_RIGHT_ALT: return CEGUI::Key::RightAlt;
      case GLFW_KEY_TAB: return CEGUI::Key::Tab;
      case GLFW_KEY_ENTER: return CEGUI::Key::Return;
      case GLFW_KEY_BACKSPACE: return CEGUI::Key::Backspace;
      case GLFW_KEY_INSERT: return CEGUI::Key::Insert;
      case GLFW_KEY_DELETE: return CEGUI::Key::Delete;
      case GLFW_KEY_PAGE_UP: return CEGUI::Key::PageUp;
      case GLFW_KEY_PAGE_DOWN: return CEGUI::Key::PageDown;
      case GLFW_KEY_HOME: return CEGUI::Key::Home;
      case GLFW_KEY_END: return CEGUI::Key::End;
      case GLFW_KEY_NUM_LOCK: return CEGUI::Key::NumLock;
      case GLFW_KEY_KP_ENTER: return CEGUI::Key::NumpadEnter;
      default: return CEGUI::Key::Unknown;

CEGUI::Key::Scan GlfwToCeguiKeyScan(int glfwKey) {
   switch (glfwKey) {
      case GLFW_KEY_UNKNOWN: return CEGUI::Key::Unknown;
      case GLFW_KEY_A: return CEGUI::Key::A;
      case GLFW_KEY_B: return CEGUI::Key::B;
      case GLFW_KEY_C: return CEGUI::Key::C;
      case GLFW_KEY_D: return CEGUI::Key::D;
      case GLFW_KEY_E: return CEGUI::Key::E;
      case GLFW_KEY_F: return CEGUI::Key::F;
      case GLFW_KEY_G: return CEGUI::Key::G;
      case GLFW_KEY_H: return CEGUI::Key::H;
      case GLFW_KEY_I: return CEGUI::Key::I;
      case GLFW_KEY_J: return CEGUI::Key::J;
      case GLFW_KEY_K: return CEGUI::Key::K;
      case GLFW_KEY_L: return CEGUI::Key::L;
      case GLFW_KEY_M: return CEGUI::Key::M;
      /* Char Ñ in Spanish-Keyboard */
      case GLFW_KEY_SEMICOLON: return CEGUI::Key::Semicolon;
      case GLFW_KEY_N: return CEGUI::Key::N;
      case GLFW_KEY_O: return CEGUI::Key::O;
      case GLFW_KEY_P: return CEGUI::Key::P;
      case GLFW_KEY_Q: return CEGUI::Key::Q;
      case GLFW_KEY_R: return CEGUI::Key::R;
      case GLFW_KEY_S: return CEGUI::Key::S;
      case GLFW_KEY_T: return CEGUI::Key::T;
      case GLFW_KEY_U: return CEGUI::Key::U;
      case GLFW_KEY_V: return CEGUI::Key::V;
      case GLFW_KEY_W: return CEGUI::Key::W;
      case GLFW_KEY_X: return CEGUI::Key::X;
      case GLFW_KEY_Y: return CEGUI::Key::Y;
      case GLFW_KEY_Z: return CEGUI::Key::Z;
      case GLFW_KEY_0: return CEGUI::Key::Zero;
      case GLFW_KEY_1: return CEGUI::Key::One;
      case GLFW_KEY_2: return CEGUI::Key::Two;
      case GLFW_KEY_3: return CEGUI::Key::Three;
      case GLFW_KEY_4: return CEGUI::Key::Four;
      case GLFW_KEY_5: return CEGUI::Key::Five;
      case GLFW_KEY_6: return CEGUI::Key::Six;
      case GLFW_KEY_7: return CEGUI::Key::Seven;
      case GLFW_KEY_8: return CEGUI::Key::Eight;
      case GLFW_KEY_9: return CEGUI::Key::Nine;
      case GLFW_KEY_KP_0: return CEGUI::Key::Zero;
      case GLFW_KEY_KP_1: return CEGUI::Key::One;
      case GLFW_KEY_KP_2: return CEGUI::Key::Two;
      case GLFW_KEY_KP_3: return CEGUI::Key::Three;
      case GLFW_KEY_KP_4: return CEGUI::Key::Four;
      case GLFW_KEY_KP_5: return CEGUI::Key::Five;
      case GLFW_KEY_KP_6: return CEGUI::Key::Six;
      case GLFW_KEY_KP_7: return CEGUI::Key::Seven;
      case GLFW_KEY_KP_8: return CEGUI::Key::Eight;
      case GLFW_KEY_KP_9: return CEGUI::Key::Nine;
      case GLFW_KEY_KP_ADD: return CEGUI::Key::Add;
      case GLFW_KEY_KP_DECIMAL: return CEGUI::Key::Decimal;
      case GLFW_KEY_KP_DIVIDE: return CEGUI::Key::Divide;
      case GLFW_KEY_KP_EQUAL: return CEGUI::Key::NumpadEquals;
      case GLFW_KEY_KP_MULTIPLY: return CEGUI::Key::Multiply;
      case GLFW_KEY_KP_SUBTRACT: return CEGUI::Key::Subtract;
      case GLFW_KEY_APOSTROPHE: return CEGUI::Key::Apostrophe;
      case GLFW_KEY_COMMA: return CEGUI::Key::Comma;
      case GLFW_KEY_EQUAL: return CEGUI::Key::Equals;
      case GLFW_KEY_PERIOD: return CEGUI::Key::Period;
      case GLFW_KEY_GRAVE_ACCENT: return CEGUI::Key::Grave;
      case GLFW_KEY_LEFT_BRACKET: return CEGUI::Key::LeftBracket;
      case GLFW_KEY_BACKSLASH: return CEGUI::Key::Backslash;
      case GLFW_KEY_RIGHT_BRACKET: return CEGUI::Key::RightBracket;
      case GLFW_KEY_MINUS: return CEGUI::Key::Minus;
      case GLFW_KEY_SLASH: return CEGUI::Key::Slash;
      case GLFW_KEY_SPACE: return CEGUI::Key::Space;
      case GLFW_KEY_WORLD_1: return CEGUI::Key::Unknown;
      case GLFW_KEY_WORLD_2: return CEGUI::Key::Unknown;

      default: return CEGUI::Key::Unknown;

CEGUI::utf32 keycodeToUTF32(unsigned int scanCode)
   CEGUI::utf32 utf = 0;

   BYTE keyboardState[256];
   unsigned char ucBuffer[3];
   static WCHAR deadKey = '\0';

   // Retrieve the keyboard layout in order to perform the necessary convertions
   HKL hklKeyboardLayout = GetKeyboardLayout(0); // 0 means current thread
                                      // This seemingly cannot fail
                                      // If this value is cached then the application must respond to WM_INPUTLANGCHANGE

                                      // Retrieve the keyboard state
                                      // Handles CAPS-lock and SHIFT states
   if (GetKeyboardState(keyboardState) == FALSE)
      return utf;

   UINT virtualKey = MapVirtualKeyEx(scanCode, 3, hklKeyboardLayout);
   if (virtualKey == 0) // No translation possible
      return utf;

   int ascii = ToAsciiEx(virtualKey, scanCode, keyboardState, (LPWORD)ucBuffer, 0, hklKeyboardLayout);
   if (deadKey != '\0' && ascii == 1)
      // A dead key is stored and we have just converted a character key
      // Combine the two into a single character
      WCHAR wcBuffer[3];
      WCHAR out[3];
      wcBuffer[0] = ucBuffer[0];
      wcBuffer[1] = deadKey;
      wcBuffer[2] = '\0';
      if (FoldStringW(MAP_PRECOMPOSED, (LPWSTR)wcBuffer, 3, (LPWSTR)out, 3))
         utf = out[0];
         // FoldStringW failed
         DWORD dw = GetLastError();
         switch (dw)
         case ERROR_INVALID_FLAGS:
      deadKey = '\0';
   else if (ascii == 1)
      // We have a single character
      utf = ucBuffer[0];
      deadKey = '\0';
      // Convert a non-combining diacritical mark into a combining diacritical mark
      switch (ucBuffer[0])
      case 0x5E: // Circumflex accent: â
         deadKey = 0x302;
      case 0x60: // Grave accent: à
         deadKey = 0x300;
      case 0xA8: // Diaeresis: ü
         deadKey = 0x308;
      case 0xB4: // Acute accent: é
         deadKey = 0x301;
      case 0xB8: // Cedilla: ç
         deadKey = 0x327;
         deadKey = ucBuffer[0];

   return utf;

An example:

Code: Select all

   //On KeyDown_Event(keycode) (if you use the callback in glfw3)
   //On KeyUp_Event(keycode) (if you use the callback in glfw3)

The "keycodeToUTF32" has been extracted from this forum.


Posts: 448
Joined: Fri Jun 19, 2015 12:18
Location: Kiryat-Bialik, Israel

Re: [APORTE] Implement KeyBoard in GLFW3

Postby YaronCT » Sun Sep 18, 2016 21:18


User avatar
Posts: 1997
Joined: Sat Oct 31, 2009 13:57
Location: Austria

Re: [APORTE] Implement KeyBoard in GLFW3

Postby Ident » Mon Sep 19, 2016 18:10

I added a reference to this thread to http://cegui.org.uk/wiki/GLFW_to_CEGUI_ ... se_Mapping

Just popping in
Just popping in
Posts: 1
Joined: Sun Apr 16, 2017 21:15

Re: [APORTE] Implement KeyBoard in GLFW3

Postby madclaws » Sun Apr 16, 2017 21:18

thanks man saved my day!!

Return to “Modifications / Integrations / Customisations”

Who is online

Users browsing this forum: No registered users and 5 guests