【问题标题】:how to make a terminal prog react to mouse clicks如何使终端编对鼠标点击做出反应
【发布时间】:2012-01-25 07:03:45
【问题描述】:

我正在编写一个 c 程序来进行一些计算。如果我能够通过点击鼠标获得响应,那将真的对我有帮助。

我怎么能也这样做如果不可能,那么只有使用 C 的哪些函数或库我才能做到这一点。

【问题讨论】:

  • 我认为这属于 StackOverflow,它不是 Ubuntu 特定的。

标签: terminal c


【解决方案1】:

Ncurses has support for GPM (mouse library).

摘自Ncurses interfacing with the mouse how-to

一旦启用了一类鼠标事件,getch() 类函数每次发生鼠标事件时都会返回 KEY_MOUSE。然后可以使用 getmouse() 来检索鼠标事件。

代码大致如下:

MEVENT event;

ch = getch();
if(ch == KEY_MOUSE)
    if(getmouse(&event) == OK)
        .    /* Do some thing with the event */
        .
        .

getmouse() 将事件返回给它的指针。这是一个包含

的结构
typedef struct
{
    short id;         /* ID to distinguish multiple devices */
    int x, y, z;      /* event coordinates */
    mmask_t bstate;   /* button state bits */
}    

bstate 是我们感兴趣的主要变量。它告诉鼠标的按钮状态。

然后使用如下代码sn-p,我们就可以知道发生了什么。

if(event.bstate & BUTTON1_PRESSED)
    printw("Left Button Pressed");

【讨论】:

    猜你喜欢
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多