Git Product home page Git Product logo

Comments (3)

TeamDman avatar TeamDman commented on June 12, 2024

I think the problem is in the key_down for RightArrow since if I have enigo do key_down for Shift then physically hit right arrow it works, so my guess is that the shift key isn't being included in the scancode for the arrow key

from enigo.

pentamassiv avatar pentamassiv commented on June 12, 2024

Hi, thank you for using my library and taking the time to open the report. I have worked on enigo a lot since version 0.1.3 and fixed many issues (Changelog). I did not get to releasing a new version yet. I tried to replicate the described problem in the current master branch and everything worked as expected. There are two release candidates published on crates.io. I have not tried it with them, but they should work as expected too. In order to use the most recent one, add this to your Cargo.toml file: enigo = "0.2.0-rc2". The API changed a little bit. Here is the code that worked for me:

#[cfg(test)]
mod tests {
    // test that sending shift + arrow keys is highlighting text
    #[test]
    fn test_shift_arrow() {
        use crate::{
            Direction::{Press, Release},
            Enigo, Key, Keyboard, Settings,
        };
        use std::thread::sleep;
        use std::time::Duration;

        let mut enigo = Enigo::new(&Settings::default()).unwrap();

        sleep(Duration::from_secs(1));
        enigo.key(Key::Shift, Press).unwrap();
        enigo.key(Key::Control, Press).unwrap();
        enigo.key(Key::RightArrow, Press).unwrap();

        enigo.key(Key::RightArrow, Release).unwrap();
        enigo.key(Key::Control, Release).unwrap();
        enigo.key(Key::Shift, Release).unwrap();
    }
}

Let me know if that fixes the issue for you too.

from enigo.

TeamDman avatar TeamDman commented on June 12, 2024

Confirmed, using 0.2.0-rc2 has fixed the issue.

Thank you for your work!

I'm using this with Bevy, so I also had to make sure I'm storing the Enigo instance in a local to ensure that state was preserved, otherwise it would continue to forget about shift being held

fn handle_input(
    tool_query: Query<&ActionState<KeyboardToolAction>, With<ActiveTool>>,
    mut enigo: Local<Option<Enigo>>,
) {
    if enigo.is_none() {
        *enigo = Enigo::new(&Settings::default()).ok();
    }
    let Some(ref mut enigo) = *enigo else {
        warn!("Failed to create enigo");
        return;
    };

    for tool_actions in tool_query.iter() {
        for variant in KeyboardToolAction::variants() {
            if tool_actions.just_pressed(variant) {
                info!("{:?} key down", variant);
                if let Err(e) = enigo.key(variant.to_enigo(), Press) {
                    warn!("Failed to send key: {:?}", e);
                }
            }
            if tool_actions.just_released(variant) {
                info!("{:?} key up", variant);
                if let Err(e) = enigo.key(variant.to_enigo(), Release) {
                    warn!("Failed to send key: {:?}", e);
                }
            }
        }
    }
}

from enigo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.