Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if Mouse button already pressed before form shows

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?

like image 554
RobS Avatar asked Feb 23 '12 12:02

RobS


People also ask

How can I tell which button my mouse is 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.

When left mouse button is pressed and released once this action is known as?

Ans. Pressing the left mouse button once is called single click. 3.


2 Answers

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;
like image 152
TLama Avatar answered Oct 04 '22 13:10

TLama


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.

like image 38
David Heffernan Avatar answered Oct 04 '22 12:10

David Heffernan