You are not logged in.

#1 2015-07-19 17:43:39

euclio
Member
Registered: 2015-07-19
Posts: 8

[SOLVED] Mouse events not working in ncurses?

I'm trying to write some software with ncurses, but mouse events are always reported as bad. I'm using xterm. Do I need to install something beyond ncurses? Here's some sample code that I'm using:

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

int main() {
    int ch, count=0;
    mmask_t old;

    initscr ();
    clear();
    noecho ();
    cbreak ();
    keypad (stdscr, TRUE);
    mousemask (ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, &old);

    while ((ch = getch ()) != 'q')
    {
      count++;
      if (ch == KEY_MOUSE)
      {
         MEVENT event;
         assert (getmouse (&event) == OK);
         mvprintw (0, 0, "Mouse Event!\n");
      }
      mvprintw (1, 1, "Event number %4d",count);
      refresh();
    }
}

A number of events are reported when I click, but they're not mouse events. In addition, has_mouse() always returns false.

Last edited by euclio (2015-07-20 19:38:07)

Offline

#2 2015-07-19 22:51:24

mauritiusdadd
Member
From: Benevento, Italy
Registered: 2013-10-27
Posts: 776

Re: [SOLVED] Mouse events not working in ncurses?

I think you should use getch() instead of getchar()

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

For some examples, see also http://tldp.org/HOWTO/NCURSES-Programmi … TINGEVENTS

Last edited by mauritiusdadd (2015-07-19 22:51:50)


About me - github

-- When you have eliminated the impossible, whatever remains, however improbable, must be the truth -- Spock | Sherlock Holmes

Offline

#3 2015-07-20 03:24:58

euclio
Member
Registered: 2015-07-19
Posts: 8

Re: [SOLVED] Mouse events not working in ncurses?

You're right, but I'm still having the same issues. Edited OP.

Last edited by euclio (2015-07-20 03:25:20)

Offline

#4 2015-07-20 07:28:42

mauritiusdadd
Member
From: Benevento, Italy
Registered: 2013-10-27
Posts: 776

Re: [SOLVED] Mouse events not working in ncurses?

Strange, it works fine for me... What command are you using to compile the program? Can you test the following code and paste here the output after a mouse click?

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

int main() {
  int ch, count=0;
  MEVENT event;

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

  clear();
  cbreak();

  mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);

  while ((ch = getch()) != 'q')
  {
    count++;
    mvprintw(1, 1, "Has mouse: %d", has_mouse());
    mvprintw(2, 1, "Key code: %x; mouse code:%x", ch, KEY_MOUSE);
    if (ch == KEY_MOUSE)
    {
      assert(getmouse(&event) == OK);
      mvprintw(3, 3, "Mouse Event: x=%d, y=%d z=%d",
               event.x, event.y, event.z);
      mvprintw(4, 3, "Mouse device id: %x", event.id);
      mvprintw(5, 3, "Mouse button mask: %x", event.bstate);
    }
    mvprintw(6, 1, "Event number: %4d",count);
    refresh();
  }
  endwin();
}

Also, are you using a mouse or a touchpad? Does it work if you click while pressing the Alt or Meta/Super key? Can you post the output of the following commands?

uname -a
pacman -Qi ncurses

--edit: fixed a typo (missing parenthesis)

Last edited by mauritiusdadd (2015-07-20 11:15:58)


About me - github

-- When you have eliminated the impossible, whatever remains, however improbable, must be the truth -- Spock | Sherlock Holmes

Offline

#5 2015-07-20 10:52:18

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [SOLVED] Mouse events not working in ncurses?

It also works fine for me.  I tried first in tmux within urxvt, then to ensure it wasn't a terminal issue I installed and tested in xterm and it worked fine - even when I set a couple bad 'TERM' values.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2015-07-20 19:37:51

euclio
Member
Registered: 2015-07-19
Posts: 8

Re: [SOLVED] Mouse events not working in ncurses?

Huh. Your example now works flawlessly, as well as my original example. I must have missed something. Thank you for your help.

Offline

Board footer

Powered by FluxBB