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

Some keyboard commands fail silently and often unpredictably on macOS #31

Open
agentofuser opened this issue Mar 12, 2020 · 2 comments
Open
Labels
bug Something isn't working

Comments

@agentofuser
Copy link

Hi, I'm starting to play with autopilot, trying to issue some common keyboard commands like switching from terminal to browser with Command+Tab, opening a new browser tab with Command+t, etc., and I'm having difficulty understanding what is happening.

I'm very new to Rust too, so I apologize if I'm doing something silly. These are some of the results I've had so far running a sample binary with cargo run from the command line:

use autopilot::key::*;

fn sleep(n: u64) {
    std::thread::sleep(std::time::Duration::from_millis(n * 1000));
}

fn main() {
    // this works
    type_string("hello world", &[], 0.0, 0.0);

    // expected: HELLO WORLD
    // actual: nothing happens
    sleep(4);
    type_string("hello world", &[Flag::Shift], 0.0, 0.0);

    // expected: _
    // actual: nothing happens
    sleep(4);
    tap(&Character('-'), &[Flag::Shift], 0, 0);

    // this works (Command+Tab to browser)
    tap(&Code(KeyCode::Tab), &[Flag::Meta], 0, 0);
    tap(&Code(KeyCode::Return), &[], 0, 0);

    // expected: open new tab (Command+t)
    // actual: nothing happens
    sleep(4);
    tap(&Character('t'), &[Flag::Meta], 0, 0);

    // this works (Command+Tab back to terminal)
    sleep(4);
    tap(&Code(KeyCode::Tab), &[Flag::Meta], 0, 0);
    tap(&Code(KeyCode::Return), &[], 0, 0);

    // this works
    sleep(4);
    tap(&Code(KeyCode::Backspace), &[], 0, 0);
    tap(&Character('D'), &[], 0, 0);

    // this doesn't work (trying to leave fullscreen mode)
    sleep(4);
    tap(&Character('f'), &[Flag::Meta, Flag::Control], 0, 0);
}

Some calls fail sometimes, but work depending on context. The Command+t (new tab) call, for instance, works occasionally, depending on whether there is some type_string() call before it or some other combination which I haven't been able to reproduce reliably.

When a call to tap() or type_string() fails, there's no error message produced. Is there another way I could look into what's happening?

Thank you for publishing this crate!

@Joni-Aaltonen
Copy link

Joni-Aaltonen commented Mar 19, 2020

I'm seeing this as well with the modifier flags. Have tried multiple different delay combinations.

Edit:
Trying to achieve paste on a mac

autopilot::key::tap(&Character('v'), &[autopilot::key::Flag::Meta], 0, 0);

@msanders msanders added the bug Something isn't working label Mar 22, 2020
@ThetaSinner
Copy link

ThetaSinner commented Jan 3, 2023

Hi @agentofuser and @Joni-Aaltonen, I have an explanation for you as to why the operations you're attempting fail.

The char_to_key_code function

fn char_to_key_code(character: char) -> CGKeyCode {
returns 0 for many inputs. e.g. the c and v that you would be relying on for copy and paste.

The system_toggle function for macos then skips doing anything

if code != 0 {
The silent failure referenced by this issue!

I have a solution that works for me on my fork. For copy and paste like @Joni-Aaltonen was trying to achieve.

@msanders I'd be happy to work on contributing this fork but I'm pretty new to macos development so I might need a few pointers.

Anyway, I hope this is useful to somebody - I know it's an old issue but you never know :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants