-
Notifications
You must be signed in to change notification settings - Fork 78
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
Latest version need to re-register handler in every event #16
Comments
@aVolpe I ended up creating something like this private extension HotKey {
func handleKeyDown(_ handler: @escaping (() -> Void)) {
keyDownHandler = {
handler()
self.handleKeyDown(handler)
}
}
} |
Hello @aVolpe, have you considered storing the hotkey object on an instance variable of an object that is guaranteed to exist during the entire app lifecycle? I believe that would solve the issue. My thoughts:
|
@tlk thats sound right, but it doesn't explain why it works in the version |
@aVolpe Good point :-) In order to reproduce the issue I created a new project with Xcode 11.3 (macOS, App, Swift, Storyboard, target 10.15), added HotKey as a SPM dependency and added the setupGlobalKeybindings & handleKeyDown code from above. The hotkey works repeatedly and I cannot seem to reproduce the error as reported. Could the issue be caused by other factors than the library? |
Able to reproduce in Catalyst-based Plugin, but it works on a simple Cocoa-based demo app. Really weird. |
@harryworld would you be able to share a small code sample - as small and simple as possible - that reproduces the issue? |
I have created a repo for this |
For some reason Catalyst.appKit is null when I run the TestCatalyst project and setup() is never called. Would you be able to simplify the example even further? Did the Expected code block work with HotKey v0.1.2? |
@tlk sorry for the late response. I can't reproduce the issue with the latest version of xcode with Hotkey 0.1.3. |
@tlk this the simplest setup to run Cocoa code in Catalyst app... Side note |
@harryworld I try to reproduce the issue like the first time, creating a new project, adding the deps using swift packages, and then registering the key, but this time it's working. |
@aVolpe No worries, thanks! Interesting. @harryworld Ok. Yes I ran the TestCatalyst project on macOS Catalina. Reading the code in MacApp.swift I see the hotKey instance variable on the MacApp class. Looking at Catalyst.swift and FirstViewController.swift the MacApp instance is only requested once (in FirstViewController.swift) and not stored anywhere. Based on that I would expect the MacApp instance to be deinitialized when FirstViewController.viewDidLoad() is completed unless there are any references to the MacApp instance or the hotKey instance. Consequently, the hotKey instance would also be deinitialized and therefore disabled. In your "Actual" code block the hotKey instance is captured by the keyDownHandler closure because it is referenced from self.handleKeyDown(hotkey: hotKey). This means that it is somewhat self-referencing, preventing it from being deinitialized, and I believe this is the reason why it works. Out of curiosity, here is a slightly modified version of your "Actual" code block where the hotKey reference is not captured by the keyDownHandler closure:
My guess is that this slightly modified version does not work unless you keep a reference to the MacApp instance stored somewhere during the entire app lifecycle. If this observation is correct another (better?) solution for you would be to store a reference to the MacApp instance, fx as an instance variable on the FirstViewController. Then your "Expected" code block as well as the code above should work. |
With the version 0.1.3 I need to re-register the key handler every time the handler is fired, for example, in the version
0.1.2
this code works:But in the version
0.1.3
I need to re-register the handler, like this:The text was updated successfully, but these errors were encountered: