我自己写的一个//sndkey32.pas网上应该有unit KeyboardUnit;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,sndkey32, Buttons, ExtCtrls,DefineUnit ;const MaxStrRecs = 78; KeyBroadStr : array[0..MaxStrRecs] of String = ('ESC','F1','F2','F3','F4','F5','F6','F7','F8','F9','F10','F11','F12','PRTSC','BREAK', '`','1','2','3','4','5','6','7','8','9','0','-','=','BACKSPACE', 'TAB','Q','W','E','R','T','Y','U','I','O','P','[',']','/', 'CAPSLOCK','A','S','D','F','G','H','J','K','L',';','''', 'ENTER', 'SHIFT','Z','X','C','V','B','N','M',',','.','/','UP', 'CTRL','ALT','SPACE','DEL','HOME','END','PGUP','PGDN','LEFT','DOWN','RIGHT');type TFrmKeyboard = class(TForm) Btnc_Num: TSpeedButton; SpeedButton17: TSpeedButton; SpeedButton18: TSpeedButton; SpeedButton19: TSpeedButton; SpeedButton20: TSpeedButton; SpeedButton21: TSpeedButton; SpeedButton22: TSpeedButton; SpeedButton23: TSpeedButton; SpeedButton24: TSpeedButton; SpeedButton25: TSpeedButton; SpeedButton26: TSpeedButton; SpeedButton27: TSpeedButton; SpeedButton28: TSpeedButton; SpeedButton29: TSpeedButton; BtnCommand: TSpeedButton; SpeedButton31: TSpeedButton; SpeedButton32: TSpeedButton; SpeedButton33: TSpeedButton; SpeedButton34: TSpeedButton; SpeedButton35: TSpeedButton; SpeedButton36: TSpeedButton; SpeedButton37: TSpeedButton; SpeedButton38: TSpeedButton; SpeedButton39: TSpeedButton; SpeedButton40: TSpeedButton; SpeedButton41: TSpeedButton; SpeedButton42: TSpeedButton; SpeedButton43: TSpeedButton; SpeedButton44: TSpeedButton; SpeedButton45: TSpeedButton; SpeedButton46: TSpeedButton; SpeedButton47: TSpeedButton; SpeedButton48: TSpeedButton; SpeedButton49: TSpeedButton; SpeedButton50: TSpeedButton; SpeedButton51: TSpeedButton; SpeedButton52: TSpeedButton; SpeedButton53: TSpeedButton; SpeedButton54: TSpeedButton; SpeedButton55: TSpeedButton; SpeedButton56: TSpeedButton; SpeedButton58: TSpeedButton; SpeedButton59: TSpeedButton; SpeedButton60: TSpeedButton; SpeedButton61: TSpeedButton; SpeedButton62: TSpeedButton; SpeedButton63: TSpeedButton; SpeedButton64: TSpeedButton; SpeedButton65: TSpeedButton; SpeedButton66: TSpeedButton; SpeedButton67: TSpeedButton; SpeedButton68: TSpeedButton; SpeedButton71: TSpeedButton; SpeedButton72: TSpeedButton; SpeedButton73: TSpeedButton; SpeedButton74: TSpeedButton; SpeedButton77: TSpeedButton; SpeedButton78: TSpeedButton; SpeedButton79: TSpeedButton; Panel1: TPanel; StaticText1: TStaticText; StaticText2: TStaticText; BtnShift: TSpeedButton; btnHide: TSpeedButton; procedure Btnc_NumClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure FormShow(Sender: TObject); procedure BtnCommandClick(Sender: TObject); procedure btnHideClick(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } FMsgHandle:THandle; //用于父窗口的窗口句柄 FISNumber:byte; //0 表示可以输入任何字符 1 表示只能输入整数,2表示可以输入小数 procedure SetISNumber(Value:byte); procedure SetMsgHandle(H:THandle); public { Public declarations } WCFoucs:TWinControl; WindowName : PChar; sWHandle:Longint; procedure WMActivate(var Message: TMessage); message WM_ACTIVATE; property MsgHandle:THandle read FMsgHandle write SetMsgHandle; property IsNumber :byte Read FISNumber Write SetISNumber; end;var FrmKeyboard: TFrmKeyboard;implementation{$R *.dfm}procedure TFrmKeyboard.SetMsgHandle(H:THandle);begin FMsgHandle:=H;end;procedure TFrmKeyboard.SetISNumber(Value:byte);var i:Integer; ntag:Integer;begin if Value>2 then exit; FISNumber:=Value; case FISNumber of 0: //0 表示可以输入任何字符 begin for i:=0 to self.ComponentCount-1 do begin if self.Components is TSpeedButton then (self.Components as TSpeedButton).Enabled :=true; end; end; 1: //1 表示只能输入整数 begin SpeedButton44.Down := False; BtnShift.Down :=False; for i:=0 to self.ComponentCount-1 do begin if self.Components is TSpeedButton then begin ntag:= (self.Components as TSpeedButton).Tag ; if (ntag<16) or (ntag >26) then (self.Components as TSpeedButton).Enabled :=false; if ntag in [28,29,55,71,72,71,73,79] then (self.Components as TSpeedButton).Enabled :=true; end; end; //end for end; 2: //2表示可以输入小数 begin SpeedButton44.Down := False; BtnShift.Down :=False; for i:=0 to self.ComponentCount-1 do begin if self.Components is TSpeedButton then begin ntag:= (self.Components as TSpeedButton).Tag ; if (ntag<16) or (ntag >26) then (self.Components as TSpeedButton).Enabled :=false; if ntag in [28,29,55,65,71,72,71,73,79] then (self.Components as TSpeedButton).Enabled :=true; end; end; //end for end; end;end;procedure TFrmKeyboard.WMActivate(var Message: TMessage);begin if (Message.wParamLo = WA_CLICKACTIVE) and (message.lParam<>sWHandle) then begin //得到当前活动窗口的标题 GetWindowText(message.lParam,WindowName,MAX_PATH); end;end;procedure TFrmKeyboard.Btnc_NumClick(Sender: TObject);Var Str:String; iTag:Integer;begin //发送字符 if WindowName=nil then exit; iTag:=(sender as TSpeedButton).Tag ; Str:= KeyBroadStr[iTag] ; if BtnShift.Down then Str:='+'+Str ; {ELSE IF BtnAlt.Down then Str:='%'+Str ELSE IF BtnCtrl.Down then Str:='^'+Str; } AppActivate(WindowName); //设置输入焦点窗口为活动窗口,以便接收消息 SendKeyS(PChar(Str),false);end;procedure TFrmKeyboard.FormCreate(Sender: TObject);begin FISNumber:=0; //分配窗口名称空间 GetMem(WindowName,MAX_PATH); sWHandle:=self.Handle ; Top :=600-self.Height ; Left:=800-self.Width ;end;procedure TFrmKeyboard.FormDestroy(Sender: TObject);begin //释放窗口名称空间 FreeMem(WindowName);end;procedure TFrmKeyboard.FormShow(Sender: TObject);var WHandle:Longint;begin //得到当前最前的窗口句柄 WHandle:=GetForegroundWindow; //得到窗口的标题 GetWindowText(WHandle,WindowName,MAX_PATH); SpeedButton44.Caption :='Caps Lock' +Char(13)+Char(10)+'大小写切换'; BtnShift.Caption :='Shift'+ Char(13)+Char(10)+'第二字符切换'; SpeedButton56.Caption :='Enter'+ Char(13)+Char(10)+'回车'; SpeedButton72.Caption :='Del'+ Char(13)+Char(10)+'删除'; SpeedButton29.Caption :='Backspace'+ Char(13)+Char(10)+'删除'; SpeedButton71.Caption :='Space'+ Char(13)+Char(10)+'空格'; SetWindowPos(Self.Handle, HWND_TOPMOST, self.Left ,Self.Top , self.Width ,self.Height , SWP_NOACTIVATE); ShowWindow(Self.Handle, SW_SHOWNOACTIVATE); if FMsgHandle<>0 then PostMessage(FMsgHandle,FSystemUserMsg, 1,5); end;procedure TFrmKeyboard.BtnCommandClick(Sender: TObject);Var Str:String; iTag:Integer;begin //发送命令码 if WindowName=nil then exit; AppActivate(WindowName); //设置输入焦点窗口为活动窗口,以便接收消息 iTag:=(sender as TSpeedButton).Tag ; Str:= '{'+KeyBroadStr[iTag]+'}' ; {IF BtnCtrl.Down then Str:='^'+Str;} SendKeys(PChar(Str), false);end;procedure TFrmKeyboard.btnHideClick(Sender: TObject);begin Close;end;procedure TFrmKeyboard.FormClose(Sender: TObject; var Action: TCloseAction);begin if FMsgHandle<>0 then PostMessage(FMsgHandle,FSystemUserMsg, 0,5);end;end.