xtd 0.2.0
Loading...
Searching...
No Matches
console_read_key.cpp

Shows how to use xtd::console::read_key and xtd::console::key_available methods.

#include <xtd/as>
#include <xtd/console>
using namespace xtd;
auto main() -> int {
console::cancel_key_press += [](xtd::console_cancel_event_args & e) {
console::write_line("Signal {0} intercepted !", e.special_key());
e.cancel(true);
};
while (true) {
if (console::key_available()) {
auto key_info = console::read_key(true);
console::write_line("Key = {} ({}), Char = '{}' ({}), Modifiers = {}", key_info.key(), as<int>(key_info.key()), key_info.key_char(), as<int>(key_info.key_char()), key_info.modifiers());
if (key_info.key() == console_key::escape) break;
}
}
}
// This code produces the following output if user stroke 'q', 'w', 'e', 'r', 't', 'y', Ctrl+C and Escape keys:
//
// Key = q (81), Char = 'q' (113), Modifiers = 0
// Key = w (87), Char = 'w' (119), Modifiers = 0
// Key = e (69), Char = 'e' (101), Modifiers = 0
// Key = r (82), Char = 'r' (114), Modifiers = 0
// Key = t (84), Char = 't' (116), Modifiers = 0
// Key = y (89), Char = 'y' (121), Modifiers = 0
// Signal control_c intercepted !
// Key = escape (27), Char = ' (27), Modifiers = 0
Provides data for the console::cancel_key_press event. This class cannot be inherited.
Definition console_cancel_event_args.h:22
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10