Thread: Can't Capture Mouse Events with NCurses

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    7

    Can't Capture Mouse Events with NCurses

    Greetings Programs,
    I hope this is the right place to post this question. I have been playing around with ncurses and I'm writing a small test program to test out the capabilities of ncurses. Everything seems to be fine until I start trying to use the mouse in my programs.

    I'm using the default terminal xterm-256color on my mac book. When I echo $TERM I get xterm-256color and when I echo $TERMINFO I get /usr/share/terminfo

    Seems fine to me. I'm not sure, but I searched the capabilities of xterm-256color and it shows over at wikipedia that it supports colors, mouse and many other capabilities.
    Following is my simple code

    Code:
    #include <ncurses.h>
    int main()
    { 
       initscr(); 
       cbreak(); 
       noecho(); 
       mousemask(ALL_MOUSE_EVENTS, NULL); 
       int mouse_input = wgetch(stdscr); 
       endwin(); 
       return 0;
    }

    This progy is written in C. I use gcc to compile and gdb to debug.
    Compile command: cc -Wall -g -lncurses ncurses_simple_mouse_demo.c -o ncurses_simple_mouse_demoWhen I debug I break at endwin() and run the program. wgetch(stdscr) doesn't seem to recognise any clicks but only recognises keyboard input!I'm not sure if this is right...I hope you guys can help me out...thank you in advance

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    You need to use the keypad mode. Add
    Code:
        keypad(stdscr, TRUE);
    before the mousemask() line. See the documentation for details.

    An example program:
    Code:
    #include <curses.h>
    #include <stdio.h>
    
    int main()
    {
        char   buffer[1024];
        MEVENT event;
        int    c;
    
        initscr();
        cbreak();
        noecho();
        curs_set(0); /* Invisible cursor */
        halfdelay(1); /* Don't wait for more than 1/10 seconds for a keypress */
        keypad(stdscr, TRUE); /* Enable keypad mode */
        mousemask(ALL_MOUSE_EVENTS, NULL); /* Report all mouse events */
    
        while (1) {
    
            c = wgetch(stdscr);
    
            /* Enter or newline exits the program. */
            if (c == '\r' || c == '\n')
                break;
    
            /* No event? */
            if (c == ERR)
                sprintf(buffer, "Nothing happened.");
            else
            if (KEY_MOUSE == c) {
                /* Mouse event. */
                if (OK == getmouse(&event))
                    sprintf(buffer, "Mouse at row %3d, column %3d: 0x%08lx", event.y, event.x, (unsigned long)event.bstate);
                else
                    sprintf(buffer, "Bad mouse event.");
            } else
                sprintf(buffer, "Key '%s' (0x%04x = %d) pressed.", keyname(c), c, c);
    
            move(0, 0);
            insertln();
            addstr(buffer);
            clrtoeol();
            move(0, 0);
        }
    
        endwin();
    
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    Thank you Nominal Animal...I have tried running your code and it keeps showing me "Nothing Happened" although I keep pressing the mouse left button everywhere on the terminal screen...I really appreciate your code...thanks

  4. #4
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    That is strange. I did verify the test program in both xterm-271-1ubuntu2.1 and xfce4-terminal-0.4.8-1ubuntu1, with the test program compiled using libncurses5-5.9-4, on Xubuntu 12.04 (locally), and on CentOS 5 (remotely), and it worked like a charm.

    Could you please change cbreak() to raw() and retry?

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    Quote Originally Posted by Nominal Animal View Post
    That is strange. I did verify the test program in both xterm-271-1ubuntu2.1 and xfce4-terminal-0.4.8-1ubuntu1, with the test program compiled using libncurses5-5.9-4, on Xubuntu 12.04 (locally), and on CentOS 5 (remotely), and it worked like a charm.

    Could you please change cbreak() to raw() and retry?
    I have tried so, but still with no mouse events captured

    I'm guessing your code is perfectly correct, however since i'm using xterm-256color on my macbook then there must be something wrongly customised in my terminal settings. I'm not sure how I can setup my environment correctly to use ncurses ... Thanks
    Last edited by moeabdol; 11-13-2012 at 04:53 AM.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    I'm actually installing linux on a virtual machine right now to test the code on that...I have a strong feeling that things will work by default on linux...I'll let you know of the test results...thanks again

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    just to confirm that I have tested this on Cent OS 6.3 and your source code works like a charm....i'll try looking into the terminal settings in Cent OS and mimic the same on OSX...peace

  8. #8
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    I think I found the problem. Mac OS X Terminal requires you to press the Alt or Option key while clicking, for Terminal to recognize and deliver mouse events to terminal applications. (And the xterm in Mac OS X needs X11 support for mouse events.) You could try iTerm2. Its GPL (free and libre), and seems to have better mouse support.

    I don't use Apple products, so I might be wrong, though.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    Quote Originally Posted by Nominal Animal View Post
    I think I found the problem. Mac OS X Terminal requires you to press the Alt or Option key while clicking, for Terminal to recognize and deliver mouse events to terminal applications. (And the xterm in Mac OS X needs X11 support for mouse events.) You could try iTerm2. Its GPL (free and libre), and seems to have better mouse support.

    I don't use Apple products, so I might be wrong, though.
    Yes! It captures the mouse keys when I press the option key while I click the mouse...this was on the mac osx xterm-256color terminal...I can't thank you enough Nominal Animal ありがとございます 

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    I downloaded iTerm2 as you have suggested and it's super awesome! I think this will be my new default terminal... Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mouse Events
    By tezcatlipooca in forum Windows Programming
    Replies: 2
    Last Post: 01-24-2008, 02:33 PM
  2. Mouse Events
    By disruptor108 in forum C# Programming
    Replies: 0
    Last Post: 01-23-2007, 11:40 PM
  3. MFC Mouse Events
    By spacecadet23 in forum Windows Programming
    Replies: 0
    Last Post: 09-10-2006, 09:01 PM
  4. using the mouse with ncurses
    By dsharp in forum Linux Programming
    Replies: 2
    Last Post: 12-24-2003, 05:35 PM
  5. Mouse Events
    By Jubba in forum Windows Programming
    Replies: 1
    Last Post: 10-30-2003, 12:03 PM

Tags for this Thread