Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Problems with the mouse

This thread is locked; no one can reply to it. rss feed Print
Problems with the mouse
arthur arthur
Member #7,336
June 2006

Hello,
first excuse my english, i'm french.

i'll try to explain my problem simply : i want to detect a mouse left click (easy), but detect just a "click" (which means mouse left button down, then released).

i've tried that :

if (mouse_b & 1)
         printf("Left button is pressed\n");

but the printf is but until the left button is released.

does anybody got an answer for me please ?

thx for all !

Niunio
Member #1,975
March 2002
avatar

Try this:

int Clicked = 0;

...
if (!Clicked && (mouse_b & 1))
         printf("Left button is pressed\n");
Clicked = mouse_b & 1;

-----------------
Current projects: Allegro.pas | MinGRo

Tobias Dammers
Member #2,604
August 2002
avatar

Hackish solution:

if (mouse_b & 1) {
  printf("Left mouse button pressed\n");
  while (mouse_b & 1)
    poll_mouse(); // not strictly necessary, but can't hurt either
}

Note that this locks your application until the mouse button is released, so a more sophisticated approach would be:

// global var:
int last_mouse_b = 0;
int cur_mouse_b = 0;

// then in main loop:
last_mouse_b = cur_mouse_b;
cur_mouse_b = mouse_b;
if ((cur_mouse_b & 1) && !(last_mouse_b & 1))
  printf("Left mouse button\n");

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Go to: