这个问题是这样的:
每一个按键的消息TForm都是接受到了的,只是在消息处理循环中被其父辈TWinControl的
TWinControl.CNKeyDown当作SPECIALKEY处理掉了。请参考下面的程序:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm1 = class(TForm)
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure AppMessage(var Msg: TMsg; var Handled: Boolean);
procedure WmKeyDown(var Message: TMessage); message WM_KEYDOWN;
procedure CNKeyDown(var Message: TWMKeyDown); message CN_KEYDOWN;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key = vk_tab then showmessage('FormKeyDown: tab key down');//检测是否按下了TAB键
if key = vk_lwin then showmessage('FormKeyDown: lwin key down');//检测是否按了左边的win95键
end;
procedure TForm1.WmKeyDown(var Message: TMessage);
begin
inherited;
if Message.wParam = vk_tab then showmessage('WmKeyDown: tab key down');//检测是否按下了TAB键
if Message.wParam = vk_lwin then showmessage('WmKeyDown: lwin key down');//检测是否按了左边的win95键
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnMessage:=AppMessage;
end;
procedure TForm1.AppMessage(var Msg: TMsg; var Handled: Boolean);
begin
Handled:=False;
if Msg.message=WM_KeyDown then
begin
if Msg.wParam = vk_tab then showmessage('AppMessage: tab key down');//检测是否按下了TAB键
if Msg.wParam = vk_lwin then showmessage('AppMessage: lwin key down');//检测是否按了左边的win95键
end;
end;
procedure TForm1.CNKeyDown(var Message: TWMKeyDown);
begin
inherited;
if Message.CharCode = vk_tab then showmessage('CNKeyDown: tab key down');//检测是否按下了TAB键
if Message.CharCode = vk_lwin then showmessage('CNKeyDown: lwin key down');//检测是否按了左边的win95键
end;
end.
这个时候按下TAB键,就只会出现
AppMessage: tab key down
CNKeyDown: tab key down
两条消息。
如果将程序作一点小改动,屏蔽掉父辈的CNKeyDown处理
procedure TForm1.CNKeyDown(var Message: TWMKeyDown);
begin
// inherited;
if Message.CharCode = vk_tab then showmessage('CNKeyDown: tab key down');//检测是否按下了TAB键
if Message.CharCode = vk_lwin then showmessage('CNKeyDown: lwin key down');//检测是否按了左边的win95键
end;
这个时候再按下TAB键,就会出现所有的消息:
AppMessage: tab key down
CNKeyDown: tab key down
FormKeyDown: tab key down
WmKeyDown: tab key down
vk_lwin的情况自己测试一下吧。
至于TWinControl.CNKeyDown可以参考(Delphi5)Controls.pas:Line 6872,好好研究一下吧,呵呵!