0

我是在 linux 中使用 lazarus 的新手,我们实际上有一个项目,我们需要制作自己的虚拟键盘。我们真的不知道从哪里开始。任何人都可以有一个想法,请分享给我们。提前致谢。

4

1 回答 1

1
  1. 创建新项目
  2. 添加新的必需包lazmouseandkeyboardinput
  3. 添加到主窗体单元的使用部分MouseAndKeyInput
  4. 放入主窗体 TPanel(在我的示例中命名为 pnlVK)和 TEdit(在我的示例中命名为 Edit1)

主窗体代码:

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.

请记住,击键将获得当前的集中控制。

一些文章供阅读:
MouseAndKeyInput
异步调用
Lazarus 论坛主题:创建虚拟键盘

希望这会有所帮助。

于 2013-05-31T08:54:19.237 回答