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?
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?
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;
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.
© 2022 - 2024 — McMap. All rights reserved.
GetAsyncKeyState
returns the physical mouse button state, whilstGetKeyState
returns logical (regarding what have you set in theSwitch primary and secondary buttons
option in mouse settings). – Jacklight