Legitimate
Legitimate

Reputation: 39

Control Terminal using the Mouse C++

So I was wondering how would I make it so that I can select a coordinate in a graph, using the mouse, similar to the effect that happens when you select a cell on the website http://demos.sftrabbit.co.uk/game-of-life/

I want to be able to use it on a Ubuntu OS. Thanks for any tips.

Upvotes: 0

Views: 784

Answers (1)

PeterT
PeterT

Reputation: 8284

I would recommend that you use one of the C libraries for this. Either curses or ncurses. For some mouse examples with ncurses, take a look at: http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/mouse.html

here's a simple example that turns every position that you click on into an 'X':

#include "ncurses.h"
#include <cstdlib>

MEVENT mev;

void quit(void)
{
    endwin();
}

int main(void)
{
  initscr();
  atexit(quit);
  clear();
  noecho();
  curs_set(0);
  cbreak();
  keypad(stdscr, TRUE);
  start_color();
  mousemask(BUTTON1_CLICKED, 0);

  mvaddstr(5, 3, "Click to turn a character into an 'X'");
  refresh();

  for(;;)
  {
    if(getch() == KEY_MOUSE && getmouse(&mev) == OK)
    {
    mvaddch(mev.y,mev.x,'X');
    refresh();
    }
  }   
  return (0);  
}

Upvotes: 3

Related Questions