检测在表单显示之前是否已按下鼠标按钮

发布于 2025-01-08 14:44:28 字数 81 浏览 2 评论 0原文

如果按下鼠标按钮并显示一个窗口,则释放鼠标按钮时该窗口将收到 MouseUp 事件。

一旦显示窗口,是否可以检测鼠标按钮是否已按下?

If a mouse button is pressed and a window is shown that window will receive the MouseUp event when the mouse button is released.

Is it possible to detect, once the window is shown, whether or not a mouse button is already pressed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴情换悲伤 2025-01-15 14:44:28

我会尝试这个:

procedure TForm1.FormShow(Sender: TObject);
begin
  if GetKeyState(VK_LBUTTON) and $8000 <> 0 then
    ShowMessage('Left mouse button is pressed...')
  else
    ShowMessage('Left mouse button is not pressed...')
end;

I would try this:

procedure TForm1.FormShow(Sender: TObject);
begin
  if GetKeyState(VK_LBUTTON) and $8000 <> 0 then
    ShowMessage('Left mouse button is pressed...')
  else
    ShowMessage('Left mouse button is not pressed...')
end;
围归者 2025-01-15 14:44:28

要直接回答您的问题,您可以使用 < 测试鼠标按钮状态代码>GetKeyStateGetAsyncKeyState。您需要的虚拟按键代码是VK_LBUTTON

它们之间的区别在于,GetKeyState 报告当前活动排队消息发布到队列时的状态。另一方面,GetAsynchKeyState 为您提供调用 GetAsynchKeyState 时的状态。

来自 GetKeyState

当线程从其消息队列中读取关键消息时,从此函数返回的关键状态会发生变化。该状态不反映与硬件关联的中断级状态。使用 GetAsyncKeyState 函数检索该信息。
应用程序调用 GetKeyState 来响应键盘输入消息。此函数检索生成输入消息时按键的状态。

我怀疑您应该使用 GetKeyState 但我不能 100% 确定,因为我实际上并不知道您想用此信息实现什么目的。

To answer your question directly, you can test for mouse button state with GetKeyState or GetAsyncKeyState. The virtual key code you need is VK_LBUTTON.

The difference between these is that GetKeyState reports the state at the time that the currently active queued message was posted to your queue. On the other hand, GetAsynchKeyState gives you the state at the instant that you call GetAsynchKeyState.

From the documentation of GetKeyState:

The key status returned from this function changes as a thread reads key messages from its message queue. The status does not reflect the interrupt-level state associated with the hardware. Use the GetAsyncKeyState function to retrieve that information.
An application calls GetKeyState in response to a keyboard-input message. This function retrieves the state of the key when the input message was generated.

I suspect that you should be using GetKeyState but I can't be 100% sure because I don't actually know what you are trying to achieve with this information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文