Windows 控制台中的 Ctrl-S 输入事件与 ReadConsoleInputW
Ctrl-S input event in Windows console with ReadConsoleInputW
我正在使用 ReadConsoleInputW
读取 Windows 10 个控制台输入。我希望能够检测何时按下 Ctrl+S。使用我拥有的代码,我可以毫无问题地检测到 Ctrl+Q,但是我没有看到任何 Ctrl+S。 Ctrl+S 能检测到吗?
下面是我按Ctrl+S几次后读到的INPUT_RECORD
的顺序,接着是Ctrl+Q.
Key { key_down: true, repeat_count: 1, key_code: 17, scan_code: 29, wide_char: 0, control_key_state: 40 }
Key { key_down: true, repeat_count: 1, key_code: 17, scan_code: 29, wide_char: 0, control_key_state: 40 }
Key { key_down: true, repeat_count: 1, key_code: 17, scan_code: 29, wide_char: 0, control_key_state: 40 }
Key { key_down: true, repeat_count: 1, key_code: 17, scan_code: 29, wide_char: 0, control_key_state: 40 }
Key { key_down: true, repeat_count: 1, key_code: 17, scan_code: 29, wide_char: 0, control_key_state: 40 }
Key { key_down: true, repeat_count: 1, key_code: 17, scan_code: 29, wide_char: 0, control_key_state: 40 }
Key { key_down: true, repeat_count: 1, key_code: 17, scan_code: 29, wide_char: 0, control_key_state: 40 }
Key { key_down: true, repeat_count: 1, key_code: 17, scan_code: 29, wide_char: 0, control_key_state: 40 }
Key { key_down: true, repeat_count: 1, key_code: 81, scan_code: 16, wide_char: 17, control_key_state: 40 }
如果重要的话,这是在 Rust 中使用 wio
。
使用 ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS
作为第二个参数调用 SetConsoleMode
(因此禁用 ENABLE_PROCESSED_INPUT
)成功了。
oconnor0 的回答帮助我找到了解决方案。
但是,我无法通过禁用 ENABLE_PROCESSED_INPUT
来获得 ctrl-s 事件,所以我尝试按照 oconnor0 的建议仅使用 ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS
。这有效,但这意味着 ENABLE_PROCESSED_INPUT
不是罪魁祸首!
所以我尝试了:
//This didn't work
if (!GetConsoleMode(hConsoleInput, &lpMode)) Error();
lpMode &= ~(ENABLE_PROCESSED_INPUT);
if (!SetConsoleMode(hConsoleInput, lpMode)) Error();
//This worked
if (!GetConsoleMode(hConsoleInput, &lpMode)) Error();
lpMode &= ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT);
if (!SetConsoleMode(hConsoleInput, lpMode)) Error();
禁用 ENABLE_ECHO_INPUT
会强制您禁用 ENABLE_ECHO_INPUT
(请参阅 msdn),但这不是罪魁祸首,因为:
//This didn't work either
if (!GetConsoleMode(hConsoleInput, &lpMode)) Error();
lpMode &= ~(ENABLE_PROCESSED_INPUT | ENABLE_ECHO_INPUT);
if (!SetConsoleMode(hConsoleInput, lpMode)) Error();
所以这意味着ENABLE_LINE_INPUT
是罪魁祸首!
虽然还不清楚为什么:
ENABLE_LINE_INPUT 0x0002 The ReadFile or ReadConsole function returns
only when a carriage return character is read. If this mode is
disabled, the functions return when one or more characters are
available.
我正在使用 ReadConsoleInputW
读取 Windows 10 个控制台输入。我希望能够检测何时按下 Ctrl+S。使用我拥有的代码,我可以毫无问题地检测到 Ctrl+Q,但是我没有看到任何 Ctrl+S。 Ctrl+S 能检测到吗?
下面是我按Ctrl+S几次后读到的INPUT_RECORD
的顺序,接着是Ctrl+Q.
Key { key_down: true, repeat_count: 1, key_code: 17, scan_code: 29, wide_char: 0, control_key_state: 40 }
Key { key_down: true, repeat_count: 1, key_code: 17, scan_code: 29, wide_char: 0, control_key_state: 40 }
Key { key_down: true, repeat_count: 1, key_code: 17, scan_code: 29, wide_char: 0, control_key_state: 40 }
Key { key_down: true, repeat_count: 1, key_code: 17, scan_code: 29, wide_char: 0, control_key_state: 40 }
Key { key_down: true, repeat_count: 1, key_code: 17, scan_code: 29, wide_char: 0, control_key_state: 40 }
Key { key_down: true, repeat_count: 1, key_code: 17, scan_code: 29, wide_char: 0, control_key_state: 40 }
Key { key_down: true, repeat_count: 1, key_code: 17, scan_code: 29, wide_char: 0, control_key_state: 40 }
Key { key_down: true, repeat_count: 1, key_code: 17, scan_code: 29, wide_char: 0, control_key_state: 40 }
Key { key_down: true, repeat_count: 1, key_code: 81, scan_code: 16, wide_char: 17, control_key_state: 40 }
如果重要的话,这是在 Rust 中使用 wio
。
使用 ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS
作为第二个参数调用 SetConsoleMode
(因此禁用 ENABLE_PROCESSED_INPUT
)成功了。
oconnor0 的回答帮助我找到了解决方案。
但是,我无法通过禁用 ENABLE_PROCESSED_INPUT
来获得 ctrl-s 事件,所以我尝试按照 oconnor0 的建议仅使用 ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS
。这有效,但这意味着 ENABLE_PROCESSED_INPUT
不是罪魁祸首!
所以我尝试了:
//This didn't work
if (!GetConsoleMode(hConsoleInput, &lpMode)) Error();
lpMode &= ~(ENABLE_PROCESSED_INPUT);
if (!SetConsoleMode(hConsoleInput, lpMode)) Error();
//This worked
if (!GetConsoleMode(hConsoleInput, &lpMode)) Error();
lpMode &= ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT);
if (!SetConsoleMode(hConsoleInput, lpMode)) Error();
禁用 ENABLE_ECHO_INPUT
会强制您禁用 ENABLE_ECHO_INPUT
(请参阅 msdn),但这不是罪魁祸首,因为:
//This didn't work either
if (!GetConsoleMode(hConsoleInput, &lpMode)) Error();
lpMode &= ~(ENABLE_PROCESSED_INPUT | ENABLE_ECHO_INPUT);
if (!SetConsoleMode(hConsoleInput, lpMode)) Error();
所以这意味着ENABLE_LINE_INPUT
是罪魁祸首!
虽然还不清楚为什么:
ENABLE_LINE_INPUT 0x0002 The ReadFile or ReadConsole function returns only when a carriage return character is read. If this mode is disabled, the functions return when one or more characters are available.