Narsil / rdev

Simple library to listen and send events to keyboard and mouse (MacOS, Windows, Linux)
MIT License
530 stars 132 forks source link

Question: Approach to keeping track of held keys with grab. #145

Open SylvanFranklin opened 1 month ago

SylvanFranklin commented 1 month ago

I could be misreading the docs, but I haven't found a built in way to keep track of what keys are being held, so currently I'm pushing and popping from a held vector something like this:

match event.event_type {
  rdev::EventType::KeyRelease(key) => {
      keys.remove(key);
  }
  rdev::EventType::KeyPress(key) => {
      keys.push(key);
  }
  _ => {}
}

This works fine for the listen callback, but using the unstable grab feature I can't mutate since grab does not take an fnMut. Just wondering how others have handled keeping track of held keys, since it seems like a pretty common use case for this crate! Cheers!

SylvanFranklin commented 3 weeks ago

For anyone who stumbles across this, I found that the best way was to use an ArcMutex storing whatever way you want to represent the held keys for me Vec and unlock within the callback to grab.