Using ncurses to capture mouse clicks on a console application

流过昼夜 提交于 2019-12-05 02:11:25

For anyone else coming here trying to figure out why s/he can't capure mouse events at all with Ncurses, most likely this is the line that you need:

keypad(window, TRUE);

Without this, I didn't get any mouse events with getch().

It's missing from all the tutorials/examples I've seen, that's why it took me a lot of time to figure out what was wrong with my code - maybe this answer will help others find the solution faster than I did.

The right mouse button is button 3, not button 2. Button 2 is the middle one.

I was using your code but I can't get any reaction. Not even the left mousebutton works.

Is this you full code?

#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; 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!