Mr. Sja
Mr. Sja

Reputation: 23

How can I get global input in bevy?

I am trying to make a music player with Bevy and Rust. I want to use global input to check if the play/pause button has been pressed even when the window is not in focus.

I've tried using a number of third-party libraries to get global input. I tried using bevy_global_input, but it gives a syntax error when I try to add the plugin as seen below.

error[E0277]: the trait bound `GlobalInputPlugins: Plugins<_>` is not satisfied
   --> src\gui\mod.rs:20:18
    |
20  |     .add_plugins(GlobalInputPlugins)
    |      ----------- ^^^^^^^^^^^^^^^^^^ the trait `bevy::bevy_app::plugin::sealed::Plugins<_>` is not implemented for `GlobalInputPlugins`, which is required by `GlobalInputPlugins: Plugins<_>`
    |      |
    |      required by a bound introduced by this call
    |
    = help: the following other types implement trait `bevy::bevy_app::plugin::sealed::Plugins<Marker>`:
              `()` implements `bevy::bevy_app::plugin::sealed::Plugins<(bevy::bevy_app::plugin::sealed::PluginsTupleMarker,)>`
              `(S0, S1)` implements `bevy::bevy_app::plugin::sealed::Plugins<(bevy::bevy_app::plugin::sealed::PluginsTupleMarker, P0, P1)>`
              `(S0, S1, S2)` implements `bevy::bevy_app::plugin::sealed::Plugins<(bevy::bevy_app::plugin::sealed::PluginsTupleMarker, P0, P1, P2)>`
              `(S0, S1, S2, S3)` implements `bevy::bevy_app::plugin::sealed::Plugins<(bevy::bevy_app::plugin::sealed::PluginsTupleMarker, P0, P1, P2, P3)>`
              `(S0, S1, S2, S3, S4)` implements `bevy::bevy_app::plugin::sealed::Plugins<(bevy::bevy_app::plugin::sealed::PluginsTupleMarker, P0, P1, P2, P3, P4)>`
              `(S0, S1, S2, S3, S4, S5)` implements `bevy::bevy_app::plugin::sealed::Plugins<(bevy::bevy_app::plugin::sealed::PluginsTupleMarker, P0, P1, P2, P3, P4, P5)>`
              `(S0, S1, S2, S3, S4, S5, S6)` implements `bevy::bevy_app::plugin::sealed::Plugins<(bevy::bevy_app::plugin::sealed::PluginsTupleMarker, P0, P1, P2, P3, P4, P5, P6)>`
              `(S0, S1, S2, S3, S4, S5, S6, S7)` implements `bevy::bevy_app::plugin::sealed::Plugins<(bevy::bevy_app::plugin::sealed::PluginsTupleMarker, P0, P1, P2, P3, P4, P5, P6, P7)>`
            and 8 others
    = note: required for `GlobalInputPlugins` to implement `Plugins<_>`
note: required by a bound in `bevy::prelude::App::add_plugins`
   --> C:\Users\samsj\.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_app-0.14.2\src\app.rs:543:52
    |
543 |     pub fn add_plugins<M>(&mut self, plugins: impl Plugins<M>) -> &mut Self {
    |                                                    ^^^^^^^^^^ required by this bound in `App::add_plugins`

I also tried using inputbot, but I need to be able to check if the key was pressed in the last frame, and inputbot only allows me to attach a callback for when the key is pressed. I tried to get around this by putting it in its own thread and making a channel to a resource. The issue I faced is that the channel makes the struct non-sync, so I can't make a resource out of it. I also tried using hookmap-core, but faced a similar issue. To use it I would have to hold a Receiver in a resource which causes the same issue as inputbot did.

Upvotes: 1

Views: 53

Answers (0)

Related Questions