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

feat: support registering media play/pause/stop/next/prev keys #71

Merged
merged 4 commits into from
Apr 23, 2024

Conversation

amrbashir
Copy link
Member

closes #70

@amrbashir
Copy link
Member Author

amrbashir commented Apr 15, 2024

@amrbashir amrbashir marked this pull request as draft April 15, 2024 09:32
@pewsheen
Copy link
Contributor

pewsheen commented Apr 17, 2024

I pushed a WIP commit to another branch: 0f51409

There are some limitations on macOS:

  • It requires adding a MachPort source into CFRunLoop (to listening OS events), this will trigger OS to request Accessibility permission
  • It can only detect which media key is pressed. If you press Cmd+MediaPlay, we only know the MediaPlay button is pressed.
  • Need to store EventTap and EventTapSource somewhere (Global?) to release it when we want to stop watching the media key
  • We need to find a way to check the media key is registered and its hotkey ID inside the callback closure (maybe Global variable again?) so that we can emit the event to the hotkey event channel. Or we can just hardcode the ID and emit it every time, letting the user decide whether to handle it or not.

@amrbashir
Copy link
Member Author

There are some limitations on macOS:

  • It requires adding a MachPort source into CFRunLoop (to listening OS events), this will trigger OS to request Accessibility permission

Does this need changes in plist file or it automatically requests it at runtime?

  • It can only detect which media key is pressed. If you press Cmd+MediaPlay, we only know the MediaPlay button is pressed.

Can't we check the NSEvent for modifiers?

  • Need to store EventTap and EventTapSource somewhere (Global?) to release it when we want to stop watching the media key

can we have multiple EventTap? if so, then store it on GlobalHotkeyManager and release it on drop

  • We need to find a way to check the media key is registered and its hotkey ID inside the callback closure (maybe Global variable again?) so that we can emit the event to the hotkey event channel. Or we can just hardcode the ID and emit it every time, letting the user decide whether to handle it or not.

CGEventTapCreate allows passing a pointer to a data, so you can create an Arc<Mutex> stored on GlobalHotKeyManager, that tracks if any media key is registered, then we clone it and convert it to a raw pointer and send it to the callback, that way you can check if it was registered

@pewsheen
Copy link
Contributor

Does this need changes in plist file or it automatically requests it at runtime?

It'll pop up automatically. Currently, it will only ask when registering media keys

I'm storing the event tap and media hotkey information in the GlobalHotKeyManager now. It will require the register and unregister to use &mut self

* feat(macos): register media keys

* chore: update documents

* organize codes

* fix: using RefCell for event_tap

* docs: remove mut from GlobalHotKeyManager::new()
@amrbashir amrbashir marked this pull request as ready for review April 23, 2024 12:29
@amrbashir amrbashir merged commit c530be0 into dev Apr 23, 2024
9 checks passed
@amrbashir amrbashir deleted the feat/media-keys branch April 23, 2024 13:08
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.

Media Keys
2 participants