delphi 键盘值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了delphi 键盘值相关的知识,希望对你有一定的参考价值。
我在做一个计算器,在界面上有模拟的键盘,可以点击,我现在想键盘输入时,比如在键盘上按'1',相当于在界面上按button1(就是执行buttonoclick事件)
现在问题是,我不知道这个onkeypress函数应该关联哪个标签,比如我关联在Edit0KeyPress()没用,关联在窗体FormKeyPress()也没用,不知有没有大牛来此解答小弟疑惑?
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就可以了。 参考技术A 建议利用窗口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;本回答被提问者采纳 参考技术B 我做的一个计算器。
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. 参考技术C 你是界面无法触发键盘事件对吧?把界面的KeyPreview属性设置为True就可以了。
虚拟键盘 - Android - Delphi XE5
【中文标题】虚拟键盘 - Android - Delphi XE5【英文标题】:Virtual Keyboard - Android - Delphi XE5 【发布时间】:2013-12-27 09:59:40 【问题描述】:默认情况下,当我单击 TMemo 时,会显示虚拟键盘。 我想要没有自动显示虚拟键盘的 TMemo,我将 Hit Test 设置为 False,但现在我的 TMemo 不可滚动:(
如何在没有虚拟键盘的情况下实现可滚动的 TMemo?
【问题讨论】:
设为只读?没有尝试过(现在无法访问我的 XE5),但它可以工作...... 还没有在 XE5 中看到 ReadOnly 属性 :) 所以 FMX 中的 TMemo 不像 VCL 中那样具有 ReadOnly 属性?就像我说的,当时我无法访问我的 XE5,所以我无法验证它。感谢您的澄清... 【参考方案1】:将“CanFocus”设置为 false。 将“HitTest”设置为 true - 可以在 TMemo 中滚动。
【讨论】:
以上是关于delphi 键盘值的主要内容,如果未能解决你的问题,请参考以下文章