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

hotkey.modal that ignores anything but given hotkeys #1505

Open
agzam opened this issue Jul 30, 2017 · 12 comments
Open

hotkey.modal that ignores anything but given hotkeys #1505

agzam opened this issue Jul 30, 2017 · 12 comments

Comments

@agzam
Copy link

agzam commented Jul 30, 2017

Is it possible to have a hs.hotkey.modal with a few bindings that would ignore all the other keys. Let's say if I have:

local myModal = hs.hotkey.modal.new({"cmd"}, "space")
myModal:bind("", "g", nil, openGoogleChrome())
myModal:bind("", "f", nil, openFirefox())
myModal:bind("", "s", nil, openSafari())

Then if I press cmd+space, it will activate the modal, in which it would only respond to g, f, and s. Any other keys would be simply ignored. Is that even possible?

@agzam agzam changed the title hotkey.modal for a single key hotkey.modal that ignores anything but given hotkeys Jul 30, 2017
@shageman
Copy link

shageman commented Aug 3, 2017

I am looking for this exact feature as a way to keep the footprint of global shortcuts that I use through hammerspoon low. This is important whenever keyboard-heavy apps are in use (IDEs like Intellij, design-tools like sketch, etc).

@deryni
Copy link
Contributor

deryni commented Aug 3, 2017

I imagine you could do this using the :entered() callback on the modal in which you disabled all your existing bindings (either via a manual list of hotkeys or by using the output from hs.hotkey.getHotkeys).

@agzam
Copy link
Author

agzam commented Aug 3, 2017

@deryni I am not talking about disabling registered hotkeys. I am talking about making all the keys (hotkeys or not) disabled and leave only those that needed in that modal. So if modal expects m to be pressed, users accidentally hits n - that would produce a side-effect in the underlying app, which is not desired. I would prefer if all the keys are completely ignored, and only m is available (in this particular example)

@asmagill
Copy link
Member

asmagill commented Aug 3, 2017

simple answer no, longer answer but you might be able to fake it.

couple of ideas come to mind but not sure which will work best for you.

First idea, add hotkeys to the modal key that cover all of the most likely mistypes with an empty callback function, e.g. function() end. Down side, it only "blocks" the mistypes you think of and means potentially a lot of null function bindings. But easy to implement.

Second idea is to create an eventtap in the modal:entered function which traps keyDown and keyUp events and dismisses them (I think by returning false, but check the documentation for hs.eventtap.new to be sure). Not sure if you'll need to add a check in the eventtap callback to exempt the specific keys that you do want to go through or not -- can't remember which gets priority within the Hammerspoon application. In the exited function make sure to disable the eventap. Down side, potentially harder to code especially if your unfamiliar with eventtaps.

@deryni
Copy link
Contributor

deryni commented Aug 3, 2017

@agzam Ah, I misunderstood. Sorry.

Then yeah, my only thought is what asmagill suggested with the eventtap to ignore everything.

@cmsj
Copy link
Member

cmsj commented Aug 21, 2017

This is an interesting idea and I wouldn't be opposed to it being implemented in Hammerspoon (likely as a pure-ObjC version of @asmagill 's eventtap idea).

@asmagill
Copy link
Member

asmagill commented Sep 1, 2017

See https://github.com/asmagill/hammerspoon-config/blob/master/_scratch/modalSuppression.lua for a solution which examines the specific modal hotkey and creates the eventtap for you

@asmagill
Copy link
Member

asmagill commented Sep 1, 2017

@cmsj, I don't see a need to make this objc, and my only objection to making it part of hs.hotkey is that it would be very easy to create a situation in which you couldn't regain keyboard control of anything (in fact, this happened during testing and I had to ssh in from another machine and kill Hammerspoon)... perhaps if a timer was added by default to auto-exit and you had to explicitly call a method on the modal to disable the timer...

@cmsj
Copy link
Member

cmsj commented Sep 1, 2017

@asmagill interesting. What do you think about having this be an optional parameter of an hs.hotkey.modal? (I'd suggest something like :setExclusive([timeout]) where the timeout defaults to some number of seconds, but can be set to 0 for no timeout if people really insist)

@asmagill
Copy link
Member

asmagill commented Sep 1, 2017

I think I like that, though I want to mull it over a bit more to see if we want to (or even can) add some additional checks to minimize the likelihood of someone accidentally "locking up" their machine.

@cmsj
Copy link
Member

cmsj commented Sep 1, 2017

@asmagill I also think a valid option would be for it to be a completely separate thing, as a Spoon, that creates a menubar item to a) show it's in exclusive mode, b) break out of that mode.

@asmagill
Copy link
Member

asmagill commented Sep 4, 2017

Liking the menubar item and doing this as a spoon. Will add to my list, but not adding to 0.9.56 since a spoon can be easily added in between releases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants