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

◆ key_press

event<control, key_press_event_handler> xtd::forms::control::key_press

Occurs when a character. space or backspace 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
The xtd::forms::control::key_press event is not raised by non-character keys other than space and backspace; however, the non-character keys do raise the xtd::forms::control::key_down and xtd::forms::control::key_up events.
Use the xtd::forms::key_press_event_args::key_char property to sample keystrokes at run time and to consume or modify a subset of common keystrokes.
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.
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.