有什么方法可以清除诅咒的事件队列吗?



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

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

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

我能想到的唯一方法是使用nodelaygetch设置为非阻塞,然后重复使用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;
}
}

PDCurses实现flushinp,它应该执行请求的操作。注释和函数(例如win32)表示

/* discard any pending keyboard or mouse input -- this is the core 
routine for flushinp() */
void PDC_flushinp(void)
{
PDC_LOG(("PDC_flushinp() - calledn"));
FlushConsoleInputBuffer(pdc_con_in);
}

顺便说一句,这个函数在大多数curses实现中都提供。这里有一个指向ncurses中手册页面的链接,这可能会有所帮助。

最新更新