gpt4 book ai didi

c++ - C - 在控制台应用程序中使用鼠标和键盘,无需等待按任意键

转载 作者:行者123 更新时间:2023-11-30 17:42:09 25 4
gpt4 key购买 nike

(抱歉我的英语不好)好吧,我尝试使用 PeekConsoleInput() 并有效。但是,如果我不按任何键,这将返回零,包括电线和 ASCII 键。我希望返回鼠标光标的实际位置,而无需按任何键或移动鼠标,并且没有“零”并且无需暂停代码这是主程序的一个片段(因为非常大):

void inputhit(void)
{
#ifndef ENABLE_QUICK_EDIT_MODE
#define ENABLE_QUICK_EDIT_MODE 0x0040
#endif

//static DWORD old_mode;
static DWORD new_mode;
static DWORD readed;
static INPUT_RECORD ir[1];
static int clic;
static int key;
screen.in = GetStdHandle(STD_INPUT_HANDLE);
// GetConsoleMode(screen.in, &old_mode);

/* keep current configuration,
* but enable mouse input,
* disable quick edit mode,
* disable ctrl+c
*/
new_mode =
(DWORD) (( ENABLE_MOUSE_INPUT) & ~ENABLE_QUICK_EDIT_MODE
& ~ENABLE_PROCESSED_INPUT);
SetConsoleMode(screen.in, new_mode);

mouse_in_hit:

PeekConsoleInput(screen.in, ir, 1, &readed);
if (ir[0].EventType == KEY_EVENT) {
key = ir[0].Event.KeyEvent.uChar.AsciiChar;
if (key == 0) key = ir[0].Event.KeyEvent.wVirtualKeyCode + 255;
}

clic = (int) (ir[0].Event.MouseEvent.dwButtonState);

printf("%d %d %d %d", (int) ir[0].Event.MouseEvent.dwMousePosition.Y,
(int) ir[0].Event.MouseEvent.dwMousePosition.X,
(int) clic,
(int) key,
);
FlushConsoleInputBuffer(screen.in);
}

结果是:

当从循环中的控制台命令打开程序时,我移动鼠标,并返回一个大的坐标列表,例如:

[Y  X  Mouseclick Key ]
10 10 1 0 <--- this result when the press or moves the mouse
0 0 0 0 <--- this result when is not pressed or moved the mouse
21 13 2 0
...

我想要的是:

[Y  x  MouseClick Key ]
10 23 0 0 <-- this result when the mouse is in this position on the window (without moving it)
12 9 1 0 <-- this result when click

最佳答案

我不会为此使用 PeekConsoleInput,我会使用 GetNumberOfConsoleInputEvents 的组合来确定是否有可用的输入和 ReadConsoleInput检索并处理它。

我调整了 MSDN Example当有可用输入时进行读取/处理,并在没有可用输入时进行其他操作。我假设您正在轮询输入,并且您也可以调整它以这种方式工作。

#include <windows.h>
#include <stdio.h>
#include <vector>

#ifndef MOUSE_HWHEELED
#define MOUSE_HWHEELED 0x0008
#endif

HANDLE hStdin;
DWORD fdwSaveOldMode;

void ErrorExit(LPSTR lpszMessage)
{
fprintf(stderr, "%s\n", lpszMessage);
SetConsoleMode(hStdin, fdwSaveOldMode);
ExitProcess(0);
}

bool KeyEventProc(const KEY_EVENT_RECORD& ker)
{
printf("key %s: %c\n", ker.bKeyDown ? "pressed" : "released", ker.uChar.AsciiChar);
return ker.uChar.AsciiChar == 'q';
}

void MouseEventProc(const MOUSE_EVENT_RECORD& mer)
{
const int X = mer.dwMousePosition.X;
const int Y = mer.dwMousePosition.Y;
const int dir = static_cast<int>(mer.dwButtonState) >> 16;
switch(mer.dwEventFlags)
{
case 0:
if(mer.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
{
printf("left button press @ %d %d\n", X, Y);
}
else if(mer.dwButtonState == RIGHTMOST_BUTTON_PRESSED)
{
printf("right button press @ %d %d\n", X, Y);
}
else
{
//You'd need more code here to track releases or more buttons.
printf("other button press/release @ %d %d\n", X, Y);
}
break;
case DOUBLE_CLICK:
printf("double click @ %d %d\n", X, Y);
break;
case MOUSE_HWHEELED:
printf("horizontal mouse wheel %s\n", dir >= 0 ? "right" : "left");
break;
case MOUSE_WHEELED:
printf("vertical mouse wheel %s\n", dir >= 0 ? "up" : "down");
break;
case MOUSE_MOVED:
printf("mouse moved: %d %d\n", mer.dwMousePosition.X, mer.dwMousePosition.Y);
break;
default:
printf("unknown mouse event\n");
break;
}
}

int main()
{
hStdin = GetStdHandle(STD_INPUT_HANDLE);
if(hStdin == INVALID_HANDLE_VALUE)
ErrorExit("GetStdHandle");

if(!GetConsoleMode(hStdin, &fdwSaveOldMode))
ErrorExit("GetConsoleMode");

DWORD fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;
if(!SetConsoleMode(hStdin, fdwMode))
ErrorExit("SetConsoleMode");

std::vector<INPUT_RECORD> irInBuf;
DWORD numEventsAvailable;
DWORD lastTickCount = GetTickCount();
bool finished = false;
while(!finished)
{
GetNumberOfConsoleInputEvents(hStdin, &numEventsAvailable);
if(numEventsAvailable)
{
irInBuf.resize(numEventsAvailable);
DWORD cNumRead;
ReadConsoleInput(hStdin, &irInBuf[0], irInBuf.size(), &cNumRead);
for(DWORD i = 0; i < cNumRead; i++)
{
switch(irInBuf[i].EventType)
{
case KEY_EVENT:
finished = KeyEventProc(irInBuf[i].Event.KeyEvent);
break;
case MOUSE_EVENT:
MouseEventProc(irInBuf[i].Event.MouseEvent);
break;
default:
break;
}
}
}
else
{
if(GetTickCount() - lastTickCount > 1000)
{
lastTickCount = GetTickCount();
printf("Press 'q' to exit\n");
}
}
}
SetConsoleMode(hStdin, fdwSaveOldMode);
return 0;
}

关于c++ - C - 在控制台应用程序中使用鼠标和键盘,无需等待按任意键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20779495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com