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?
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.
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.
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.
Open the keyboardAt the bottom right, select the time. Accessibility. Under "Keyboard and text input," turn on On-screen keyboard.
A few things to try:
MouseDown
, call Windows.SetFocus(Handle)
. In my experience, the WinAPI function SetFocus
often works better than the VCL's SetFocus
method.WM_GETDLGCODE
message, reply with Message.Result := Message.Result or DLGC_WANTCHARS or DLGC_WANTARROWS or DLGC_WANTTAB or DLGC_WANTALLKEYS
;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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With