Search code examples
pythonmacosncursescursespython-curses

Python curses mouse event on Mac OS X


I am attempting to retrieve a mouse event via python curses running on Mac OS X 10.10.3. I have been following another question (Python curses.getmouse()) which has a simple example, but it doesn't produce the expected results for me. I think this has something to do with my Terminal and/or OS, but I am unsure.

Here is the code I am working with:

import curses 

screen = curses.initscr() 
#curses.noecho() 
curses.curs_set(0) 
screen.keypad(1) 
curses.mousemask(1)

screen.addstr("This is a Sample Curses Script\n\n") 

while True:
    event = screen.getch() 
    if event == ord("q"):
        screen.addstr(20, 10, "Q") 
    if event == curses.KEY_MOUSE:
        a = curses.getmouse()
        screen.addstr(20, 10, a)
    screen.refresh()

curses.endwin()

Does anyone have any idea why the curses.KEY_MOUSE event never seems to come up?

Edit: I have already checked the return value of mousemask, and when it's run initially it returns the tuple (1, 0), as expected.


Solution

  • As noted, Terminal.app does not itself support mouse events. This question has been asked before, with the suggestion to use another terminal emulator. Alternatively, there is at least one add-on programs which can help, as noted in Does OS X's terminal app support mouse? suggests iTerm and iTerm2, but also mentions MouseTerm (an add-on). The same information is given in Mac OS X Terminal: mouse support?

    The original question was asked and answered in June 2015. Apple provided a more up-to-date Terminal.app, seen in El Capitan OSX (later renamed to macOS) 10.11, later that year, documented in ncurses as nsterm-build361 (see note for October 2015).