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;
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.