The OnKeyDown event does not fire when VK_LEFT is pressed on TComboBox or TButton

Is there a way to get the OnKeyDown event when the VK_LEFT key is pressed on a TCheckBox or TButton control. Currently, it just selects another control, but does not fire this event.

UPDATE

Here is my code to use the VK_LEFT key, like TAB Back.

First, I needed to disable the standard VK_LEFT behavior on some controls (TCheckBox, TButton, ...):

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

then the OnKeyDown event is also fired for VK_LEFT on TCheckBox, TButton, ... In this case, I put the code to select the previous control. Of course, KeyPreview should be true.

 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; 
+4
source share
2 answers

You cannot trigger this event because this keystroke is interpreted as formal navigation. The top-level message cable recognizes that it is a navigation key and forwards the message to perform this navigation.

If you want to handle this event, then your only way to do this is in Application.OnMessage , which fires before the message is redirected.

UPDATE

In the comment, you indicate that you want to intercept this event in order to navigate. Since the event does not fire because the default navigation is being performed, perhaps the ideal solution is to override the default navigation.

I believe that the key procedure that drives this is TWinControl.CNKeyDown . After reading this code, I think you just need to handle the CM_DIALOGKEY in your form and convince the navigation to behave the way you want it to.

Your code should look something like this:

 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; 
+5
source

You need to process the WM_GETDLGCODE message:

 procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE; .... procedure TfmBase.WMGetDlgCode(var Message: TWMGetDlgCode); begin Message.Result:=DLGC_WANTALLKEYS or DLGC_WANTARROWS; end; 
+3
source

Source: https://habr.com/ru/post/1340590/


All Articles