coder
coder

Reputation: 183

how to make a terminal prog react to mouse clicks

I am writing a c program to do some calculations.It would really help me if I was able to get responses by clicks of mouse.

How can i do this also If it is not possible then using which functions or libraries of C only would I be able to do that.

Upvotes: 2

Views: 920

Answers (1)

Andrejs Cainikovs
Andrejs Cainikovs

Reputation: 28474

Ncurses has support for GPM (mouse library).

Excerpt from Ncurses interfacing with the mouse how-to:

Once a class of mouse events have been enabled, getch() class of functions return KEY_MOUSE every time some mouse event happens. Then the mouse event can be retrieved with getmouse().

The code approximately looks like this:

MEVENT event;

ch = getch();
if(ch == KEY_MOUSE)
    if(getmouse(&event) == OK)
        .    /* Do some thing with the event */
        .
        .

getmouse() returns the event into the pointer given to it. It's a structure which contains

typedef struct
{
    short id;         /* ID to distinguish multiple devices */
    int x, y, z;      /* event coordinates */
    mmask_t bstate;   /* button state bits */
}    

The bstate is the main variable we are interested in. It tells the button state of the mouse.

Then with a code snippet like the following, we can find out what happened.

if(event.bstate & BUTTON1_PRESSED)
    printw("Left Button Pressed");

Upvotes: 5

Related Questions