PDA

View Full Version : how to read mulity keys hold?



est000dog
02-07-2010, 10:19 PM
Subject name says ewerything ;)

chronozphere
03-07-2010, 08:11 AM
GetASyncKeyState() (http://msdn.microsoft.com/en-us/library/ms646293%28VS.85%29.aspx) ;)

User137
03-07-2010, 10:15 AM
You can do this also using onKeyDown onKeyUp events of form.

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;

Mohammed Nasman
06-07-2010, 06:40 AM
You need to check that on OnKeyDown or OnKeyUp, for example if you need to check if CTRL + A has pressed you can use:

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;

Ñuño Martínez
06-07-2010, 08:12 AM
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.

User137
06-07-2010, 02:19 PM
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.

WILL
07-07-2010, 12:25 PM
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.

phibermon
10-08-2010, 12:01 AM
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.

Brainer
13-08-2010, 11:45 AM
Here's the code I use for capturing keyboard/mouse input. Hope you can make something out of it.

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.