mirror of
https://github.com/slint-ui/slint.git
synced 2025-09-27 20:42:25 +00:00
C++: expose special key codes constants
This commit is contained in:
parent
ad84114bae
commit
0a8f9c585a
5 changed files with 164 additions and 10 deletions
|
@ -44,8 +44,39 @@ fn enums(path: &Path) -> anyhow::Result<()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
i_slint_common::for_each_enums!(print_enums);
|
i_slint_common::for_each_enums!(print_enums);
|
||||||
|
|
||||||
writeln!(enums_pub, "}}")?;
|
writeln!(enums_pub, "}}")?;
|
||||||
writeln!(enums_priv, "}}")?;
|
writeln!(enums_priv, "}}")?;
|
||||||
|
|
||||||
|
// Print the key codes constants
|
||||||
|
// This is not an enum, but fits well in that file
|
||||||
|
writeln!(
|
||||||
|
enums_pub,
|
||||||
|
r#"
|
||||||
|
/// This namespace contains constants for each special non-printable key.
|
||||||
|
///
|
||||||
|
/// Each constant can be converted to SharedString.
|
||||||
|
/// The constants are meant to be used with the slint::Window::dispatch_key_press_event() and
|
||||||
|
/// slint::Window::dispatch_key_release_event() functions.
|
||||||
|
///
|
||||||
|
/// Example:
|
||||||
|
/// ```
|
||||||
|
/// window.dispatch_key_press_event(slint::platform::key_codes::Tab);
|
||||||
|
/// ```
|
||||||
|
namespace slint::platform::key_codes {{
|
||||||
|
"#
|
||||||
|
)?;
|
||||||
|
macro_rules! print_key_codes {
|
||||||
|
($($char:literal # $name:ident # $($qt:ident)|* # $($winit:ident)|* ;)*) => {
|
||||||
|
$(
|
||||||
|
writeln!(enums_pub, "/// A constant that represents the key code to be used in slint::Window::dispatch_key_press_event()")?;
|
||||||
|
writeln!(enums_pub, r#"constexpr std::u8string_view {} = u8"\u{:04x}";"#, stringify!($name), $char as u32)?;
|
||||||
|
)*
|
||||||
|
};
|
||||||
|
}
|
||||||
|
i_slint_common::for_each_special_keys!(print_key_codes);
|
||||||
|
writeln!(enums_pub, "}}")?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,19 +33,15 @@ struct SharedString
|
||||||
/// Creates a new SharedString from the null-terminated string pointer \a s. The underlying
|
/// Creates a new SharedString from the null-terminated string pointer \a s. The underlying
|
||||||
/// string data is copied. It is assumed that the string is UTF-8 encoded.
|
/// string data is copied. It is assumed that the string is UTF-8 encoded.
|
||||||
SharedString(const char *s) : SharedString(std::string_view(s)) { }
|
SharedString(const char *s) : SharedString(std::string_view(s)) { }
|
||||||
#if defined(__cpp_char8_t) || __cplusplus >= 202002L
|
|
||||||
/// Creates a new SharedString from the null-terminated string pointer \a s. The underlying
|
/// Creates a new SharedString from the null-terminated string pointer \a s. The underlying
|
||||||
/// string data is copied.
|
/// string data is copied.
|
||||||
SharedString(const char8_t *s) : SharedString(reinterpret_cast<const char *>(s)) { }
|
SharedString(const char8_t *s) : SharedString(reinterpret_cast<const char *>(s)) { }
|
||||||
#endif
|
|
||||||
#ifdef __cpp_lib_char8_t
|
|
||||||
/// Creates a new SharedString from the string view \a s. The underlying string data is copied.
|
/// Creates a new SharedString from the string view \a s. The underlying string data is copied.
|
||||||
SharedString(std::u8string_view s)
|
SharedString(std::u8string_view s)
|
||||||
{
|
{
|
||||||
cbindgen_private::slint_shared_string_from_bytes(
|
cbindgen_private::slint_shared_string_from_bytes(
|
||||||
this, reinterpret_cast<const char *>(s.data()), s.size());
|
this, reinterpret_cast<const char *>(s.data()), s.size());
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
/// Creates a new SharedString from \a other.
|
/// Creates a new SharedString from \a other.
|
||||||
SharedString(const SharedString &other)
|
SharedString(const SharedString &other)
|
||||||
{
|
{
|
||||||
|
|
|
@ -61,9 +61,116 @@ static slint::platform::NativeWindowHandle window_handle_for_qt_window(QWindow *
|
||||||
|
|
||||||
static slint::SharedString key_event_text(QKeyEvent *e)
|
static slint::SharedString key_event_text(QKeyEvent *e)
|
||||||
{
|
{
|
||||||
// TODO: handle special keys
|
switch (e->key()) {
|
||||||
|
case Qt::Key::Key_Backspace:
|
||||||
|
return slint::platform::key_codes::Backspace;
|
||||||
|
case Qt::Key::Key_Tab:
|
||||||
|
return slint::platform::key_codes::Tab;
|
||||||
|
case Qt::Key::Key_Enter:
|
||||||
|
case Qt::Key::Key_Return:
|
||||||
|
return slint::platform::key_codes::Return;
|
||||||
|
case Qt::Key::Key_Escape:
|
||||||
|
return slint::platform::key_codes::Escape;
|
||||||
|
case Qt::Key::Key_Backtab:
|
||||||
|
return slint::platform::key_codes::Backtab;
|
||||||
|
case Qt::Key::Key_Delete:
|
||||||
|
return slint::platform::key_codes::Delete;
|
||||||
|
case Qt::Key::Key_Shift:
|
||||||
|
return slint::platform::key_codes::Shift;
|
||||||
|
case Qt::Key::Key_Control:
|
||||||
|
return slint::platform::key_codes::Control;
|
||||||
|
case Qt::Key::Key_Alt:
|
||||||
|
return slint::platform::key_codes::Alt;
|
||||||
|
case Qt::Key::Key_AltGr:
|
||||||
|
return slint::platform::key_codes::AltGr;
|
||||||
|
case Qt::Key::Key_CapsLock:
|
||||||
|
return slint::platform::key_codes::CapsLock;
|
||||||
|
case Qt::Key::Key_Meta:
|
||||||
|
return slint::platform::key_codes::Meta;
|
||||||
|
case Qt::Key::Key_Up:
|
||||||
|
return slint::platform::key_codes::UpArrow;
|
||||||
|
case Qt::Key::Key_Down:
|
||||||
|
return slint::platform::key_codes::DownArrow;
|
||||||
|
case Qt::Key::Key_Left:
|
||||||
|
return slint::platform::key_codes::LeftArrow;
|
||||||
|
case Qt::Key::Key_Right:
|
||||||
|
return slint::platform::key_codes::RightArrow;
|
||||||
|
case Qt::Key::Key_F1:
|
||||||
|
return slint::platform::key_codes::F1;
|
||||||
|
case Qt::Key::Key_F2:
|
||||||
|
return slint::platform::key_codes::F2;
|
||||||
|
case Qt::Key::Key_F3:
|
||||||
|
return slint::platform::key_codes::F3;
|
||||||
|
case Qt::Key::Key_F4:
|
||||||
|
return slint::platform::key_codes::F4;
|
||||||
|
case Qt::Key::Key_F5:
|
||||||
|
return slint::platform::key_codes::F5;
|
||||||
|
case Qt::Key::Key_F6:
|
||||||
|
return slint::platform::key_codes::F6;
|
||||||
|
case Qt::Key::Key_F7:
|
||||||
|
return slint::platform::key_codes::F7;
|
||||||
|
case Qt::Key::Key_F8:
|
||||||
|
return slint::platform::key_codes::F8;
|
||||||
|
case Qt::Key::Key_F9:
|
||||||
|
return slint::platform::key_codes::F9;
|
||||||
|
case Qt::Key::Key_F10:
|
||||||
|
return slint::platform::key_codes::F10;
|
||||||
|
case Qt::Key::Key_F11:
|
||||||
|
return slint::platform::key_codes::F11;
|
||||||
|
case Qt::Key::Key_F12:
|
||||||
|
return slint::platform::key_codes::F12;
|
||||||
|
case Qt::Key::Key_F13:
|
||||||
|
return slint::platform::key_codes::F13;
|
||||||
|
case Qt::Key::Key_F14:
|
||||||
|
return slint::platform::key_codes::F14;
|
||||||
|
case Qt::Key::Key_F15:
|
||||||
|
return slint::platform::key_codes::F15;
|
||||||
|
case Qt::Key::Key_F16:
|
||||||
|
return slint::platform::key_codes::F16;
|
||||||
|
case Qt::Key::Key_F17:
|
||||||
|
return slint::platform::key_codes::F17;
|
||||||
|
case Qt::Key::Key_F18:
|
||||||
|
return slint::platform::key_codes::F18;
|
||||||
|
case Qt::Key::Key_F19:
|
||||||
|
return slint::platform::key_codes::F19;
|
||||||
|
case Qt::Key::Key_F20:
|
||||||
|
return slint::platform::key_codes::F20;
|
||||||
|
case Qt::Key::Key_F21:
|
||||||
|
return slint::platform::key_codes::F21;
|
||||||
|
case Qt::Key::Key_F22:
|
||||||
|
return slint::platform::key_codes::F22;
|
||||||
|
case Qt::Key::Key_F23:
|
||||||
|
return slint::platform::key_codes::F23;
|
||||||
|
case Qt::Key::Key_F24:
|
||||||
|
return slint::platform::key_codes::F24;
|
||||||
|
case Qt::Key::Key_Insert:
|
||||||
|
return slint::platform::key_codes::Insert;
|
||||||
|
case Qt::Key::Key_Home:
|
||||||
|
return slint::platform::key_codes::Home;
|
||||||
|
case Qt::Key::Key_End:
|
||||||
|
return slint::platform::key_codes::End;
|
||||||
|
case Qt::Key::Key_PageUp:
|
||||||
|
return slint::platform::key_codes::PageUp;
|
||||||
|
case Qt::Key::Key_PageDown:
|
||||||
|
return slint::platform::key_codes::PageDown;
|
||||||
|
case Qt::Key::Key_ScrollLock:
|
||||||
|
return slint::platform::key_codes::ScrollLock;
|
||||||
|
case Qt::Key::Key_Pause:
|
||||||
|
return slint::platform::key_codes::Pause;
|
||||||
|
case Qt::Key::Key_SysReq:
|
||||||
|
return slint::platform::key_codes::SysReq;
|
||||||
|
case Qt::Key::Key_Stop:
|
||||||
|
return slint::platform::key_codes::Stop;
|
||||||
|
case Qt::Key::Key_Menu:
|
||||||
|
return slint::platform::key_codes::Menu;
|
||||||
|
default:
|
||||||
|
if (e->modifiers() & Qt::ControlModifier) {
|
||||||
|
// e->text() is not the key when Ctrl is pressed
|
||||||
|
return QKeySequence(e->key()).toString().toLower().toUtf8().data();
|
||||||
|
}
|
||||||
return e->text().toUtf8().data();
|
return e->text().toUtf8().data();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class MyWindow : public QWindow, public slint::platform::WindowAdapter
|
class MyWindow : public QWindow, public slint::platform::WindowAdapter
|
||||||
{
|
{
|
||||||
|
|
|
@ -5,8 +5,22 @@
|
||||||
//!
|
//!
|
||||||
//! The key code comes from <https://www.unicode.org/Public/MAPPINGS/VENDORS/APPLE/CORPCHAR.TXT>
|
//! The key code comes from <https://www.unicode.org/Public/MAPPINGS/VENDORS/APPLE/CORPCHAR.TXT>
|
||||||
//! the names comes should match with <https://www.w3.org/TR/uievents-key/#named-key-attribute-values>,
|
//! the names comes should match with <https://www.w3.org/TR/uievents-key/#named-key-attribute-values>,
|
||||||
|
//!
|
||||||
// NOTE: Update builtin_elements.md when changing/adding/removing keys, to keep the docs in sync!
|
//! The format is a semicolon separated list of keys
|
||||||
|
//! `<char code> # Slint name # Qt code # Winit code`
|
||||||
|
//!
|
||||||
|
//! ## Example
|
||||||
|
//!
|
||||||
|
//! ```
|
||||||
|
//! macro_rules! do_something_with_keys {
|
||||||
|
//! ($($char:literal # $name:ident # $($qt:ident)|* # $($winit:ident)|* ;)*) => {
|
||||||
|
//! //...
|
||||||
|
//! };
|
||||||
|
//! }
|
||||||
|
//! i_slint_common::for_each_special_keys!(do_something_with_keys);
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
// NOTE: Update namespaces.md when changing/adding/removing keys, to keep the docs in sync!
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! for_each_special_keys {
|
macro_rules! for_each_special_keys {
|
||||||
($macro:ident) => {
|
($macro:ident) => {
|
||||||
|
|
|
@ -137,9 +137,15 @@ pub mod key_codes {
|
||||||
/// internal used unicode representation. The enum is convertible to [`std::char`] and [`slint::SharedString`](`crate::SharedString`).
|
/// internal used unicode representation. The enum is convertible to [`std::char`] and [`slint::SharedString`](`crate::SharedString`).
|
||||||
/// Use this with [`slint::platform::WindowEvent`](`crate::platform::WindowEvent`) to supply key events to Slint's platform abstraction.
|
/// Use this with [`slint::platform::WindowEvent`](`crate::platform::WindowEvent`) to supply key events to Slint's platform abstraction.
|
||||||
///
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// Send an tab key press event to a window
|
||||||
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let slint_key_code: char = slint::platform::Key::Tab.into();
|
/// use slint::platform::{WindowEvent, Key};
|
||||||
/// assert_eq!(slint_key_code, '\t')
|
/// fn send_tab_pressed(window: &slint::Window) {
|
||||||
|
/// window.dispatch_event(WindowEvent::KeyPressed { text: Key::Tab.into() });
|
||||||
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub enum Key {
|
pub enum Key {
|
||||||
$($name,)*
|
$($name,)*
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue