xtd 0.2.0
Loading...
Searching...
No Matches

◆ key_down

event<control, key_event_handler> xtd::forms::control::key_down

Occurs when a key is pressed while the xtd::forms::control has focus.

Remarks
Key events occur in the following order:
  1. xtd::forms::control::key_down event
  1. xtd::forms::control::key_press event
  1. xtd::forms::control::key_up event
To handle keyboard events only at the form level and not enable other controls to receive keyboard events, set the xtd::forms::key_press_event_args::handled property in your form's xtd::forms::control::key_press event-handling method to true. Certain keys, such as the TAB, RETURN, ESC, and arrow keys are handled by controls automatically. To have these keys raise the xtd::forms::control::key_down event, you must override the is_input_key method in each control on your form. The code for the override of the is_input_key would need to determine if one of the special keys is pressed and return a value of true. Instead of overriding the is_input_key method, you can handle the preview_key_down event and set the is_input_key property to true.
Examples
The following code example demonstrates the use of control keyboard events.
#define TRACE
#include <xtd/forms/application>
#include <xtd/forms/form>
#include <xtd/forms/trace_form>
#include <xtd/ctrace>
using namespace xtd;
using namespace xtd::forms;
class form1 : public form {
public:
form1() {
text("Key events example");
control1.dock(dock_style::fill);
control1.parent(*this);
control1.key_down += [&](object & sender, key_event_args & e) {
ctrace << string::format("key_down={{key_code={}, key_data=[{}], value=0x{:X4}, modifiers=[{}]}}", e.key_code(), e.key_data(), e.key_value(), e.modifiers()) << environment::new_line;
};
control1.key_press += [&](object & sender, key_press_event_args & e) {
ctrace << string::format("key_press={{key_char={}}}", e.key_char() == 0 ? "[none]" : string::format("'{}'", e.key_char())) << environment::new_line;
};
control1.key_up += [&](object & sender, key_event_args & e) {
ctrace << string::format("key_up={{key_code={}, key_data=[{}], value=0x{:X4}, modifiers=[{}]}}", e.key_code(), e.key_data(), e.key_value(), e.modifiers()) << environment::new_line;
if (e.modifiers() == keys::none) ctrace << environment::new_line;
};
}
private:
control control1;
};
auto main() -> int {
application::run(form1 {});
}
Represents text as a sequence of character units.
Definition basic_string.h:79
The environment class.
Definition environment.h:66
static void run()
Begins running a standard application message loop on the current thread, without a form.
Defines the base class for controls, which are components with visual representation.
Definition control.h:81
Represents a window or dialog box that makes up an application's user interface.
Definition form.h:54
Provides data for the xtd::forms::control::key_down or xtd::forms::control::key_up event.
Definition key_event_args.h:25
Provides data for the xtd::forms::control::key_press event.
Definition key_press_event_args.h:26
Represents a form that displays trace form. This class cannot be inherited.
Definition trace_form.h:36
std::ostream ctrace(nullptr)
Provides an std::ostream for xtd::diagnostics::trace.
@ e
The E key.
@ text
The xtd::forms::status_bar_panel displays text in the standard font.
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition xtd_about_box.h:12
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Remarks
For more information about handling events, see Handling and Raising Events.