Detect mouse click in Terminal (Ubuntu server)

I want to solve a pretty easy problem. I just want to detect a mouse click under Ubuntu Server(without GUI respectively windows management). Therefore, I use Ncurses and following code snippet, but it doesn't detect any mouse actions. I looked up the web for 2 days now without success. would be great to get your help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  #include <ncurses.h>

int main(int argc, char **argv){

while(1)
{

    mousemask( ALL_MOUSE_EVENTS, NULL);
        int c = getch();
        MEVENT event;
        switch(c)
        {
            case KEY_UP:
                printf("keyup");
                break;
            case KEY_DOWN:
                printf("keydown");
                break;
            case KEY_MOUSE:
                if(getmouse(&event) == OK)
                {
                    if(event.bstate & BUTTON1_PRESSED) // This works for left-click
                    {
                        printf("button1");
                    }
                    else if(event.bstate & BUTTON2_PRESSED) // This doesn't capture right-click
                    {
                        printf("button2");
                    }
                    else
                        printf("Event: %i", event.bstate); // Doesn't print anything on right-click
                }
                break;
        }
}
return 0;
}
Last edited on
// This doesn't capture right-click

I don't think there's any sort of guarantee that "button 2" has to be right click.

I think you're supposed to use wgetch rather than getch though I can't tell for sure cause I never actually used curses. (Is it actually useful for anything other than roguelikes, vim and emacs?)
The wgetch() function is for windows use only, right? since I have no window in the terminal mode this one will not work.
The button itself doesn't make a big difference a, I tried every button and still no output.

Would be great to get more help.
ertt
Topic archived. No new replies allowed.