第1个回答 2009-03-04
建议利用窗口Form的onKeyDown()事件,不过要先把其KeyPreview属性设置为true,使窗口响应键盘输入。
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift:TShiftState);
begin
if (Key = Ord('1')) or(Key = Ord(VK_NUMPAD1)) then
showMessage('1') //也可以设置为按键1的其他响应函数
else if (Key = Ord('2')) or(Key = Ord(VK_NUMPAD2)) then
showMessage('2');
end;本回答被提问者采纳
第2个回答 2009-03-05
我做的一个计算器。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
SpeedButton4: TSpeedButton;
SpeedButton5: TSpeedButton;
SpeedButton6: TSpeedButton;
SpeedButton7: TSpeedButton;
SpeedButton8: TSpeedButton;
SpeedButton9: TSpeedButton;
SpeedButton10: TSpeedButton;
SpeedButton11: TSpeedButton;
SpeedButton12: TSpeedButton;
SpeedButton13: TSpeedButton;
SpeedButton14: TSpeedButton;
SpeedButton15: TSpeedButton;
SpeedButton16: TSpeedButton;
Edit1: TEdit;
SpeedButton17: TSpeedButton;
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton12Click(Sender: TObject);
procedure SpeedButton13Click(Sender: TObject);
procedure SpeedButton17Click(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
Flag: Boolean = True; //。标记位
Flag1: Integer = 1; //计算方法标志位
num1, num2, result: Real;
flagresult: Boolean = False;
procedure TForm1.SpeedButton1Click(Sender: TObject);
var
str: string;
begin
str := Edit1.Text;
if (Length(str) = 1) and (str = '0') then
Edit1.Clear;
Edit1.Color := clBlue;
if ((Sender as TSpeedButton).Caption = '.') then
begin
if Flag then
begin
Edit1.Text := Edit1.Text + (sender as TSpeedButton).Caption;
Flag := False;
end
end
else
Edit1.Text := Edit1.Text + (sender as TSpeedButton).Caption;
end;
procedure TForm1.SpeedButton12Click(Sender: TObject);
begin
Edit1.Color := clRed;
num2 := StrToFloatDef(Edit1.Text, 0.00);
case Flag1 of
1: result := num1 + num2;
2: result := num1 - num2;
3: result := num1 * num2;
4: if num2 = 0 then
begin
MessageBox(0, '除数为0', '', 0);
result := 0;
end
else
result := num1 / num2;
end;
Edit1.Text := FloatToStr(result);
end;
procedure TForm1.SpeedButton13Click(Sender: TObject);
begin
Flag1 := (Sender as TSpeedButton).Tag;
num1 := StrToFloatDef(Edit1.Text, 0.00);
Edit1.Text := '0';
end;
procedure TForm1.SpeedButton17Click(Sender: TObject);
begin
Flag := True; //。标记位
Flag1 := 1; //计算方法标志位
num1 := 0;
num2 := 0;
result := 0;
Edit1.Text := '0';
end;
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
case key of
'1': SpeedButton1.Click;
'2': SpeedButton2.Click;
'3': SpeedButton3.Click;
'4': SpeedButton4.Click;
'5': SpeedButton5.Click;
'6': SpeedButton6.Click;
'7': SpeedButton7.Click;
'8': SpeedButton8.Click;
'9': SpeedButton9.Click;
'0': SpeedButton10.Click;
'.': SpeedButton11.Click;
'+': SpeedButton13.Click;
'-': SpeedButton14.Click;
'*': SpeedButton15.Click;
'/': SpeedButton16.Click;
end;
end;
end.