2013-05-31 50 views

回答

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

希望這將有所幫助。

相關問題