Why won't my control accept keyboard input?

I created a custom control to which I am trying to send input. It will accept mouse input and tell MouseDown, MouseMove and MouseUp correctly, but for some reason it will not accept keyboard input. When I click on it, it does not receive focus, and any keys that I click on are interpreted by any control.

This is probably something really simple. The first thing I thought was in the ControlStyle property, but the only thing I can see in the keyboard input help file is that it csNoStdEventsdisables it, and my control does not have this. So what do I need to do to make sure that my control can get input focus?

+5
source share
5 answers

A few things to try:

  • In MouseDowncall Windows.SetFocus(Handle). In my experience, the WinAPI function SetFocusoften works better than the VCL method SetFocus.
  • In response to the message, WM_GETDLGCODEreply Message.Result := Message.Result or DLGC_WANTCHARS or DLGC_WANTARROWS or DLGC_WANTTAB or DLGC_WANTALLKEYS;
+7
source

Could it be as simple as calling SetFocus on the mouse?

procedure TYourCustomControl.MouseDown(Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer);
begin
  inherited;

  if CanFocus then
    SetFocus;
end;
+4
source

WS_TABSTOP set? , . 10 , , .

{TWinControl.}TabStop := True; . do-nothing, TWinControl , , , .

+2

, , . "" ""?

, :

procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
procedure WMKillFocus(var Message: TWMKillFocus); message WM_KILLFOCUS;
procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE;

procedure KeyDown(var Key: Word; Shift: TShiftState); override;
+1

Is keystroke available at the form level? That is, KeyPreview is enabled, and can you see a keystroke in the OnKeypress form? You can monitor it from there in the debugger. Is control (as Dan pointed out) suitable for keyboard input? For example, TLabel, although it displays text, is a graphical control.

0
source

All Articles