为什么我的控件不能接受键盘输入? [英] Why won't my control accept keyboard input?
问题描述
我已经构建了一个自定义控件,我正在尝试向其发送输入。它会接受鼠标输入并正确报告MouseDown,MouseMove和MouseUp,但不管什么原因,它都不会接受键盘输入。当我点击它时,它没有得到焦点,并且我按下的任何键都被解释为已经有焦点的任何控制。
这可能是非常简单的事情。我想看的第一个地方是在ControlStyle属性,但我可以看到有关键盘输入帮助文件的唯一的事情是 csNoStdEvents
,它禁用它,我的控制doesn没有那个。那么我需要做些什么才能使我的控件可以接收输入焦点? 解决方案
/ p>
- 在
MouseDown
中,调用Windows.SetFocus(Handle)
。根据我的经验,WinAPI函数SetFocus
通常比VCL的SetFocus
方法效果更好。 b - 响应
WM_GETDLGCODE
消息,回复Message.Result:= Message.Result或DLGC_WANTCHARS或DLGC_WANTARROWS或DLGC_WANTTAB或DLGC_WANTALLKEYS
;
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?
A few things to try:
- On
MouseDown
, callWindows.SetFocus(Handle)
. In my experience, the WinAPI functionSetFocus
often works better than the VCL'sSetFocus
method. - In response to the
WM_GETDLGCODE
message, reply withMessage.Result := Message.Result or DLGC_WANTCHARS or DLGC_WANTARROWS or DLGC_WANTTAB or DLGC_WANTALLKEYS
;
这篇关于为什么我的控件不能接受键盘输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!