Search Home Members Contacts
About Us
Products
Downloads
Community
Support
Pages: [1]
  Print  
Author Topic: Delphi GetKeyBuffer Solution  (Read 390 times)
bruffner
Customers
Community Member
*****
Posts: 200


« on: December 30, 2003, 04:05:22 PM »

I was trying to use GetKeyBuffer and couldn't find a good example so i figured i would share with everyone my solution in Delphi.

Declare this outside of your loop:

Code:
KeyBuffer: PSafeArray;


Init your PSafeArray

Code:
Procedure doInit;
var
sab: TVarArrayBound;
begin
  Sab.ElementCount := 1;
  Sab.LowBound := 0;
  KeyBuffer := SafeArrayCreate(varDispatch,1,NumPressed);
end;


Call this in your loop to handle the keys.

Code:
Function DoKeys(InputFactory: TTVInputEngine);
type
  Key = Array of TV_KEYDATA;
var
  x: Integer;
  NumPressed: Integer;
  KeyData: Pointer;
begin

  if KeyBuffer <> nil then
  begin
  InputFactory.GetKeyBuffer(KeyBuffer, NumPressed);

  if (NumPressed > 0) and (keyBuffer <> nil) then
  begin
    for x := 0 to NumPressed - 1 do
    begin
      SafeArrayAccessData(KeyBuffer, KeyData);
      if (Key(KeyData)[x].Pressed) then
        if (Key(KeyData)[x].Key = TV_KEY_BACKSPACE) then
        begin
          DoBackspace;
        end
        else
            TypeKey(Key(KeyData)[x].key);
    end;
  end;
  end;
end;


On Destroy you may want to call :   SafeArrayDestroy(KeyBuffer);

BR
Logged

Visit us at:
James
Community Member
*
Posts: 883


« Reply #1 on: December 31, 2003, 07:53:49 AM »

Interesting.. Due to the speed of COM and safe arrays I implemented my own DirectInput objects and just wrote my own handling routines - but this info should help other Delphi people wondering about the safe array stuff Smiley
Logged

James
[www.DeepVoodooGaming.Com (Ireland)]
DarekRuman
Community Member
*
Posts: 67

game developer


WWW
« Reply #2 on: June 28, 2008, 06:04:23 AM »

Works great but there is a mistake in Init code. Should be :

Code:
KeyBuffer: PSafeArray;

Init your PSafeArray

Code:
Procedure doInit;
var
sab: TVarArrayBound;
begin
  Sab.ElementCount := 1;
  Sab.LowBound := 0;
  KeyBuffer := SafeArrayCreate(varDispatch,1,Sab); // <--
end;

And if you wanna check more than 1 key in the same time, increase Sab.ElementCount to that quantity.

Greetz!
Logged

www.reddotgames.pl
paintball eXtreme | project
TV6.3 developer
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.3 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks