How to use keyboard::Key::Character


Why can’t I use it just like examples:

editor\src\main.rs
138:            keyboard::Key::Character("s") if modifiers.command() => {

pane_grid\src\main.rs
228:        Key::Character("v") => Some(Message::SplitFocused(Axis::Vertical)),
229:        Key::Character("h") => Some(Message::SplitFocused(Axis::Horizontal)),
230:        Key::Character("w") => Some(Message::CloseFocused),

stopwatch\src\main.rs
98:                keyboard::Key::Character("r") => Some(Message::Reset),

it doesn’t show “mismatched types” in examples but my code.

Not sure why it works in one place but not the other, but did you manage to make it work in some other way? Changing "s" to "s".into() should do it.

I’ve tried, it doesn’t work.

A couple of possibilities come to mind:

  • Are you on an old version of Rust and/or smol_str? I have rustc 1.78.0 and smol_str 0.2.2.
  • Is it possible that the tool that’s giving you that message is not aligned with what rustc says? Can you try compiling directly with cargo/rustc?

With cargo run:

error[E0308]: mismatched types
   --> src\app.rs:131:47
    |
59  |         match event {
    |               ----- this expression has type `iced::Event`
...
131 |                 key: keyboard::Key::Character("s"),
    |                                               ^^^ expected `SmolStr`, found `&str`

And the version of rustc is rustc 1.76.0 (07dca489a 2024-02-04) on Windows 10, and smol_str 0.2.2.
:melting_face: And it’s the same when attached with .into()

Mind sharing the full source? It may be related to the context in which you’re trying to match “s” against a SmolStr.

this may help