Narsil / rdev

Simple library to listen and send events to keyboard and mouse (MacOS, Windows, Linux)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

When used with `bevy` app, keystrokes cause `EXC_BREAKPOINT`.

golmschenk opened this issue · comments

Problem

Hello! I'm attempting use rdev along with bevy. When I run both the bevy app and the rdev listen loop at the same time, rdev detects mouse events without an issue, but crashes when a keyboard event occurs. The resulting error being:

Exception: EXC_BREAKPOINT (code=1, subcode=0x18fb44924)

Minimal example

The minimal version of the code to obtain this error is:

use std::thread;
use rdev::{listen, Event};
use bevy::prelude::{App, DefaultPlugins};

fn callback(event: Event) {
    println!("My callback {:?}", event.event_type);
}

fn main() {
    thread::spawn(|| {
        if let Err(error) = listen(callback) {
            println!("Error: {:?}", error)
        }
    });
    App::new()
        .add_plugins(DefaultPlugins)
        .run();
}

This error does not occur if the rdev listen is not used or if the bevy app is not run (by replacing it with a loop{}).

Catching the error, the error seems to occur within

let layout = TISGetInputSourceProperty(keyboard, kTISPropertyUnicodeKeyLayoutData);

at line 94 of keyboard.rs.

System:

macOS = Version 13.1 (22C65)
rust = 1.68.0-nightly
bevy = 0.9.1
rdev = 0.5.2

More complete output showing mouse input working correctly, but crashing on keyboard input:

My callback MouseMove { x: 722.30859375, y: 541.48046875 }
My callback MouseMove { x: 723.671875, y: 540.796875 }
My callback MouseMove { x: 724.60546875, y: 540.484375 }
My callback MouseMove { x: 725.54296875, y: 540.484375 }
My callback MouseMove { x: 726.48046875, y: 540.484375 }
My callback MouseMove { x: 727.41796875, y: 540.484375 }
My callback ButtonPress(Left)
My callback ButtonRelease(Left)
My callback ButtonPress(Left)
My callback ButtonRelease(Left)
My callback ButtonPress(Left)
My callback ButtonRelease(Left)

Process finished with exit code 133 (interrupted by signal 5: SIGTRAP)

(The error occurs immediately on first keyboard input, before anything is printed for that event.)

Additional notes

If, for the bevy app, I use MinimalPlugins instead of DefaultPlugins, the error does not occur. Adding back in the WinitPlugin (and it's required WindowPlugin) the error occurs again. I'm assuming there is some conflict between how rdev and winit listen for events.

Hi,

As a sidenote, you are aware this crate is to detect global keyboard events, and that bevy has probably better suited app level event stream catching, right ?

Thanks for reporting. This is most likely due to who tries to get (and has) the main thread of the app.
rdev needs to run in the main thread within MacOS (it's unavoidable, it is mandatory to listen within the main thread).

Since I expect bevy to also be listening for events at the app level, on the main thread, it's likely a conflict occurs. Since with MinimalPlugin the error does not occur, I'm guessing everything happens because of conflicting bindings to the OS.

If global events is not a requirement, then don't use rdev and focus on bevy event streams. If you need global events, then I would try to start from a minimal plugin, remove everything event related in Bevy, and go from there.

If you get to the bottom of this, don't hesitate to share back here.

Hello!

As a sidenote, you are aware this crate is to detect global keyboard events, and that bevy has probably better suited app level event stream catching, right ?

Yep, I'm looking to use Bevy to provide a display of something that the user interacts with while another app is in focus, so I'm trying to use rdev as the input method.

rdev needs to run in the main thread within MacOS

Just to clarify, does rdev need to be on the main thread? Or just the main process (but a different thread in that process is ok)? The readme for rdev says process, and when I run it in the main process, but not the main thread, it seems to work when Bevy is not also running.

I would try to start from a minimal plugin, remove everything event related in Bevy, and go from there.

Unfortunately, starting with MinimalPlugins and adding the WinitPlugin causes the issue. And, as far as I know, the window display capabilities of Bevy are largely based on winit. That said, since only the keyboard seems to be conflicting, perhaps I can find some winit configuration that disables keyboard monitoring. Once I have some time, I'll dig into it a bit deeper.

Thanks again for your time!

The readme for rdev says process

Trust the doc, not me, I forgot those details. But I remember MacOS being picky sometimes. Maybe main process is enough :)

It's most likely something in bevy or winit is using similar OS capabilities and end up conflicting the OS. The OS error could also be helpful (it's usually an error code of some kind, and getting the message requires using google or some other OS function).