求代码 console中只能输入Y或N,无法输入其它的字符,该如何解决
求代码 console中只能输入Y或N,无法输入其它的字符
求C++代码 console中比如问是否确定,然后只能输入Y或N,无法输入其它的字符,按其它的按键都不会打上去。谢谢
------解决方案--------------------
#include <windows.h>
#include <iostream>
using namespace std;
int getYorN()
{
static HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
INPUT_RECORD ir;
DWORD n;
while (1)
{
ReadConsoleInput(hIn, &ir, 1, &n);
while( ir.EventType != KEY_EVENT || !ir.Event.KeyEvent.bKeyDown )
ReadConsoleInput(hIn, &ir, 1, &n);
FlushConsoleInputBuffer(hIn);
WORD vk = ir.Event.KeyEvent.wVirtualKeyCode;
WORD vs = ir.Event.KeyEvent.wVirtualScanCode;
WORD ck = ir.Event.KeyEvent.dwControlKeyState;
if( vk == 'Y ' || vk == 'N ' )
return vk;
}
return 0;
}
int main()
{
cout < < "yes or no: ";
char c = getYorN();
cout < < "[ " < < c < < "] " < < endl;
}
求C++代码 console中比如问是否确定,然后只能输入Y或N,无法输入其它的字符,按其它的按键都不会打上去。谢谢
------解决方案--------------------
#include <windows.h>
#include <iostream>
using namespace std;
int getYorN()
{
static HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
INPUT_RECORD ir;
DWORD n;
while (1)
{
ReadConsoleInput(hIn, &ir, 1, &n);
while( ir.EventType != KEY_EVENT || !ir.Event.KeyEvent.bKeyDown )
ReadConsoleInput(hIn, &ir, 1, &n);
FlushConsoleInputBuffer(hIn);
WORD vk = ir.Event.KeyEvent.wVirtualKeyCode;
WORD vs = ir.Event.KeyEvent.wVirtualScanCode;
WORD ck = ir.Event.KeyEvent.dwControlKeyState;
if( vk == 'Y ' || vk == 'N ' )
return vk;
}
return 0;
}
int main()
{
cout < < "yes or no: ";
char c = getYorN();
cout < < "[ " < < c < < "] " < < endl;
}