有没有办法清除Curse的事件队列?

时间:2015-04-28 14:04:44

标签: c++ pdcurses

我正在尝试从PDCurses获取鼠标位置数据,它通常有效。问题是,如果在事件检查发生之前按两次鼠标按钮,则只会从队列中弹出一个事件。这意味着第二次按下的事件仍在队列中,下次按下鼠标时,将弹出旧位置而不是最近的位置。如果这种情况持续发生,队列就会开始备份,并且报告的鼠标按下的次数越来越少。

因为我使用getch的唯一目的是鼠标事件(我使用Window的GetAsyncKeyState和我自己的键盘事件经理),我认为一个简单的解决方案就是在阅读鼠标事件后清除事件队列。

不幸的是,它似乎并不那么容易,因为我找不到任何允许清除队列的方法。

我能想到的唯一方法是使用getchnodelay设置为非阻止,然后重复使用getch,保留弹出的最后一个事件。但这似乎效率低下且不准确。

因为这可能是一个XY问题,所以这是有问题的函数:

void EventHandler::getMousePos(int& x, int& y) {
    MEVENT event;
    if (nc_getmouse(&event) == OK) {
        x = event.x, y = event.y;
    }
}

EventHandler.h:

#ifndef EVENT_HANDLER_H
#define EVENT_HANDLER_H

#include <vector>
#include <atomic>
#include <thread>
#include <functional>
#include <windows.h>
#include <WinUser.h>
#include "curses.h"

#define LEFT_MOUSE VK_LBUTTON
#define RIGHT_MOUSE VK_RBUTTON
#define MIDDLE_MOUSE VK_MBUTTON

typedef std::function<void(char)> KeyHandler;
typedef std::function<void(char,long,long)> MouseHandler;

class EventHandler {

    std::thread listeningThread;

    std::atomic<bool> listening = false;

    std::vector<char> keysToCheck;
    std::vector<char> mButtonsToCheck;

    KeyHandler keyHandler = KeyHandler();
    MouseHandler mouseHandler = MouseHandler();

    void actOnPressedKeys();

public:
    EventHandler();

    ~EventHandler();

    void setKeyHandler(KeyHandler);
    void setMouseHandler(MouseHandler);

    void setKeysToListenOn(std::vector<char>);
    void setButtonsToListenOn(std::vector<char>);

    void listenForPresses(int loopMSDelay = 100);
    void stopListening();

    static void getMousePos(int& x, int& y);

};

#endif

EventHandler.cpp:

#include "EventHandler.h"

#include <thread>
#include <stdexcept>
#include <cctype>

EventHandler::EventHandler() {

}

EventHandler::~EventHandler() {
    stopListening();
    if (listeningThread.joinable()) {
        //May need to fix this. May cause the EventHandler to freeze
        // on destruction if listeningThread can't join;
        listeningThread.join();
    }
}

void EventHandler::actOnPressedKeys() {
    for (char key : keysToCheck) {
        if (GetAsyncKeyState(key)) {
            //pressedKeys.insert(key);
            keyHandler(key);
        }
    }

    for (char button : mButtonsToCheck) {
        if ( GetAsyncKeyState(button) ) {

            int x = 0, y = 0;
            getMousePos(x, y);
            mvprintw(y, x, "Button Press Detected");
            mouseHandler(button, x, y);
        }
    }
}

/*void EventHandler::actOnPressedKeys() {
    pressedKeys.forEach([](char key){
        keyHandler(key);
    });
}*/

void EventHandler::setKeyHandler(KeyHandler handler) {
    keyHandler = handler;
}

void EventHandler::setMouseHandler(MouseHandler handler) {
    mouseHandler = handler;
}

void EventHandler::setKeysToListenOn(std::vector<char> newListenKeys) {
    if (listening) {
        throw std::runtime_error::runtime_error(
            "Cannot change the listened-on keys while listening"
        );
        //This could be changed to killing the thread by setting
        // listening to false, changing the keys, then restarting
        // the listening thread. I can't see that being necessary though.
    }

    //Untested
    for (char& key : newListenKeys) {
        if (key >= 'a' && key <= 'z') {
            key += 32;
        }
    }

    keysToCheck = newListenKeys;

}

void EventHandler::setButtonsToListenOn(std::vector<char> newListenButtons) {
    if (listening) {
        throw std::runtime_error::runtime_error(
            "Cannot change the listened-on buttons while listening"
        );
    }

    mButtonsToCheck = newListenButtons;
}

void EventHandler::listenForPresses(int loopMSDelay) {
    listening = true;
    listeningThread = std::thread ([=]{
        do {
            actOnPressedKeys();
            std::this_thread::sleep_for(std::chrono::milliseconds(loopMSDelay));
        } while (listening);

    });
}

void EventHandler::stopListening() {
    listening = false;
}

void EventHandler::getMousePos(int& x, int& y) {
    MEVENT event;
    if (nc_getmouse(&event) == OK) {
        x = event.x, y = event.y;
    }
}

0 个答案:

没有答案