-
Notifications
You must be signed in to change notification settings - Fork 15.8k
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
Can I make non global accelerators without putting things in a menu? #1334
Comments
@maxkorp You can capture key shortcuts via |
Yeah, I was just hoping to use some of the super nice |
I'd buy that feature actually, because one of our biggest bugs right now is trying to deal with the non-US keyboard web debacle in Chrome; even when you know the keyboard layout you still have to have a massive list of tables |
As far as I can tell, theres no way currently on the js side to access the accelerator class. I'll take a look at enabling that, but I'm not familiar enough with the code to give a real opinion. From what I see, i'd need to add something in atom/browser/api, then add it to the bindings as well. That seem roughly correct-ish? |
@maxkorp Yeah, classes get automatically bound via this framework called |
Hrm, I'll take a crack at it. No clue if being able to instantiate an accelerator would help, even, but it's worth a shot. |
Hrm, unfortunately a little more complex than I'd hoped but I have a plan of attack. IMO, 2 separate features would be good, and I'd be happy to start at them if you guys think they're good ideas.
|
FYI: I have a temporary workaround, where I set up a small helper with a map that stores shortcuts by keystroke (and in my case, by some other stuff dependent on my apps architecture). Whenever a window focuses, all of the shortcuts are registered. Whenever a window blurs, it checks if any window has focus, and if one does, it leaves them enabled. If no window has focus anymore, then the shortcuts are unregistered. Clearly, that could be tweaked to work on a per window basis, etc, to fit someones needs. Does anyone have any opinion on how they'd like to see that implemented in atom shell? I'm still leaning towards putting something in API, but I might put a compatible js/coffee solution in first, then move over to the native way next. My thoughts are as follows: // This is global, if any window in the app is focused it works
LocalShortcut.register(accelerator, callback);
// This is only active if someWindow has focus
LocalShortcut.register(accelerator, callback, someWindow); Does anybody have any thoughts on that? Something missing? More functionality than wanted? Is the idea a welcome one (I know something to fill the whole is wanted, but does this seem like a good fit)? |
The accelerators are handled natively in A js/coffee solution sounds good to me, but we should probably make it a third party module instead of being in atom-shell core. |
@maxkorp, did you end up publishing a module for this? |
Unfortunately no. I have some ideas, but I haven't had time. |
For people looking at this issue, most of you can just use a JavaScript library like https://github.com/atom/atom-keymap or https://craig.is/killing/mice. |
This solution is gonna be a real bummer with international keyboards though until https://code.google.com/p/chromium/issues/detail?id=227231 lands in Electron |
👍 In my app, I need to do something when a shortcut combination is triggered, but only when my app is focused. Currently, I use global shortcuts and subscribe/unsubscribe depending on whether the window is focused, but support for local shortcuts would be much better. |
@sindresorhus has that been working consistantly for you? I was having issues with those events not firing when switching was caused by electon itself, for example by the dev tools getting focused by a breakpoint. |
I'm using keymaster in my app to declare key combos and events to execute when they are pressed. It seems to be enough for my needs. Maybe it can work for you? |
Yes, but I just started using it.
Create a minimal testcase and open a new issue.
No, as I don't control the browser contents. I need it for electron-debug. It also doesn't solve the already mentioned issues in this thread. |
How is that issue going.. how soon do you think we'll be able to use accelerators without the menu? |
I published a module to handle local window shortcuts. |
Uses https://npmjs.com/package/electron-localshortcut to workaround a bug in Electron (electron/electron#1334). We can remove `electron-localshortcut` once that bug is fixed.
Uses https://npmjs.com/package/electron-localshortcut to workaround a bug in Electron (electron/electron#1334). We can remove `electron-localshortcut` once that bug is fixed.
@parro-it The issue with @paulcbetts You're right. |
Oh yes I didn' t check but that is for sure because that action doesn' t trigger lostfocus event on browserwindow instance. |
Any news on this one? |
@pmario I just solved it as follows:
I only use one window but it can be modified for multiple to check if at least one window is still in focus. Edit: Added a |
You can use app.on('web-contents-created', function (event, wc) {
wc.on('before-input-event', function (event, input) {
if (input.key === 'x' && input.ctrl && !input.alt && !input.meta && !input.shift) {
// Do something for Ctrl-X
event.preventDefault()
}
})
}) @parro-it |
Given the workarounds above, we believe this module is better implemented in userland (ex. the excellent electron-localshortcut and thus i'm going to label this a |
I think there is good value in having a consistent API for shortcuts inline with Menu Accelerators and GlobalShortcuts. |
This should be implemented. Most of the time you'd want local shortcuts. Making event listeners on BrowserWindows don't work if there are webviews in use that capture focus. And even if creating a menu worked it makes no sense that a menu has to be created to enable keyboard shortcuts (which are a very core feature of most applications). I'm saying, this should be handled by Electron itself and not some third party library. |
Unfortunately, It did not work for me. It never allows me to press |
Just for someone who may have a same problem like me in 2020...
This returns the current focused BrowserWindow object.
E.g. I could make a full screen shortcut as the above way. |
Here's a working flexible solution that allows you to manually define non-global shortcuts for focusedWindow without putting them into app menu: app.on('web-contents-created', (webContentsCreatedEvent, webContents) => {
webContents.on('before-input-event', (beforeInputEvent, input) => {
// console.log('Main console::', input)
const { code, alt, control, shift, meta } = input
// Shortcut: toggle devTools
if (shift && control && !alt && !meta && code === 'KeyI') {
BrowserWindow.getFocusedWindow().webContents.toggleDevTools({ mode: 'detach' })
}
// Shortcut: window reload
if (shift && control && !alt && !meta && code === 'KeyR') {
BrowserWindow.getFocusedWindow().reload()
}
})
}) Also works when you disable the app menu with |
Wouldn't a native solution be infinitely better than any hacky workaround? As another user said, keyboard shortcuts are a core feature of most applications... |
By the way, the current version of electron-localshortcut does not register globalShortcuts |
@parro-it That's great to know, thanks for pointing it out! |
For anyone interested in this issue, I just published electron-shortcuts. I needed a safe solution to local shortcuts and decided to put something together, which ended up becoming a tiny, safety-focused npm library 😄 It's definitely still early stage, so contributions and feedback of any kind are definitely welcome! |
I'd like to add some hotkeys for events in my app, and accelerators seem like the natural way to do that, but some of these things don't make sense in a menu. Is there a way to use accelerators without registering a global shortcut or a menu? I'd rather not have hidden menus.
The text was updated successfully, but these errors were encountered: