how can i start making my own virtual keyboard program using lazarus in linux

I am new in using lazarus in linux, We actually have a project that we need to make our own virtual keyboard. We really don't know where to start. Can anybody have an idea please share to us. Thanks in advance.

Upvotes: 0

Views: 2198

Answers (1)

Abelisto
Abelisto

Reputation: 15624

  1. Create new project
  2. Add new required package lazmouseandkeyboardinput
  3. Add to uses section of the main form unit MouseAndKeyInput
  4. Put to the main form TPanel (named pnlVK in my example) and TEdit (named Edit1 in my example)

Code of the main form:

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
    Classes, Forms, Controls, ExtCtrls,
    Buttons, StdCtrls, MouseAndKeyInput;

type

    { TForm1 }

    TForm1 = class(TForm)
        Edit1: TEdit;
        pnlVK: TPanel;
        procedure FormCreate(Sender: TObject);
        procedure CapsChange(Sender: TObject);
        procedure VKClick(Sender: TObject);
        procedure SendVK(Data: PtrInt);
    private
        { private declarations }
    public
        { public declarations }
    end;

var
    Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.VKClick(Sender: TObject);
begin
    // Asynchrouse call to ensure that will no any collisions with LCL code
    Application.QueueAsyncCall(@SendVK, (Sender as TSpeedButton).Tag);
end;

procedure TForm1.SendVK(Data: PtrInt);
begin
    // Emulate key press
    KeyInput.Press(Integer(Data));
end;

procedure TForm1.FormCreate(Sender: TObject);
var
    b: TSpeedButton;
    cb: TSpeedButton;
    i: Byte;
    c: Char;
begin
    // Create virtual numeric keys from 0 to 9
    for i := 0 to 9 do
    begin
        c := Chr(i + Ord('0'));
        b := TSpeedButton.Create(Self);
        b.Caption := c;
        b.Tag := Ord(c);
        b.OnClick := @VKClick;
        b.Parent := pnlVK;
        b.Top := 4;
        b.Left := i * (b.Width + 2) + 2;
    end;

    // Create virtual alpha keys from a to j
    for i := 0 to 9 do
    begin
        // Use upper case characters for key codes
        c := Chr(i + Ord('A'));
        b := TSpeedButton.Create(Self);
        b.Caption := c;
        b.Tag := Ord(c);
        b.OnClick := @VKClick;
        b.Parent := pnlVK;
        b.Top := 8 + b.Height;
        b.Left := i * (b.Width + 2) + 2;
    end;

    // For changing shift states (Shift, Alt, Ctrl) see methods
    // procedure Apply(Shift: TShiftState);
    // procedure Unapply(Shift: TShiftState);
    // of the KeyInput object

    cb := TSpeedButton.Create(Self);
    with cb do
    begin
        Caption := 'Caps Lock';
        Width := Self.Canvas.TextWidth(Caption) + 8;
        AllowAllUp := True;
        GroupIndex := 1;
        Parent := pnlVK;
        Top := 4;
        Left := (b.Width + 2) * 10 + 4;
        Down := False;
        OnClick := @CapsChange;
    end;

    // Form & controls layout
    pnlVK.Align := alBottom;
    Self.Width := cb.Left + cb.Width + 10;
    pnlVK.Height := b.Height * 2 + 10;
    Edit1.Top := 4;
    Edit1.Left := 4;
    Edit1.Width := Self.Width - 8;
    Self.Height := Edit1.Height + pnlVK.Height + 10;
end;

procedure TForm1.CapsChange(Sender: TObject);
begin
    // Set shift state depending of the Caps Lock virtual key state
    if (Sender as TSpeedButton).Down then
        KeyInput.Apply([ssShift])
    else
        KeyInput.Unapply([ssShift]);
end;

end.

Remember that key strokes will receive currently focused control.

Some articles for reading:
MouseAndKeyInput
Asynchronous Calls
Lazarus forum Topic: Creating Virtual Keyboard

Hope this will helpful.

Upvotes: 1

Related Questions