Delphi XE和陷阱箭头键与OnKeyDown [英] Delphi XE and Trapping Arrow Key with OnKeyDown
问题描述
主要消息由接收这些消息的控件本身处理,这就是为什么当你在按钮表单没有收到消息。所以通常你必须对这些控件进行子类化,但是如果表单感兴趣,则VCL很适合请教父母表单:
type
TForm1 = class(TForm)
..
private
procedure DialogKey(var Msg:TWMKey);消息CM_DIALOGKEY;
..
程序TForm1.DialogKey(var Msg:TWMKey);
begin
如果没有(Msg.CharCode在[VK_DOWN,VK_UP,VK_RIGHT,VK_LEFT])然后
继承;
结束
François编辑:回答OP原始问题,您需要打电话onKeyDown不知何故,以便他的事件代码可以工作(随时可以编辑;对于评论太长了)。
键入
TForm1 = class(TForm)
Button1:TButton;
Button2:TButton;
Button3:TButton;
Button4:TButton;
procedure FormKeyDown(Sender:TObject; var Key:Word; Shift:TShiftState);
private
{私有声明}
程序DialogKey(var Msg:TWMKey);消息CM_DIALOGKEY;
public
{公开声明}
end;
var
Form1:TForm1;
执行
{$ R * .dfm}
程序TForm1.DialogKey(var Msg:TWMKey);
begin
case Msg.CharCode
VK_DOWN,VK_UP,VK_RIGHT,VK_LEFT:
如果已分配(onKeyDown)然后
onKeyDown(Self,Msg.CharCode,KeyDataToShiftState( Msg.KeyData));
else
继承
end;
结束
程序TForm1.FormKeyDown(发件人:TObject; var Key:Word;
Shift:TShiftState);
begin
case
的关键字VK_DOWN:Top:= Top + 5;
VK_UP:Top:= Top - 5;
VK_LEFT:左:=左 - 5;
VK_RIGHT:Left:= Left + 5;
结束
结束
I want my form to handle the arrow keys, and I can do it -- as long as there is no button on the form. Why is this?
Key messages are processed by the controls themselves who receives these messages, that's why when you're on a button the form is not receiving the message. So normally you would have to subclass these controls, but the VCL is kind enough to ask the parenting form what to do if the form is interested:
type
TForm1 = class(TForm)
..
private
procedure DialogKey(var Msg: TWMKey); message CM_DIALOGKEY;
..
procedure TForm1.DialogKey(var Msg: TWMKey);
begin
if not (Msg.CharCode in [VK_DOWN, VK_UP, VK_RIGHT, VK_LEFT]) then
inherited;
end;
François editing: to answer the OP original question, you need to call onKeyDown somehow so that his event code would work (feel free to edit; was too long for a comment).
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
private
{ Private declarations }
procedure DialogKey(var Msg: TWMKey); message CM_DIALOGKEY;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.DialogKey(var Msg: TWMKey);
begin
case Msg.CharCode of
VK_DOWN, VK_UP, VK_RIGHT, VK_LEFT:
if Assigned(onKeyDown) then
onKeyDown(Self, Msg.CharCode, KeyDataToShiftState(Msg.KeyData));
else
inherited
end;
end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
case Key of
VK_DOWN: Top := Top + 5;
VK_UP: Top := Top - 5;
VK_LEFT: Left := Left - 5;
VK_RIGHT: Left := Left + 5;
end;
end;
这篇关于Delphi XE和陷阱箭头键与OnKeyDown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!