xtd 0.2.0
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::write_line("Signal {0} intercepted !", e.special_key());
e.cancel(true);
};
while (true) {
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.hpp:22
static event< console, console_cancel_event_handler > cancel_key_press
Occurs when the Control modifier key (Ctrl) and either the ConsoleKey.C console key (C) or the Break ...
Definition console.hpp:335
static console_key_info read_key()
Obtains the next character or function key pressed by the user. The pressed key is displayed in the c...
static bool key_available()
Gets a value indicating whether a key press is available in the input stream.
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
type_t as(any_object &o)
Casts a type into another type.
Definition __as_any_object.hpp:59
@ escape
The ESC (ESCAPE) key.
Definition console_key.hpp:34
@ e
The E key.
Definition console_key.hpp:96
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8