该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
最新的Win10SDK里面直接附带了,之前的有没有没注意过.
附一段代码吧, 应该只能用MSC编译,
extern "C" int __cdecl _getch()
{
__acrt_lock(__acrt_conio_lock);
int result = 0;
__try
{
result = _getch_nolock();
}
__finally
{
__acrt_unlock(__acrt_conio_lock);
}
return result;
}
extern "C" int __cdecl _getch_nolock()
{
// Check the pushback buffer for a character. If one is present, return it:
if (chbuf != EOF)
{
int const c = static_cast(chbuf & 0xff);
chbuf = EOF;
return c;
}
if (__dcrt_lowio_console_input_handle == -2)
__dcrt_lowio_initialize_console_input();
if (__dcrt_lowio_console_input_handle == -1)
return EOF;
HANDLE const console_handle = reinterpret_cast(__dcrt_lowio_console_input_handle);
// Switch console to raw mode:
DWORD old_console_mode;
GetConsoleMode(console_handle, &old_console_mode);
SetConsoleMode(console_handle, 0);
int result = 0;
__try
{
for ( ; ; )
{
// Get a console input event:
INPUT_RECORD input_record;
DWORD num_read;
if (!ReadConsoleInput(console_handle, &input_record, 1, &num_read) || num_read == 0)
{
result = EOF;
__leave;
}
// Look for, and decipher, key events.
if (input_record.EventType == KEY_EVENT && input_record.Event.KeyEvent.bKeyDown)
{
// Easy case: if UnicodeChar is non-zero, we can just return it:
unsigned char const c = static_cast(input_record.Event.KeyEvent.uChar.AsciiChar);
if (c != 0)
{
result = c;
__leave;
}
// Hard case: either it is an extended code or an event which
// should not be recognized. Let _getextendedkeycode do the work:
CharPair const* const cp = _getextendedkeycode(&input_record.Event.KeyEvent);
if (cp != nullptr)
{
chbuf = cp->SecondChar;
result = cp->LeadChar;
__leave;
}
}
}
}
__finally
{
// Restore the previous console mode:
SetConsoleMode(console_handle, old_console_mode);
}
return result;
}