自已的一个小工具需要用到软键盘,就写成了个函数~

自已的一个小工具需要用到软键盘,就写成了个函数~看图

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    Memo2: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure CreateKeyboard(myParent: TwinControl);  //创建软键盘
    procedure btnKeyClick(Sender: TObject);
    procedure btnBackspaceClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnBackspaceClick(Sender: TObject);
begin
    // 如果没有选择文本,则判断光标位置是否大于0
    if Memo2.SelStart > 0 then
    begin
      // 如果光标位置大于0,则删除光标前一个字符
      Memo2.SelStart := Memo2.SelStart - 1;
      Memo2.SelLength := 1;
      Memo2.SelText := '';
    end;
   Memo2.SetFocus;
   Memo2.SelStart := Length(Memo2.Text);
end;

procedure TForm1.btnKeyClick(Sender: TObject);
var
//  index: Integer;
  letter: string;
begin
    Letter:=LowerCase((Sender as TButton).Caption);
   if letter='back' then
   begin
    //Button2.Click;
     btnBackspaceClick(nil);
    Exit;
   end;
   if letter='space' then letter:=' ';
   Memo2.Lines.Text:= Memo2.Lines.Text+ Letter;
   Memo2.SetFocus;
   Memo2.SelStart := Length(Memo2.Text);

end;

procedure TForm1.CreateKeyboard(myParent: TwinControl);  //创建软键盘
var
  i, row, col: Integer;
  KeyMapping: array[0..37] of string;
  btnKey: array[0..37] of TButton;
begin
  KeyMapping[0] := '1';
  KeyMapping[1] := '2';
  KeyMapping[2] := '3';
  KeyMapping[3] := '4';
  KeyMapping[4] := '5';
  KeyMapping[5] := '6';
  KeyMapping[6] := '7';
  KeyMapping[7] := '8';
  KeyMapping[8] := '9';
  KeyMapping[9] := '0';
  KeyMapping[10] := 'Q';
  KeyMapping[11] := 'W';
  KeyMapping[12] := 'E';
  KeyMapping[13] := 'R';
  KeyMapping[14] := 'T';
  KeyMapping[15] := 'Y';
  KeyMapping[16] := 'U';
  KeyMapping[17] := 'I';
  KeyMapping[18] := 'O';
  KeyMapping[19] := 'P';

  KeyMapping[20] := 'A';
  KeyMapping[21] := 'S';
  KeyMapping[22] := 'D';
  KeyMapping[23] := 'F';
  KeyMapping[24] := 'G';
  KeyMapping[25] := 'H';
  KeyMapping[26] := 'J';
  KeyMapping[27] := 'K';
  KeyMapping[28] := 'L';

  KeyMapping[29] := 'Z';
  KeyMapping[30] := 'X';
   KeyMapping[31] := 'C';
    KeyMapping[32] := 'V';
     KeyMapping[33] := 'B';
    KeyMapping[34] := 'N';
      KeyMapping[35] := 'M';
  KeyMapping[36] := 'SPACE';
  KeyMapping[37] := 'BACK';
  for i := 0 to 37 do
  begin
    btnKey[i] := TButton.Create(Self);
    btnKey[i].Parent := myParent;
    row := i div 10;
    col := i mod 10;
    if row = 0 then
      btnKey[i].Top := 7
    else if row = 1 then
      btnKey[i].Top := 40
    else if row = 2 then
      btnKey[i].Top := 73
    else if row = 3 then
      btnKey[i].Top := 106;

    if row = 0 then
    begin
      if col < 9 then
        btnKey[i].Left := 10 + col * 33
      else if col = 9 then
        btnKey[i].Left := 305;
    end
    else if row = 1 then
    begin
      if col < 9 then
        btnKey[i].Left := 23 + col * 33
      else if col = 9 then
        btnKey[i].Left := 320;
    end
    else if row = 2 then
    begin
      if col = 0 then
        btnKey[i].Left := 40
      else if col < 9 then
        btnKey[i].Left := 40 + col * 33
      else if col = 9 then
        btnKey[i].Left := 335;
    end
    else if row = 3 then
    begin
      if col = 0 then
        btnKey[i].Left := 85
      else if col < 9 then
        btnKey[i].Left := 85 + col * 33
      else if col = 9 then
        btnKey[i].Left := 122;
    end;

    btnKey[i].Caption := KeyMapping[i];
    btnKey[i].Width := 32;
    btnKey[i].Height := 32;

     if i=29 then   //Z键
    begin
      btnKey[i].Top:=106;
      btnKey[i].Left:=53;
    //  btnKey[i].Width:=50;
    end;

    if i =36 then  //空格键
    begin
      btnKey[i].Width := 90;
    end;

    if i=37 then   //退格键
    begin
      btnKey[i].Top:=7;
      btnKey[i].Left:=338;
      btnKey[i].Width:=50;
    end;

     btnKey[i].OnClick := btnKeyClick;   //按纽事件
  end;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
    CreateKeyboard(panel1);
end;

end.

 

posted @ 2023-10-22 16:38  williamlv  阅读(21)  评论(0编辑  收藏  举报