Class TKeyboard

Unit

Declaration

type TKeyboard = class(TObject)

Description

Encapsulates keyboard input.

You don't need to create it, use it through TA3DGEApplication.Keyboard.

Hierarchy

  • TObject
  • TKeyboard

Overview

Methods

Public procedure Clear;
Public function Keypressed: Boolean; inline;
Public function Readkey: LongInt;
Public function UReadkey (out aScancode: LongInt): LongInt;
Public function KeyModifiers: LongInt; inline;

Properties

Public property KeyState[KeyCode:Integer]: Boolean read GetKeyState write SetKeyState;
Public property OnKeyDown: TKeyboardEvent read fOnKeyDown write fOnKeyDown;
Public property OnKeyUp: TKeyboardEvent read fOnKeyUp write fOnKeyUp;

Description

Methods

Public procedure Clear;

Clear keyboard state and key buffer.

See also
TKeyboard.Readkey
Read next key input.
TKeyboard.KeyState
Access to key states.
Public function Keypressed: Boolean; inline;

Check if one or more keys where pressed.

See also
TKeyboard.Readkey
Read next key input.
TKeyboard.UReadkey
Read next UNICODE key input.
Public function Readkey: LongInt;

Read next key input.

It returns the next character from the keyboard buffer, in ASCII format. If key buffer is empty it returns 0. You can see if there are queued keypresses with Keypressed.

The low byte of the return value contains the ASCII code of the key, and the high byte the scancode. The scancode remains the same whatever the state of the shift, ctrl and alt keys, while the ASCII code is affected by shift and ctrl in the normal way (shift changes case, ctrl+letter gives the position of that letter in the alphabet, eg. ctrl+A = 1, ctrl+B = 2, etc). Pressing alt+key returns only the scancode, with a zero ASCII code in the low byte. For example:

var
  Key: LongInt;
  ...
  Key := a3dge.Application.Keyboard.Readkey;

  if (Key and $FF) = ord ('d') then
    WriteLn ('You pressed "d"');

  if (Key shr 8) = ALLEGRO_KEY_SPACE then
    WriteLn ('You pressed Space');

  if (Key and $FF) = 3 then
    WriteLn ('You pressed Control+C');

  if Key = (ALLEGRO_KEY_X shl 8) then
    WriteLn ('You pressed Alt+X');

This function cannot return character values greater than 255.

See also
TKeyboard.UReadkey
Read next UNICODE key input.
TKeyboard.KeyModifiers
Read key modifiers state.
TKeyboard.Clear
Clear keyboard state and key buffer.
TKeyboard.KeyState
Access to key states.
Public function UReadkey (out aScancode: LongInt): LongInt;

Read next UNICODE key input.

It returns the next character from the keyboard buffer, in Unicode format. If the buffer is empty, it returns 0. You can see if there are queued keypresses with Keypressed. The return value contains the Unicode value of the key, and the argument will be set to the scancode. Unlike Readkey, this function is able to return character values greater than 255. Example:

var
  Key, Scancode: LongInt;
  ...
  Key := a3dge.Application.keyboard.UReadkey (Scancode);

  if Key = $00F1 then
    WriteLn ('You pressed n with tilde');

  if Key = $00DF then
    WriteLn ('You pressed sharp s');

You should be able to find Unicode character maps at http://www.unicode.org/.

See also
TKeyboard.Readkey
Read next key input.
TKeyboard.KeyModifiers
Read key modifiers state.
TKeyboard.Clear
Clear keyboard state and key buffer.
Public function KeyModifiers: LongInt; inline;

Read key modifiers state.

It returns the key modifiers of the current keyboard buffer. If keyboard buffer is empty, this will return invalid values. For example:

var
  Key, Modifier: LongInt;
  ...
  Modifier := Game.Keyboard.KeyModifiers;
  Key := Game.Keyboard.Readkey;

  if (Key shr 8) = ALLEGRO_KEY_LEFT then
  begin
    if (Modifier and ALLEGRO_KEY_MOD_SHIFT) <> 0 then
      X := X - 10
    else
      X := X - 1
  end;
  if (Key shr 8) = ALLEGRO_KEY_RIGHT then
  begin
    if (Modifier and ALLEGRO_KEY_MOD_SHIFT) <> 0 then
      X := X + 10
    else
      X := X + 1
  end;

Note you should call to KeyModifiers before than Readkey.

See also
TKeyboard.Readkey
Read next key input.
TKeyboard.UReadkey
Read next UNICODE key input.
TKeyboard.KeyModifiers
Read key modifiers state.

Properties

Public property KeyState[KeyCode:Integer]: Boolean read GetKeyState write SetKeyState;

Access to key states.

See also
TKeyboard.Clear
Clear keyboard state and key buffer.
TKeyboard.Keypressed
Check if one or more keys where pressed.
Public property OnKeyDown: TKeyboardEvent read fOnKeyDown write fOnKeyDown;

Event triggered when a key is pressed.

Public property OnKeyUp: TKeyboardEvent read fOnKeyUp write fOnKeyUp;

Event triggered when a key is released.


Generated by PasDoc 0.15.0. Generated on 2025-07-31 11:41:01.