如何检测没有焦点的控件中的修改键更改? [英] How to detect modifier key change in a control which doesn't have focus?

查看:56
本文介绍了如何检测没有焦点的控件中的修改键更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究从 TCustomControl 类派生的控件,该控件可以获取焦点以及内部有一些内部元素。如果用户将光标悬停在这些内部元素上,则可以选择它们,将它们移动等等,这些内部元素将突出显示。现在解决问题...

I'm working on a control derived from TCustomControl class which can get focus and which has some inner elements inside. Those inner elements are highlighted if the user hovers them with the cursor, you can select them, move them and so on. Now to the problem...

我正在使用( )(如果用户持有 CTRL ALT SHIFT 修饰符)。如果用户将鼠标悬停在元素上并按住 CTRL 键,那么我想更改鼠标光标。非常简单,您只需覆盖 KeyDown KeyUp 方法,并检查其 Key 参数等于 VK_CONTROL 。在这样的代码中:

I'm doing different actions with the (let's say) focused element if the user holds CTRL, ALT or SHIFT modifiers. What I would like is to change the mouse cursor if the user hovers the element and holds for instance CTRL key. Pretty simple, you just override the KeyDown and KeyUp methods and check if their Key parameter equals to VK_CONTROL. In code like this way:

procedure TMyCustomControl.KeyDown(var Key: Word; Shift: TShiftState);
begin
  inherited;
  if Key = VK_CONTROL then
    Screen.Cursor := crSizeAll;
end;

procedure TMyCustomControl.KeyUp(var Key: Word; Shift: TShiftState);
begin
  inherited;
  if Key = VK_CONTROL then
    Screen.Cursor := crDefault;
end;

即使那不是检查 CTRL 按下并释放键(例如,由于现有的 Shift 状态参数),它可以按预期工作,而控件具有焦点,甚至可以获取焦点,但是...

Even if that wouldn't be the best way to check if the CTRL key was pressed and released (e.g. because of the existing Shift state parameter), it works as expected whe the control has focus, which can even get, but...

我的目标是在用户将控件(或确切地说,控件中的某个元素)悬停并按住例如时更改鼠标光标 CTRL 键,即使我的控件没有焦点也是如此。可以这么说,因此只需覆盖 MouseMove 方法并在其中询问修饰符状态即可。就是这样,但是...

My aim is to change the mouse cursor when user hovers the control (or to be precise, a certain element inside it) and holds e.g. that CTRL key even when my control doesn't have focus. One can say, so just override the MouseMove method and ask for the modifier states there. And it would be the way, but...

如果用户将鼠标指针停留在我的控件上并按下并释放 CTRL 键?那不会为我的控件产生任何鼠标移动或按键事件,或者我错了吗?好吧,所以我的问题很明显...

What if the user stays with the mouse cursor over my control and press and release that CTRL key ? That won't generate any mouse move nor key press event for my control, or am I wrong ? Well, so my question is quite obvious...

如何检测修饰键更改如果控件没有焦点并且用户没有用鼠标移动?我当时在考虑这两个选项,但我希望能错过一些东西:

How can I detect modifier key changes if the control doesn't have focus and the user doesn't move with the mouse ? I was thinking about these two options, but I hope there is something I missed:


  • 键盘钩-可靠,但对我来说看起来有些过头了

  • 使用计时器对修饰符状态进行定期检查-我忍受不了延迟

因此,您如何检测当前未关注的控件的修饰键更改?

So, how would you detect modifier key changes of a control which isn't currently focused ?

推荐答案

我会写一个 WM_SETCURSOR 消息以调用 GetKeyboardState 来获取键盘状态(在Delphi中,您只需调用KeyboardStateToShiftState ),并根据该结果(以及点击测试)调用 SetCursor ,并带有适当的光标。

I would write a message handler for WM_SETCURSOR message to call GetKeyboardState to get the keyboard state (in Delphi you can just call KeyboardStateToShiftState) and based on the result of that (and the hit test) call SetCursor with the appropriate cursor.

要处理 WM_SETCURSOR ,在VCL中有一个示例: TCustomGrid.WMSetCursor 网格单元。

For handling WM_SETCURSOR, there's an example in the VCL: TCustomGrid.WMSetCursor in the Grids unit.

这篇关于如何检测没有焦点的控件中的修改键更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆