gFEB
gFEB

Reputation: 29

How to use the keybord as a braille keyboard in Delphi?

I tried working on events onKeyDown and onKeyUp. The program works perfectly when only two keys are pressed.

For combinations of more then 2 keys, if 2 keys are already pressed (and so they are down), the pressure of another key isn't caught and so the combination FGH is seen as FG corresponding to a different braille symbol.

Moreover, when 3 or more keys are pressed together the numbers of onKeyDown events caught aren't always the same.

Upvotes: 2

Views: 460

Answers (3)

GJ.
GJ.

Reputation: 10937

You can expect some troubles with cheap keyboards! Here you have a simple test program to test your keyboard. (Don't forget to define forms OnKeyDown and OnKeyUp events.)

type
  TForm8 = class(TForm)
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form8: TForm8;

implementation

{$R *.dfm}

procedure TForm8.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var
  State   :TKeyboardState;
  n       :integer;
  s       :string;
begin
  GetKeyboardState(State);
  s := '';
  for n := Low(byte) to High(byte) do
    if State[n] and 128 <> 0 then
      s := s + 'VK(' + IntToStr(n) + ') ';
  Caption :=  s;
end;

procedure TForm8.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
var
  State   :TKeyboardState;
  n       :integer;
  s       :string;
begin
  GetKeyboardState(State);
  s := '';
  for n := Low(byte) to High(byte) do
    if State[n] and 128 <> 0 then
      s := s + 'VK(' + IntToStr(n) + ') ';
  Caption :=  s;
end;

end.

Upvotes: 0

GolezTrol
GolezTrol

Reputation: 116180

Use GetAsyncKeyState. It doesn't only return the current state of the keys, but also whether the key has been pressed since the last call. There is a limit on the number of keys that can be pressed simultaneously. I think that limit is around 8 keys, but it may differ between hardware, drivers and OS version.

Upvotes: 0

Stijn Sanders
Stijn Sanders

Reputation: 36850

With GetKeyboardState you can retrieve a full array of the state of each key. To catch multiple keypresses like the braille symbols, you'd have to call it in a high sequence, for example from a TTimer with a really small Interval, or from a class inheriting from TThread. Also chances are with two or more buttons to get pressed, the key-down will not register on all keys at exactly the same time, so you'll have to keep track and only take combination that fits a criterium, for example that existed the longest.

Upvotes: 0

Related Questions