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

macOS: Simplifying the code #161

Merged
merged 2 commits into from
Mar 25, 2023
Merged

Conversation

pentamassiv
Copy link
Collaborator

On macOS the library panics when using Key::Layout. This is likely caused by input methods being used. We had a few issues (#124) for it and a merged PR. However the problem continues #153. This is another attempt at fixing the issue.

Instead of first trying to use TISCopyCurrentKeyboardInputSource and then checking if it was successful, we now immediately use TISCopyCurrentKeyboardLayoutInputSource. This should work regardless of any input methods.

The mac I test on does not have any input methods in use, so I never saw this issue. I also cannot reproduce it. However the code continues to work for me after the changes. I am hoping for feedback from users who had issues to tell me if this PR solves them.

@jackeydou
Copy link

I will help to test your code in my code tonight, hope the problem will go away.

@jackeydou
Copy link

I will help to test your code in my code tonight, hope the problem will go away.

I just try the code of this commit but it still crashes at this line let current_keyboard = unsafe { TISCopyCurrentKeyboardLayoutInputSource() };.

@pentamassiv
Copy link
Collaborator Author

Okay, thank you for the feedback. Back to the drawing board :(

@pentamassiv pentamassiv marked this pull request as draft March 7, 2023 19:54
@pentamassiv pentamassiv force-pushed the fix_macos branch 4 times, most recently from e2d4cde to e200708 Compare March 8, 2023 00:23
@pentamassiv
Copy link
Collaborator Author

I pushed a few more commits. I got rid of the custom implementation of a lot of the types. Maybe there is something wrong with a type and that is why it crashes on your ARM Mac but not on my x86 Mac. It is pure guessing at this point though. If you have time, you can try out the updated code from this PR. It might fix it.

If it does crash, can you please copy the output? Maybe that will give me a hint

@jackeydou
Copy link

Ok, let me try it

@jackeydou
Copy link

jackeydou commented Mar 8, 2023

Maybe I should give more information, I use this lib in my Tauri app, and enigo use in the tauri command fn:

#[tauri::command]
async fn copy_selection(window: tauri::Window) {
    let mut enigo = Enigo::new();
    let trusted = accessibility::query_accessibility_permissions();
    if trusted {
        enigo.key_down(Key::Meta);
        enigo.key_click(Key::Layout('c'));
        enigo.key_up(Key::Meta);
    }
}

it only crashes when Key::Layout('c') is executed, I try to debug in vscode, in create_string_for_key
image
next step will crash
image


then I edit the code, only use TISCopyCurrentKeyboardLayoutInputSource
image
next step
image
then next step it crashed
image

@pentamassiv
Copy link
Collaborator Author

Does it crash if you execute:
cargo run --example key
?

@jackeydou
Copy link

Does it crash if you execute: cargo run --example key ?

oh, I just try the example, it runs ok. It’s my fault, Maybe there is something happened in Tauri? Should I start an issue in Tauri?

@pentamassiv
Copy link
Collaborator Author

Ah, that is interesting. I don't know what Tauri does, but I think it would be helpful if you would open an issue there and ask for assistance. Please post the link so that I can follow it as well to see if there are any fixes needed from Enigo's side.

@pentamassiv pentamassiv changed the title macOS: Fix panics when an input method is used macOS: Simplifying the code Mar 10, 2023
@pentamassiv pentamassiv marked this pull request as ready for review March 25, 2023 00:17
@pentamassiv pentamassiv merged commit c566f91 into enigo-rs:master Mar 25, 2023
@pentamassiv pentamassiv deleted the fix_macos branch March 25, 2023 00:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants