Results 1 to 9 of 9

Thread: how to read mulity keys hold?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    how to read mulity keys hold?

    Subject name says ewerything

  2. #2

    Re: how to read mulity keys hold?

    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  3. #3

    Re: how to read mulity keys hold?

    You can do this also using onKeyDown onKeyUp events of form.

    [pascal]First define public variable:
    keys: array[0..255] of boolean;

    onKeyDown:
    if key<256 then Keys[key]:=true;

    onKeyUp:
    if key<256 then Keys[key]:=false;

    Now in your main application loop you can use it:
    if keys[VK_UP] then MoveForward;
    if keys[VK_DOWN] then MoveBack;
    if keys[VK_LEFT] then StrafeLeft;
    if keys[VK_RIGHT] then StrafeRight;
    if keys[ord('Z')] then Shoot;[/pascal]

  4. #4

    Re: how to read mulity keys hold?

    You need to check that on OnKeyDown or OnKeyUp, for example if you need to check if CTRL + A has pressed you can use:

    [pascal]procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
    Shift: TShiftState);
    begin
    if (ssCtrl in Shift ) and (Key = 65) then
    ShowMessage('CTRL + A has pressed');
    end;[/pascal]

  5. #5

    Re: how to read mulity keys hold?

    Actually it deppends on what library are you using. The previous work for VCL, CLX and LCL, but SDL uses a different way, Allegro.pas another one, a "pure" console another...

    Actually it would be different if you're using Delphi, Lazarus, "plain Free Pascal", GNU/Pascal, etc. And different Operating Systems... you know.
    No signature provided yet.

  6. #6

    Re: how to read mulity keys hold?

    Using the onKeyDown method works equally on all operating systems in Delphi, Lazarus and Kylix (is that still alive?). I would imagine starting game programmers use visual user interface. For console based stuff experience level should be enough to figure out the rest

    SDL has its own keyboard functions if its used and those are also not dependant on platform or compiler.

  7. #7
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Re: how to read mulity keys hold?

    Quote Originally Posted by User137
    Kylix (is that still alive?)
    Nope, it's dead. The new Project X has taken over and there will be 2 completely new compiler projects in the works. Delphi for Mac and Delphi for Linux. Their working out the memory stack issues as we all type.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  8. #8
    PGD Staff / News Reporter phibermon's Avatar
    Join Date
    Sep 2009
    Location
    England
    Posts
    524

    Re: how to read mulity keys hold?

    I found and stuck to my cross platform solution a few years ago. I got so fed up of catering for different operating systems, doing the same kind of code over and over only in different contexts with different calls. so :

    Lazarus for an IDE (FPC as compiler). Cross Platform. You don't have to use the FCL. It's Free. syn-edit empowered code editor / code completion not to mention a hierarchical code explorer for complex objects and one click jump between routine decelerations / implementations. It's nearly everything you need.

    SDL is great for uniform input handling, window & context(GL) creation etc plus it exists for most of the important computer/OS combos that FPC supports. (I don't personally use it for anything other than input, window/context creation and sound. But it's nice to know it's all there should I need it)

    http://imaginglib.sourceforge.net/ - Vampyre - FPC, Cross platform etc more than good enough for your image loading/saving needs.

    Audio... use SDL or platform specifics if you want to keep it open source or use BASS/FMOD if you want to save yourself tons of work.

    oh and there's a few good networking libs depending on what kind of project you're working on. Synapse, lnet, indy etc. I use my own for raw packet game type stuff.
    When the moon hits your eye like a big pizza pie - that's an extinction level impact event.

  9. #9

    Re: how to read mulity keys hold?

    Here's the code I use for capturing keyboard/mouse input. Hope you can make something out of it.
    [pascal]
    unit UInput;

    interface

    uses
    Windows;

    type
    { .: TInputMouseButton :. }
    TInputMouseButton = (btnLeft, btnMiddle, btnRight);

    { .: TInputActionKey :. }
    TInputActionKey = (akMoveForward = 0, akBackpedal, akStrafeLeft, akStrafeRight);

    { .: TInputActionKeys :. }
    TInputActionKeys = array[TInputActionKey] of Integer;

    { .: TInputManager :. }
    TInputManager = class sealed(TObject)
    private
    { Private declarations }
    FAction: TInputActionKeys;
    function GetActionKey(X: TInputActionKey): Integer;
    procedure SetActionKey(X: TInputActionKey; const Value: Integer);
    public
    { Public declarations }
    function IsKeyDown(const KeyCode: Integer): Boolean;
    function IsKeyUp(const KeyCode: Integer): Boolean;

    function IsMouseButtonDown(const AMouseButton: TInputMouseButton): Boolean;
    function GetMousePosition(): TPoint;
    procedure SetMousePosition(const DesiredPos: TPoint);

    property ActionKeys[X: TInputActionKey]: Integer read GetActionKey write SetActionKey;
    end;

    implementation

    { TBrainInputManager }

    function TInputManager.GetActionKey(X: TInputActionKey): Integer;
    begin
    Result := FAction[X];
    end;

    function TInputManager.GetMousePosition: TPoint;
    begin
    GetCursorPos(Result);
    end;

    function TInputManager.IsKeyDown(const KeyCode: Integer): Boolean;
    begin
    Result := (GetAsyncKeyState(KeyCode) < 0);
    end;

    function TInputManager.IsKeyUp(const KeyCode: Integer): Boolean;
    begin
    Result := (GetAsyncKeyState(KeyCode) = 0);
    end;

    function TInputManager.IsMouseButtonDown(
    const AMouseButton: TInputMouseButton): Boolean;
    begin
    Result := False;
    case AMouseButton of
    btnLeft:
    Result := IsKeyDown(VK_LBUTTON);
    btnMiddle:
    Result := IsKeyDown(VK_MBUTTON);
    btnRight:
    Result := IsKeyDown(VK_RBUTTON);
    end;
    end;

    procedure TInputManager.SetActionKey(X: TInputActionKey; const Value: Integer);
    begin
    FAction[X] := Value;
    end;

    procedure TInputManager.SetMousePosition(const DesiredPos: TPoint);
    begin
    SetCursorPos(DesiredPos.X, DesiredPos.Y);
    end;

    end.
    [/pascal]

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
  •