Electron Fundamentals, v2 Electron Fundamentals, v2

Write to System Clipboard Solution

Check out a free preview of the full Electron Fundamentals, v2 course:
The "Write to System Clipboard Solution" Lesson is part of the full, Electron Fundamentals, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Steve live codes the solution to the exercise

Get Unlimited Access Now

Transcript from the "Write to System Clipboard Solution" Lesson

[00:00:00]
>> Steve Kinney: All right, so our mission was to do something very similar to what we did for creating a new clipping, but this time taking the most recent clipping, and writing it back to the clipboard. So we put down the underlying foundations for doing that. We basically just needed to send.

[00:00:16] We needed to register a global shortcut, and then when that global shortcut was activated, then we needed to trigger the message from the main process to the render. And have the render go ahead and do the rest of the work for us. So let's go ahead and see if we can implement that.

[00:00:34] So in the main process, very similar to what we have right now, we can even go ahead and grab that.
>> Steve Kinney: This case, we'll call writeToClipboardShortcut and in this case-
>> Steve Kinney: Send a message to write to the clipboard.
>> Steve Kinney: And we'll say write-to-clipboard, it's very similar to what we had earlier.

[00:01:23]
>> Steve Kinney: Paste that as well. That's a change to the main process, so we will have to restart it, but before we do that, let's implement the render process. The only thing we need to do is not rush the same keyboard shortcut twice, I'm not even gonna try to say that word.

[00:01:40] All right, so we've got these very intuitive keyboard commands in place. Back in our renderer, we need to basically say-
>> Steve Kinney: handleWriteToClipboard. When we get that-
>> Steve Kinney: write-to-clipboard message in place.
>> Steve Kinney: Do npm start.
>> Steve Kinney: Very cool, so I'm going to copy literally anything else to my clipboard, that seems good.

[00:02:26] Then we'll go over here to do a Cmd+Shift+2, paste that.
>> Steve Kinney: I say I get at index zero, all right? So let's take a look at that.
>> Steve Kinney: And the write to clipboard function.
>> Steve Kinney: Looks like it's having a hard time getting that off the state.
>> Speaker 2: It's an object when you writeToClipboard.

[00:03:11]
>> [INAUDIBLE].
>> Steve Kinney: Good catch, I don't even need to debug when I have friends with me. [LAUGH] This is why you write some code hastily and then have other people do the exercise. And then when you all come back together, they've solved your bugs for you. Yeah. [LAUGH] That's why you live code with friends.

[00:03:33] Cool, so we should be able to restart that.
>> Steve Kinney: There we go, I can copy something else, copy all of that. I'll hit Cmd + Shift + @, and we'll get the most recent thing on the clipboard. All right, cool, so now we have the ability to have global keyboard shortcuts anywhere.

[00:03:57] I can reach my application, I can write to my application.