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?
Click all the buttons on your mouse and check if they light up on the mouse illustration. Point your mouse cursor at the mouse illustration and then spin the scroll wheel on your mouse up and down. Check if the arrows on the illustration also light up.
Ans. Pressing the left mouse button once is called single click. 3.
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.
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