delphi 键盘值

我在做一个计算器,在界面上有模拟的键盘,可以点击,我现在想键盘输入时,比如在键盘上按'1',相当于在界面上按button1(就是执行buttonoclick事件)

现在问题是,我不知道这个onkeypress函数应该关联哪个标签,比如我关联在Edit0KeyPress()没用,关联在窗体FormKeyPress()也没用,不知有没有大牛来此解答小弟疑惑?

建议利用窗口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;
我做的一个计算器。

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.
如果你是界面无法触发键盘事件对吧?把界面的KeyPreview属性设置为True就可以了。
温馨提示:内容为网友见解,仅供参考
第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.
第3个回答  2009-03-05
你是界面无法触发键盘事件对吧?把界面的KeyPreview属性设置为True就可以了。

delphi 键盘值
建议利用窗口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的其...

关于delphi键盘钩子问题, 键值lparam以及HHOOK 大概是什么东西啊!不是...
CallNextHookEx(hhk: HHOOK; {当前钩子的句柄}nCode: Integer; {钩子代码; 就是给下一个钩子要交待的}wParam: WPARAM; {要传递的参数; 由钩子类型决定是什么参数}lParam: LPARAM {要传递的参数; 由钩子类型决定是什么参数}): LRESULT; {会返回下一个钩子执行后的返回值; 0 表示失败}\/\/H...

delphi中选择行获取dbgrid值是用 oncellclick事件,但用键盘上的方向键...
delphi 的 AdoQurey 与 dbgrid 是数据感知控件,即当在 dbgrid 中移动行时,与之相关联的的 adoQuery 记录指针也会相应移动。上面的 edit 控件可以用 dbEdit 控件代替,可以即时的反映出数据记录的变化,可以节省代码。如果想响应记录行的变化,可以考虑直接在 adoQuery 的事件中,选择响应的事件,比如...

什么什么大冒险有按键精灵吗?
二、设计程序,用按键根本做不了自已的界面 按键会鼓吹用VB、DELPHI来设计界面,可笑的时,一个人如果会用VB、DELPHI这么大型的复杂的编程工具,还有必要用按键这种垃圾吗。而模拟精灵就不同了,使用网页就可以做出界面,大家知道网页的展现能力丰富,设计简单,《你必须知道的十种国产编程语言》高度评价“...

相似回答