如何在linux中使用lazarus开始制作我自己的虚拟键盘程序

时间:2013-05-31 02:27:34

标签: ubuntu-12.04 lazarus

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

1 个答案:

答案 0 :(得分:1)

  1. 创建新项目
  2. 添加新的必需包lazmouseandkeyboardinput
  3. 使用主窗体单元MouseAndKeyInput
  4. 的“使用”部分
  5. 放入主窗体TPanel(在我的示例中名为pnlVK)和TEdit(在我的示例中名为Edit1)
  6. 主要表格代码:

    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
    Asynchronous Calls
    Lazarus forum Topic: Creating Virtual Keyboard

    希望这会有所帮助。