Results 1 to 2 of 2

Thread: Convert SDL key codes (SDLK_ESCAPE, etc.) to a string (Delphi 2010)?

  1. #1

    Convert SDL key codes (SDLK_ESCAPE, etc.) to a string (Delphi 2010)?

    Hi all,
    I am using SDL in a game engine, and now I need to map SDK key codes (SDLK_ESCAPE, etc.) to a string. This is so I can get the user to configure their input keys for a game and see the currently selected input keys as a string.

    Any ideas?

    I have tried the code below (from in a DLL):
    Code:
    function  Window_KeyCodeToStr(KeyCode: Cardinal): PWideChar;
    var
      KeyName: String;
      s      : String;
    begin
      Result := '';
    
      if KeyCode > cMaxKeyCode then Exit;
    
      KeyName := SDL_GetKeyName(KeyCode);
      s       := 'KEY_'+UpperCase(KeyName);
    
      Result := PWideChar(s);
    end;
    but it puts garbage characters in KeyName for keys like Escape, CTRL, and other non-printable ascii codes. It works just fine for keys like SDLK_1, letters, and other similar ones.

    I have enabled Unicode support by using:

    Code:
    SDL_EnableUNICODE(1);
    but that hasn't helped...

    cheers,
    Paul
    Games:
    Seafox


    Pages:
    Syntax Error Software itch.io page

    Online Chess
    http://gameknot.com/#paul_nicholls

  2. #2
    I guess I could do something like this (seems to work), but then I'm afraid I will be locked into only Latin type keyboards?

    Code:
    function  Window_KeyCodeToStr(KeyCode: Cardinal): PWideChar;
    var
      KeyName: TxeString;
    begin
      Result := '';
    
      if KeyCode > cMaxKeyCode then Exit;
    
      if (KeyCode > $20) and (KeyCode < $7F) then
        KeyName := SDL_GetKeyName(KeyCode)
      else
      begin
        case KeyCode of
          SDLK_DELETE      : KeyName := 'DELETE';
          SDLK_BACKSPACE   : KeyName := 'BACKSPACE';
          SDLK_TAB         : KeyName := 'TAB';
          SDLK_CLEAR       : KeyName := 'CLEAR';
          SDLK_RETURN      : KeyName := 'RETURN';
          SDLK_PAUSE       : KeyName := 'PAUSE';
          SDLK_ESCAPE      : KeyName := 'ESCAPE';
          SDLK_SPACE       : KeyName := 'SPACE';
          // Numeric keypad
          SDLK_KP0         : KeyName := 'KP0';
          SDLK_KP1         : KeyName := 'KP1';
          SDLK_KP2         : KeyName := 'KP2';
          SDLK_KP3         : KeyName := 'KP3';
          SDLK_KP4         : KeyName := 'KP4';
          SDLK_KP5         : KeyName := 'KP5';
          SDLK_KP6         : KeyName := 'KP6';
          SDLK_KP7         : KeyName := 'KP7';
          SDLK_KP8         : KeyName := 'KP8';
          SDLK_KP9         : KeyName := 'KP9';
          SDLK_KP_PERIOD   : KeyName := 'KP_PERIOD';
          SDLK_KP_DIVIDE   : KeyName := 'KP_DIVIDE';
          SDLK_KP_MULTIPLY : KeyName := 'KP_MULTIPLY';
          SDLK_KP_MINUS    : KeyName := 'KP_MINUS';
          SDLK_KP_PLUS     : KeyName := 'KP_PLUS';
          SDLK_KP_ENTER    : KeyName := 'KP_ENTER';
          SDLK_KP_EQUALS   : KeyName := 'KP_EQUALS';
          // Arrows + Home/End pad
          SDLK_UP          : KeyName := 'UP';
          SDLK_DOWN        : KeyName := 'DOWN';
          SDLK_RIGHT       : KeyName := 'RIGHT';
          SDLK_LEFT        : KeyName := 'LEFT';
          SDLK_INSERT      : KeyName := 'INSERT';
          SDLK_HOME        : KeyName := 'HOME';
          SDLK_END         : KeyName := 'END';
          SDLK_PAGEUP      : KeyName := 'PAGEUP';
          SDLK_PAGEDOWN    : KeyName := 'PAGEDOWN';
          // Function keys
          SDLK_F1          : KeyName := 'F1';
          SDLK_F2          : KeyName := 'F2';
          SDLK_F3          : KeyName := 'F3';
          SDLK_F4          : KeyName := 'F4';
          SDLK_F5          : KeyName := 'F5';
          SDLK_F6          : KeyName := 'F6';
          SDLK_F7          : KeyName := 'F7';
          SDLK_F8          : KeyName := 'F8';
          SDLK_F9          : KeyName := 'F9';
          SDLK_F10         : KeyName := 'F10';
          SDLK_F11         : KeyName := 'F11';
          SDLK_F12         : KeyName := 'F12';
          SDLK_F13         : KeyName := 'F13';
          SDLK_F14         : KeyName := 'F14';
          SDLK_F15         : KeyName := 'F15';
          // Key state modifier keys
          SDLK_NUMLOCK     : KeyName := 'NUMLOCK';
          SDLK_CAPSLOCK    : KeyName := 'CAPSLOCK';
          SDLK_SCROLLOCK   : KeyName := 'SCROLLOCK';
          SDLK_RSHIFT      : KeyName := 'RSHIFT';
          SDLK_LSHIFT      : KeyName := 'LSHIFT';
          SDLK_RCTRL       : KeyName := 'RCTRL';
          SDLK_LCTRL       : KeyName := 'LCTRL';
          SDLK_RALT        : KeyName := 'RALT';
          SDLK_LALT        : KeyName := 'LALT';
          SDLK_RMETA       : KeyName := 'RMETA';
          SDLK_LMETA       : KeyName := 'LMETA';
          SDLK_LSUPER      : KeyName := 'LSUPER';
          SDLK_RSUPER      : KeyName := 'RSUPER';
          SDLK_MODE        : KeyName := 'MODE';
          SDLK_COMPOSE     : KeyName := 'COMPOSE';
          // Miscellaneous function keys
          SDLK_HELP        : KeyName := 'HELP';
          SDLK_PRINT       : KeyName := 'PRINT';
          SDLK_SYSREQ      : KeyName := 'SYSREQ';
          SDLK_BREAK       : KeyName := 'BREAK';
          SDLK_MENU        : KeyName := 'MENU';
          SDLK_POWER       : KeyName := 'POWER';
          SDLK_EURO        : KeyName := 'EURO';
        else
        end;
      end;
    
      Result := PWideChar(KeyName);
    end;
    cheers,
    Paul
    Games:
    Seafox


    Pages:
    Syntax Error Software itch.io page

    Online Chess
    http://gameknot.com/#paul_nicholls

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •