截取 TAB 键并抑制它 [英] Intercept TAB key and suppress it
问题描述
我需要拦截 TEdit 上的 TAB 键盘敲击并以编程方式抑制它们.在某些情况下,我不希望焦点转到下一个控件.
I need to intercept the TAB keyboard stroke on TEdits and suppress them programmatically. In certain cases I don't want the focus to change to the next control.
我尝试在 TEdit 级别和 TForm 上使用 KeyPreview=true 处理 KeyPress、KeyDown.我偷看了以下建议:
I tried to handle KeyPress, KeyDown both on TEdit level and on TForm with KeyPreview=true. I've peeked advices from:
但是没有用.这些事件是针对 Enter 键而不是针对 TAB 键触发的.
But it didn't work. The events are fired for, let's say, the Enter key BUT not for the TAB key.
我使用的是 Delphi 7.感谢您的帮助.
I'm using Delphi 7. Thanks for your help.
推荐答案
如果您想拦截 TAB 键行为,您应该捕获 CM_DIALOGKEY
消息.在这个例子中,如果你将 YouWantToInterceptTab
布尔值设置为 True,TAB
键将被吃掉:
If you want to intercept the TAB key behavior, you should catch the CM_DIALOGKEY
message. In this example, if you set the YouWantToInterceptTab
boolean value to True, the TAB
key will be eaten:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
private
YouWantToInterceptTab: Boolean;
procedure CMDialogKey(var AMessage: TCMDialogKey); message CM_DIALOGKEY;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.CMDialogKey(var AMessage: TCMDialogKey);
begin
if AMessage.CharCode = VK_TAB then
begin
ShowMessage('TAB key has been pressed in ' + ActiveControl.Name);
if YouWantToInterceptTab then
begin
ShowMessage('TAB key will be eaten');
AMessage.Result := 1;
end
else
inherited;
end
else
inherited;
end;
end.
这篇关于截取 TAB 键并抑制它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!