Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Anytime Detect If Mouse Button is Down

delphi mouse button windows api

  • Please log in to reply
No replies to this topic

#1 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 22 February 2013 - 01:09 AM

Sometime you want to check whether any or particular mouse button is pressed anytime. You can use the following codes.

 

 

uses
  ...
  , Windows
  , ...
  ;

type
  TMouseBtnType = (mbLeft, mbMiddle, mbRight);

const
  MOUSE_BTN_VKEYS: Array [TMouseBtnType] of Integer = (VK_LBUTTON, VK_MBUTTON, VK_RBUTTON);

(* This function returns true when the specified mouse button is pressed *)
function IsMouseBtnDown(const AMouseBtn: TMouseBtnType): Boolean;
begin
  Result := GetAsyncKeyState(MOUSE_BTN_VKEYS[AMouseBtn])
            AND $8000 <> 0;
end;


(* This function returns true when any of the mouse button is pressed *)
function IsMouseBtnDown: Boolean;
begin
  Result := (GetAsyncKeyState(VK_LBUTTON)
             OR GetAsyncKeyState(VK_MBUTTON)
             OR GetAsyncKeyState(VK_RBUTTON)
             )
            AND $8000 <> 0;
end;

 

 

Note:

 

This code does not take into account the possibility of mouse button swapping. You know that users could swap mouse buttons (i.e. the left becoming the right and vice versa) through control panel. This is to benefit those left-handed people.

 

I will post code sample to detect if mouse buttons were swapped.

 

Edit: I posted code to detect if mouse buttons are swapped here.


Edited by LuthfiHakim, 22 February 2013 - 01:30 AM.

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download