C: Trapping two mouse buttons at the same time

From FSDeveloper Wiki
Jump to navigationJump to search

Detecting multiple mouse actions isn’t quite as easy as it would appear. The natural thing would be to do this:

 if((mouse_flags & MOUSE_LEFTSINGLE)&&(mouse_flags & MOUSE_RIGHTSINGLE))

but the value of mouse_flags is a reflection of action, not state. One answer is to use variables to detect the mouse button actions.

 // Add these to a global variable file
 bool bBtnLeft=false;
 bool bBtnRight=false;
 bool bBtnMid=false;
 int iBtnStatus=0;
// Inside the mouse callback
BOOL FSAPI myMouse_mcb( PPIXPOINT relative_point, FLAGS32 mouse_flags )
{
  static int initbtn = 0; // Used to detect which button is pressed first
 
  if (mouse_flags & MOUSE_LEFTSINGLE)
  {
    bBtnLeft = true;
    if (!initbtn)initbtn = 1;	// First button pressed - left
  }
  else if (mouse_flags & MOUSE_LEFTRELEASE)bBtnLeft = false;
 
  if (mouse_flags & MOUSE_RIGHTSINGLE)
  {
    bBtnRight = true;
    if (!initbtn)initbtn = 2;	// First button pressed - right
  }
  else if (mouse_flags & MOUSE_RIGHTRELEASE)bBtnRight = false;
 
  // Button down status
  if (bBtnLeft == false && bBtnRight == false)iBtnStatus = 0, initbtn = 0;  // Neither
  if (bBtnLeft == true && bBtnRight == false)iBtnStatus = 1;                // Left
  if (bBtnLeft == false && bBtnRight == true)iBtnStatus = 2;                // Right
  if (bBtnLeft == true && bBtnRight == true)iBtnStatus = 3;                 // Both
 
  // Action code here
  // Mouse buttons released
  if(!iBtnStatus)
  {
  }
  else
  // Left mouse button only
  if(iBtnStatus==1)
  {
  }
  else
  // Right mouse button only
  if(iBtnStatus==2)
  {
  }
  else
  // Both mouse buttons pressed
  if (iBtnStatus==3)
  {
    if(initbtn==1)	// Left mouse button was pressed first
    {
    }
    if(initbtn==2)	// Right mouse button was pressed first
    {
    }		
  }
}