Showing posts with label Input Devices. Show all posts
Showing posts with label Input Devices. Show all posts

Friday, October 8, 2010

Dealing with Input

In BB.Input there is one class that handles the three main input artifacts (keyboard, mouse and joystick)


TInput = class
  public
    constructor Create(aHandle: hWnd; aDevices: TInputDevices); reintroduce;
    destructor Destroy; override;
    procedure Open;
    function IsOpen: boolean;
    procedure Close;
    procedure Read(aDevices: TInputDevices);
    procedure WaitForKey(aKey: integer);
    function WaitForAnyKey: integer;
    function InRectangle(aDevice: TInputDevice; const aRect: TRect): boolean;

    property Keyboard[key: integer]: boolean Read GetKeyboard;
    property KeyboardAvailable: boolean Read FKeyboardAvailable;
    property Mouse: TMouseData Read FMouse;
    property MouseAvailable: boolean Read FMouseAvailable;
    property Joystick: TJoyData Read FJoystick;
    property JoystickAvailable: boolean Read FJoystickAvailable;
  end;
Mouse example:
var
  input: TInput;

begin
  //Of course you can mix devices...
input := TInput.Create(WindowHandle, [idMouse]); //In the main loop repeat input.Read([idMouse]); //it is not mandatory to read all the devices if input.Mouse.ButtonL = bsDown then Beep; Writeln(input.Mouse.X + ',' + input.Mouse.Y); //Also you can check for Z (wheel) and delta changes via XI and YI until "WhateverCondition"; end;
Keyboard example:
var
  input: TInput;

begin
  input := TInput.Create(WindowHandle, [idKeyboard]);
  repeat
    input.Read([idKeyboard]);
   //The whole key list is listed in the source file
    if Input.Keyboard[DIK_1] then
      Beep;

  until "WhateverCondition";
end;

Joystick example:

var
  input: TInput;

begin
  input := TInput.Create(WindowHandle, [idJoystick]);
if not Input.JoystickAvailable then
Abort;

  repeat
    input.Read([idJoystick]);
    if (Input.Joystick.Left) or (Input.Joystick.Right) or 
(Input.Joystick.Up) or (Input.Joystick.Down) then
      Beep;

    if Input.Joystick.Button[0] = bsDown then
      Break;
until "WhateverCondition";
end;