Add support for dispatching key events through the public platform API

This change adds `KeyPress` and `KeyRelease` variants to the
`WindowEvent` enum, along with the new `slint::Key` enum, that allows
encoding keys.
This commit is contained in:
Florian Blasius 2022-10-19 14:53:38 +02:00 committed by Simon Hausmann
parent e3838543fe
commit 61c39b5fa1
36 changed files with 681 additions and 315 deletions

View file

@ -1005,15 +1005,25 @@ namespace slint::testing {
using cbindgen_private::KeyboardModifiers;
/// Send a key events to the given component instance
inline void send_keyboard_char(const slint::interpreter::ComponentInstance *component,
const slint::SharedString &str, bool pressed)
{
const cbindgen_private::WindowAdapterRcOpaque *win_ptr = nullptr;
cbindgen_private::slint_interpreter_component_instance_window(
reinterpret_cast<const cbindgen_private::ErasedComponentBox *>(component), &win_ptr);
cbindgen_private::slint_send_keyboard_char(
&str, pressed, reinterpret_cast<const cbindgen_private::WindowAdapterRc *>(win_ptr));
}
/// Send a key events to the given component instance
inline void send_keyboard_string_sequence(const slint::interpreter::ComponentInstance *component,
const slint::SharedString &str,
KeyboardModifiers modifiers = {})
const slint::SharedString &str)
{
const cbindgen_private::WindowAdapterRcOpaque *win_ptr = nullptr;
cbindgen_private::slint_interpreter_component_instance_window(
reinterpret_cast<const cbindgen_private::ErasedComponentBox *>(component), &win_ptr);
cbindgen_private::send_keyboard_string_sequence(
&str, modifiers, reinterpret_cast<const cbindgen_private::WindowAdapterRc *>(win_ptr));
&str, reinterpret_cast<const cbindgen_private::WindowAdapterRc *>(win_ptr));
}
}

View file

@ -24,12 +24,17 @@ inline void send_mouse_click(const Component *component, float x, float y)
}
template<typename Component>
inline void send_keyboard_string_sequence(const Component *component,
const slint::SharedString &str,
cbindgen_private::KeyboardModifiers modifiers = {})
inline void send_keyboard_char(const Component *component, const slint::SharedString &str,
bool pressed)
{
cbindgen_private::send_keyboard_string_sequence(&str, modifiers,
&component->m_window.window_handle());
cbindgen_private::slint_send_keyboard_char(&str, pressed, &component->m_window.window_handle());
}
template<typename Component>
inline void send_keyboard_string_sequence(const Component *component,
const slint::SharedString &str)
{
cbindgen_private::send_keyboard_string_sequence(&str, &component->m_window.window_handle());
}
#define assert_eq(A, B) \

View file

@ -478,7 +478,9 @@ SCENARIO("Send key events")
property <string> result;
scope := FocusScope {
key-pressed(event) => {
result += event.text;
if (event.text != Key.Shift && event.text != Key.Control) {
result += event.text;
}
return accept;
}
}
@ -487,7 +489,7 @@ SCENARIO("Send key events")
"");
REQUIRE(comp_def.has_value());
auto instance = comp_def->create();
slint::testing::send_keyboard_string_sequence(&*instance, "Hello keys!", {});
slint::testing::send_keyboard_string_sequence(&*instance, "Hello keys!");
REQUIRE(*instance->get_property("result")->to_string() == "Hello keys!");
}