Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't my control accept keyboard input?

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, it won't accept keyboard input. When I click on it, it doesn't receive focus, and any keys I press get interpreted by whatever control had the focus already.

This is probably something really simple. The first place I thought to look was in the ControlStyle property, but the only thing I can see in the helpfile about keyboard input is csNoStdEvents, which disables it, and my control doesn't have that. So what do I need to do to make it so my control can receive input focus?

like image 326
Mason Wheeler Avatar asked May 12 '10 12:05

Mason Wheeler


People also ask

Why is my computer not responding to my keyboard?

An outdated or corrupt driver could be the reason your keyboard isn't working. Step 1: Right-click on Start and select Device Manager. Step 2: Expand Keyboards. Step 3: Right-click on the affected keyboard and select Update driver.

How do I enable keyboard?

Go to Start , then select Settings > Accessibility > Keyboard, and turn on the On-Screen Keyboard toggle. A keyboard that can be used to move around the screen and enter text will appear on the screen. The keyboard will remain on the screen until you close it.

Why is my external keyboard not typing?

Check your connection Sometimes the simplest solution fixes the problem. Verify the keyboard is plugged in securely. Disconnect the keyboard from the computer and reconnect it to the same port. If you have a USB keyboard, you may also want to try a different USB port to isolate the issue.

How do I get keyboard inputs on my screen?

Open the keyboardAt the bottom right, select the time. Accessibility. Under "Keyboard and text input," turn on On-screen keyboard.


2 Answers

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;
like image 137
Andreas Rejbrand Avatar answered Oct 10 '22 16:10

Andreas Rejbrand


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;
like image 32
Lars Truijens Avatar answered Oct 10 '22 16:10

Lars Truijens