Why won't my control accept keyboard input?

后端 未结 5 1115
梦谈多话
梦谈多话 2021-01-18 23:09

I\'ve built a custom control that I\'m trying to send input to. It will accept mouse input and report MouseDown, MouseMove and MouseUp correctly, but for whatever reason, i

相关标签:
5条回答
  • 2021-01-18 23:36

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

    procedure TYourCustomControl.MouseDown(Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer);
    begin
      inherited;
    
      if CanFocus then
        SetFocus;
    end;
    
    0 讨论(0)
  • 2021-01-18 23:41

    I've checked the code for my control and I can't see anything that might stop this working. Are you calling "inherited" in the Create procedure?

    I do handle the following, but nothing special:

    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;
    
    0 讨论(0)
  • 2021-01-18 23:50

    Do you have WS_TABSTOP set? You don't have input focus without that, I believe. But this is based on a recollection from nearly 10 years ago, when I was writing my own syntax-highlighting code editor, for which I have long since lost the source.

    {TWinControl.}TabStop := True; ought to do. A quick test app with a do-nothing component derived from TWinControl and displaying a dialog for key events seems to show that it makes all the difference.

    0 讨论(0)
  • 2021-01-18 23:52

    A few things to try:

    • On MouseDown, call Windows.SetFocus(Handle). In my experience, the WinAPI function SetFocus often works better than the VCL's SetFocus method.
    • In response to the WM_GETDLGCODE message, reply with Message.Result := Message.Result or DLGC_WANTCHARS or DLGC_WANTARROWS or DLGC_WANTTAB or DLGC_WANTALLKEYS;
    0 讨论(0)
  • 2021-01-18 23:56

    Is the keystroke available at form level? That is, is KeyPreview turned on, and can you see the keystroke in the form's OnKeypress event? You can follow it from there in the debugger. Is the control (as Dan indicated) suitable for keyboard input? For instance, a TLabel, although it displays text, is a graphical control.

    0 讨论(0)
提交回复
热议问题