Results 1 to 10 of 12

Thread: Sdl/OpenGl Keyboard management

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    Chesso: Yes, this is cross platform so windowze API is no go, especially since I don't use windowze...
    User137: Yes, I am looking at doing a -32 on a-z keys automatically, but does it also work with caps, backspace and num lock?

    I've had no luck at all getting it to work. So I guess there is no sdl function to do this? Or does anyone have an engine that has that functionality integrated (open source please)? I don't use lazarus so my plain fpc install wont have the VCL. My executable size is already knocking on 800k due to all the libs I'm using.

    cheers,
    code_glitch.
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  2. #2
    Lazarus seems to use CN_CHAR message for onKeyPress. Look up TWinControl in controls.pas and wincontrol.inc.
    TWinControl.CNChar() sends to DoKeyPress(). Seems pretty complicated process to me and there was no documentation for CN_CHAR.

    Backspace is very simple... if key number is VK_BACK then text:=copy(text,1,length(text)-1);
    At least if not allowing cursor moving to the middle of text, or selecting part of text. I'm afraid for graphical application all funtionality of TEdit has to be coded by self.

    If you can simulate onKeyPress event by some means there is some hints from my UI class:
    Code:
    // note: caretPos is a property with built in limitations to value range.
    
    procedure TUIEdit.KeyPress(c: char);
    var l: integer;
    begin
      if byte(c)>31 then begin
        FText:=UTF8toSys(FText);
        l:=length(FText);
        SetText(SysToUTF8(copy(FText,1,caretPos)+c+copy(FText,caretPos+1,l-caretPos)));
        caretPos:=caretPos+1;
      end;
    end;
    
    procedure TUIEdit.KeyDown(key: word; shift: TShiftState);
    var l,p: integer;
    begin
      l:=length(text);
      case byte(key) of
        VK_LEFT: caretPos:=caretPos-1;
        VK_RIGHT: caretPos:=caretPos+1;
        VK_HOME: caretPos:=0;
        VK_END: caretPos:=l;
        VK_DELETE:
          if (caretPos<l) and (l>0) then begin
            FText:=UTF8toSys(FText);
            SetText(SysToUTF8(copy(text,1,caretPos)+copy(text,caretPos+2,l-caretPos)));
          end;
        VK_BACK:
          if caretPos>0 then begin
            p:=caretPos; caretPos:=caretPos-1;
            FText:=UTF8toSys(FText);
            SetText(SysToUTF8(copy(text,1,p-1)+copy(text,p+1,l-p)));
          end;
        VK_RETURN: Focused:=false;
      end;
    end;
    Last edited by User137; 14-03-2011 at 07:00 PM.

  3. #3
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    Thanks for those, will start implementing it all in my code as soon as I can... A little pressed for time recently, but its nice to get all solutions done so they are ready to be implemented the next time I get some time.
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  4. #4
    Perhaps this SDL tutorial "Getting String Input" might help?

    http://www.lazyfoo.net/SDL_tutorials/lesson23/index.php

    It isn't Pascal, but should be easy to get info from

    cheers,
    Paul

  5. #5
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    Thanks paul, looked through and sure enough a solution for sdl to handle it automatically which is indeed very nice as it means I dont have to re-write my event handlers.

    cheers for that great find.
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  6. #6
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    Just for future reference let me say that the solution for this is to use SdlEvent^.Key.KeySym.Unicode rather then SdlEvent^.Key.KeySym.Sym with Sdl_EnableUnicode(1) and then using chr() and ord() as required to get special characters, upper-case, lower-case and etc...

    End of line.
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  7. #7
    Good to know

    cheers,
    Paul

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
  •