Ncurses: how shorten mouse click time response?

Right now when I click a mouse button while running ncurses, it takes about half a second to respond, and clicking faster doesn't seem to help. My guess is that this is due to the forced wait to see if the click will eventually turn into a double and triple-click. It seems that there isn't any noticeable lag as the mouse wheel is extremely responsive.

How can I tell ncurses to only register clicks and presses and to not wait to do stuff? I would like to be able to rapid click and have those clicks registered. I'm thinking the solution is something to do with masking, and I've only found information about masking for ALL mouse events and not just a few.

The program below counts any mouse button click (including wheel up/down).

mouse.c:

#include <ncurses.h>
#include <assert.h>

/*

Complile with:
gcc click.c -o click -lncurses

*/

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

int ch, i = 0;

MEVENT event;

initscr();
raw();
keypad(stdscr, TRUE);
noecho();
clear();
cbreak();

mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
mvprintw(0,0, "Click any mouse button to count clicks or press 'q' to exit.", i);

while ((ch = getch()) != 'q' ){

if (ch == KEY_MOUSE){

assert(getmouse(&event) == OK);

mvprintw(0,0, "Clicks: %d\n\nPress 'q' to exit", ++i);

}
}

refresh();
endwin();
}

Hi @bathtime,

here is a simplified version that handles left & right clicks and q:

#include <ncurses.h>

int main() {
    int c, x, y;
    MEVENT event;

    initscr();
    raw();
    keypad(stdscr, TRUE);
    noecho();
    clear();
    cbreak();

    mousemask(ALL_MOUSE_EVENTS, NULL); // catch all events

    while ((c = getch()) != 'q') {
        if (c == KEY_MOUSE && getmouse(&event) == OK) {
            x = event.x, y = event.y;
            if (event.bstate & BUTTON1_PRESSED)
                mvprintw(0, 0, "left btn click at (%d,%d)\n", x, y);
            if (event.bstate & BUTTON3_PRESSED)
                mvprintw(0, 0, "right btn click at (%d,%d)\n", x, y);
            // more handling here
            refresh();
        }
    }

    endwin();
    return 0;
}

Further information you can find here: Interfacing with the mouse

1 Like

Thanks for this. I'd already looked at that guide, and it doesn't seem to go into detail about masking events—only covering the ALL_MOUSE_EVENTS mask. Also, I have BUTTON1_PRESSED and BUTTON1_CLICKED in my program, and it's still slow. I could put BUTTON1_DOUBLE_CLICKED and BUTTON1_TRIPLE_CLICKED, but then it would require 2 or 3 clicks to get the equivalent of just one execution to do stuff.

I'd like to be able to rapid click and have the program ignore double and triple clicks if possible.

Found the solution. You simply put this line in the code:

mouseinterval(0);

It should be noted that setting a value of '0' will disable any ability to use the BUTTON1_CLICKED event, but you may use BUTTON1_PRESSED and BUTTON1_RELEASED with near instant response time.

More info here (bottom of page):
https://www.gnu.org/software/guile-ncurses/manual/html_node/Mouse-handling.html

3 Likes

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.