使用ncurses捕获控制台应用程序上的鼠标单击 [英] Using ncurses to capture mouse clicks on a console application

查看:148
本文介绍了使用ncurses捕获控制台应用程序上的鼠标单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为unix平台制作一个控制台应用程序,并且正在使用curses(或ncurses)库来处理键盘和鼠标输入.问题是我从,没有非常详细的示例. 我已经设法捕获了左键单击,但是我无法使它适用于右键单击,因为终端仿真器的选项菜单出现在光标位置,但是该事件未由应用程序处理.如何避免这种情况,并在应用程序中捕获事件?

I'm making a console application for unix platforms, and I'm using the curses (or ncurses) library to handle keyboard and mouse input. The problem is that I've found very little documentation on how exactly to use it for that, appart from this page and this one, which don't have very detailed examples. I've managed to capture the left click, but I can't get it to work for the right click because the options menu for the terminal emulator appears at the cursor location, but the event is not processed by the application. How can I avoid this and have the event captured in the application?

我有以下几行用于配置鼠标事件:

I have the following line for the configuration of mouse events:

// Set up mouse event throwing
mousemask(BUTTON1_PRESSED | BUTTON2_PRESSED, NULL);

在处理输入的方法中,我有以下内容:

And in the method that processes input, I have the following:

int c = getch();
MEVENT event;
switch(c)
{
    case KEY_UP:
        ... do stuff
        break;
    case KEY_DOWN:
        ... do stuff
        break;
    case KEY_MOUSE:
        if(getmouse(&event) == OK)
        {
            if(event.bstate & BUTTON1_PRESSED) // This works for left-click
            {
                ... do stuff
            }
            else if(event.bstate & BUTTON2_PRESSED) // This doesn't capture right-click
            {
                ... do other stuff
            }
            else
                fprintf(stderr, "Event: %i", event.bstate); // Doesn't print anything on right-click
        }
        break;
    default:
        return;
}

我也尝试过使用ALL_MOUSE_EVENTS掩码配置mousemask(),但是它仍然不会在最后一个else子句上显示任何事件,因此我认为该事件根本没有触发. 在这方面的任何帮助将不胜感激.

I've also tried configuring mousemask() with the ALL_MOUSE_EVENTS mask, but it still doesn't print any events on the last else clause, so I figure the event simply isn't triggering. Any help on this would be much appreciated.

推荐答案

对于其他来这里的人来说,为什么他/他根本无法捕获Ncurses的鼠标事件这是您需要的行:

For anyone else coming here trying to figure out why s/he can't capture mouse events at all with Ncurses, most likely this is the line that you need:

keypad(window, TRUE);      

没有这个,我没有通过getch()遇到任何鼠标事件.

Without this, I didn't get any mouse events with getch().

我见过的所有教程/示例都缺少它,这就是为什么我花了很多时间弄清楚我的代码出了什么问题-也许这个答案将比其他人更快地找到解决方案.

It's missing from all the tutorials/examples I've seen, that's why it took me a lot of time to figure out what was wrong with my code - maybe this answer will help others find the solution faster than I did.

这篇关于使用ncurses捕获控制台应用程序上的鼠标单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆