Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help getting certain keys to type via Key::Layout #203

Open
Compute-AMSR opened this issue Sep 16, 2023 · 8 comments
Open

Help getting certain keys to type via Key::Layout #203

Compute-AMSR opened this issue Sep 16, 2023 · 8 comments
Labels
question Windows windows specific

Comments

@Compute-AMSR
Copy link

I am trying to use Key::Layout to type each char one after another like a typewriter. However when I feed certain chars into Key::Layout they are not typed and simply skipped over.

let ch = '{';
enigo.key_click(Key::Layout(ch));

This produces an empty output. I need to be able to type the following chars at the minimum {}(). Any ideas on how this can be done?

@pentamassiv
Copy link
Collaborator

If it is not working, it is a bug. Can you give me some more information? The library is cross-platform and there are multiple protocols involved. Which OS are you using, which program are you trying to enter the characters into? If you are using Linux, do you know if it is X11 or Wayland?

@Compute-AMSR
Copy link
Author

I am using a Windows 11 host OS. The rust application is running on the windows side and I am trying to push the output to cool-retro-term running through WSL2. I am running X11 Ubuntu inside WSL2. Almost all the characters work except for the ones I listed above. I realize this is a very unique one off scenario and appreciate any help or guidance.

@pentamassiv
Copy link
Collaborator

Okay, that's a complicated case. The Windows protocols should be the ones relevant here so I guess it is a Windows bug. I have to investigate this.
In the meantime: Does this work for you
enigo.key_sequence("{");?

@pentamassiv
Copy link
Collaborator

A few more questions that would help me find out where exactly the bug is:
Can you try to enter those characters into a different Windows application such as the Text Editor?
Does it work if you try to enter those characters into a different application in Ubuntu such as Firefox or gedit?
What exactly is the code you try to run? Is it the keyboard example? If not, can you please change that example so that it tries to enter the characters and see if it works?

@Compute-AMSR
Copy link
Author

enigo.key_sequence doesn't work at all on my system outside of the keyboard example file which is really odd. If I run my program and open notepad on windows I get the exact same issue. Here is my current code and below that is the input file.

use std::time::Duration;
use std::thread::sleep;
use rand::Rng;
use std::fs::File;
use std::io::*;
use enigo::*;

fn main() -> std::io::Result<()> {
    sleep(Duration::from_secs(5));
    let mut enigo = Enigo::new();
    let file = File::open("./src/variables2.rs")?;
    let reader = BufReader::new(file);

    for line in reader.lines() {
        let mut rng = rand::thread_rng();
        
        for mut ch in line.expect("Unable to read line").chars() {          
            let rand_delay: u64 = rng.gen_range(50..100);
            sleep(Duration::from_millis(rand_delay));

            let check_upper = ch.is_ascii_uppercase();
            println!("{} {}", ch, check_upper);

            if check_upper {
                ch.make_ascii_lowercase();
                enigo.key_down(Key::Shift);
                enigo.key_click(Key::Layout(ch));
                enigo.key_up(Key::Shift);
            } else {
                enigo.key_click(Key::Layout(ch));
            }

        }
        
        let rand_delay: u64 = rng.gen_range(50..100);
        sleep(Duration::from_millis(rand_delay));
        enigo.key_click(Key::Return);
        
    }

    Ok(())
}
// variables2.rs
//
// Execute `rustlings hint variables2` or use the `hint` watch subcommand for a
// hint.

fn main() {
    let x: i8 = 10;
    if x == 10 {
        println!("x is ten!");
    } else {
        println!("x is not ten!");
    }
}

@Compute-AMSR
Copy link
Author

I've done a bit more digging and have figure out something. It seems to be related to VK_OEM keys. For example the key [ works just fine, however { does not work unless I tell it to hold shift and enter [. This is VK_OEM_4. Looks like more filtering will need to be added to sort out these keys and pre-apply shift, otherwise nothing comes back.

@pentamassiv
Copy link
Collaborator

Does the error persist for you on the newly released version 0.2.0?

@pentamassiv
Copy link
Collaborator

I think this PR might fix your issue.

@pentamassiv pentamassiv added the Windows windows specific label Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Windows windows specific
Projects
None yet
Development

No branches or pull requests

2 participants