Electron, v3

Global & Keyboard Shortcuts

Steve Kinney

Steve Kinney

Electron, v3

Check out a free preview of the full Electron, v3 course

The "Global & Keyboard Shortcuts" Lesson is part of the full, Electron, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Steve walks through implementing keyboard shortcuts that can access the Clipmaster application regardless of whether the app is currently in focus.


Transcript from the "Global & Keyboard Shortcuts" Lesson

>> All right, so now we've got that in place. There are a few just other OS integration niceties and fun things to inspire you that I think are super useful in these kinds of applications. So we can write to the clipboard. We can copy from the clipboard. The other thing that we can do is we can register global shortcuts, right?

So you saw before when I made an application menu, I could have an accelerator like Command or Ctrl O, right, that would trigger that file menu, but that's obviously only while you're in the app, right? What would be cool if I wanted, let's say this is holding memes instead of deep thoughts that I wanted to copy and paste.

What if I wanted to be able to at a moment's notice, select something, hit a keystroke and have it saved in there, right, or something along those lines, from anywhere, right? And there are applications we probably have on our computers like that new CounterEvent, whatever keystroke thing pops up, or to-do list thing, or what have you.

We can do that. That's totally possible in an Electron app in a way that's not possible in a browser app, and to no one's surprise, also, shockingly, easy. So we've got this thing called the global shortcut module. It does what it says on the tin, right? It registers global shortcuts.

We do need to make sure that our application is ready, right now, that is just simply creating a window for the first time. So what we do need to do is to say, well, go. We still wanna create a window, but then also we want to register a global shortcut.

As you can see, GitHub Copilot knows that I have now built this app about five times in the last week, and knows what I'm gonna type next, which is super helpful. So yeah, in this case, and again, you can choose. You know your computer better than me, to know what keystrokes are not currently registered by something else that you have some weird conflict.

But in this case, you basically, can go ahead, register a global shortcut with the same kind of accelerator syntax that we saw before, right? And we could do something like, Content is, read whatever is on the clipboard. And then what we wanna do is send it, or let's even just do the simplest possible thing right now.

Again, you know how the IPC stuff works. You can send a message to, in this case, our one browser window, you can get the currently focused browser window, right? This application strikes me as one where I only ever want one browser window. So I might choose to move some of that logic and store it in a variable at the top scope.

And you can send a message to it and then, say, OS, this comes in, before we did, we registered the handler ON file open, in a react app, we might choose to, for instance, have a use effect hook, right? Component mounts, start listening for anything over the channel, saying, a new clipping has come in, and then add it into the store the same way we would in a react app.

Let's just do the simplest possible thing right now, cuz I think, how you wire up the different pieces is stuff we've covered over and over again so far. But I think do the idea of registering the global shortcut and seeing it in place, so this is why I was to say, we'll do, yeah, I mean, that's gonna be the main window.

Browser window, just go ahead and the return value of that create window. And we'll just say, browserWindow.show(). So browserWindow.focus(). So from anywhere, I just even want to pull the app up as the foremost thing. I'm trying to remember if I need to also, if I wanna be able to make, yeah, let's also focus the app as well.

So this should, theoretically, this is a live decision I've made at the moment, Cmd, Ctrl+Shift+C. Just cuz I don't know what I've got registered, I'm also gonna say all. So I've gotta hit Cmd, Ctrl+Shift+Option+C. I don't think that does anything else on my computer presently, so we'll find out.

The other thing that I will say that you do need to do is app dot, Onquit, or willquit, somewhere along those lines in the lifecycle, you probably wanna do globalShortcut unregisterAll, right. If you register them when the app is quitting, you should give them back to the OS cuz it's polite.

So we'll have those two pieces in place, and we'll just do npm, start again. Cool, and ideally, what I wanna do is go over here. Look at that, I'm somewhere else, I'm finding stuff, I have a thought, I'm gonna go back over the GitHub, I'm gonna hit Command, Option, Shift C, Clip Master, right?

And now it is ready to go and I can copy something from the clipboard, right? And so you can register these shortcuts and they kinda work almost anywhere, and they're ready to rock and roll for you as well.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now