enigo-rs / enigo

Cross platform input simulation in Rust
MIT License
1.05k stars 103 forks source link

Non-ascii characters not working on certain applications #88

Open Mandragorian opened 4 years ago

Mandragorian commented 4 years ago

Describe the bug Trying to simulate typing the string "hello world ❤️" in a text input field of firefox types only "hello world ". Similar results with other non-ascii characters, for example "¯\_(ツ)_/¯" results in "\_()_/". I also tested this keepassxc and it also fails. In other applications like chromium / alacritty / telegram-desktop the string is printed correctly.

To Reproduce

  use enigo::{
      Enigo,
      MouseControllable,
      KeyboardControllable,
  };

  fn main() {
      let mut enigo = Enigo::new();

      // Make the following line aim the cursor at a text input field
      enigo.mouse_move_to(0, 0);

      enigo.mouse_click(enigo::MouseButton::Left);
      enigo.key_sequence("hello world ❤️");
  }

Expected behavior "hello world ❤️" should be printed in the text input field.

"hello world " is printed instead.

Environment (please complete the following information):

Additional context Might have something to do with #36 ?

Firefox version 73.0

From keepassxc

KeePassXC - Version 2.5.3 Revision: f8c962b

Qt 5.14.1

pythoneer commented 4 years ago

Do you have posted a comment recently? I got a notification but cannot see any new comment here? I have looked into this a little bit and i can reproduce this. It seems like Firefox does need some more time than other applications to recognize Non-ascii characters. Here is the modified example that is running on my System (Archlinux) (the thread::sleep(Duration::from_secs(1));s are not necessary i just used them for convenience)

use enigo::{
    Enigo,
    MouseControllable,
    KeyboardControllable,
};

use std::thread;
use std::time::Duration;

fn main() {
    let mut enigo = Enigo::new();

    // Make the following line aim the cursor at a text input field
    enigo.mouse_move_relative(0, 0);
    thread::sleep(Duration::from_secs(1));
    enigo.mouse_click(enigo::MouseButton::Left);
    thread::sleep(Duration::from_secs(1));
    enigo.set_delay(400000);
    enigo.key_sequence("hello world ❤️");
}

Mandragorian commented 4 years ago

Yeah I posted a comment and deleted it because it was wrong. It didn't occur to me that github sends an email for comments and it would seem weird. Sorry about the confusion.

I added the delay as you suggest in my programme. And while this does make the problem better, it doesn't make it go away. What I mean is that, at least on my machine, with the delay it works some of the time, while some other times it still misses some non-ascii character. I had to try a few times to observe this.

pythoneer commented 4 years ago

No worries. I think this is just an unfortunate trait of Xorg that we have to live with :/ ... we tried many implementations to get this working reliably but couldn't really figure it out and just used xdo in the end. Their code is similar to ours that we have started with but was a little bit more reliable. But in the end this is really just a "hack". I really don't know a better way to do this.

pentamassiv commented 1 year ago

Regarding the "hack", with the key mapping. There is some documentation about it in Chapter 5. Keyboards on the following page: https://web.archive.org/web/20231005143214/https://x.org/releases/X11R7.7/doc/xproto/x11protocol.html

However it contradicts the actual behavior :-)