在TComboBox或TButton上按VK_LEFT时未触发OnKeyDown事件

mar*_*_ja 4 delphi

当在TCheckBox或TButton控件上按下VK_LEFT键时,是否有办法触发OnKeyDown事件.目前,它只是选择另一个控件,但不会触发该事件.

UPDATE

这是我使用密钥VK_LEFT的代码,如TAB Back.

首先,我需要在某些控件上禁用VK_LEFT的标准行为,例如(TCheckBox,TButton,...):

procedure TfmBase.CMDialogKey(var Message: TCMDialogKey);
begin
  if Message.CharCode <> VK_LEFT then
    inherited;
end;
Run Code Online (Sandbox Code Playgroud)

届时,onKeyDown事件也被解雇VK_LEFT上TCheckBox,TButton的,......在这种情况下我把代码来选择一个控件.KeyPreview当然必须是真的.

procedure TfmBase.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  handled: Boolean;
begin
  if Key = VK_LEFT then
  begin
    handled := false;
    TabBack(handled); //This is my function, which checks the type of the control and selects the previous control.
    if handled then
      Key := 0;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

Dav*_*nan 5

您不会触发该事件,因为该按键被解释为表单导航.顶级消息循环识别出这是一个导航键并转移消息以执行该导航.

如果您想要处理此事件,那么您唯一的机会就是在Application.OnMessage消息被转移之前发生火灾.

UPDATE

在评论中,您指示要拦截此事件以执行导航.由于事件未因为正在执行默认导航而触发,因此理想的解决方案可能是覆盖默认导航.

我相信驱动这一点的关键例程是TWinControl.CNKeyDown.通过阅读此代码,我认为您只需要处理CM_DIALOGKEY您的表单并说服导航按您希望的方式运行.

您的代码应如下所示:

procedure TMyForm.CMDialogKey(var Message: TCMDialogKey);
begin
  if GetKeyState(VK_MENU) >= 0 then begin
    case Message.CharCode of
    VK_LEFT:
      if ActiveControl=MyControl1 then begin
        MyControl2.SetFocus;
        Message.Result := 1;
        Exit;
      end;
    end;
  end;
  inherited;
end;
Run Code Online (Sandbox Code Playgroud)