Using Keyboard shortcuts with the Arduino Keyboard library
Are you using the Arduino keyboard library and you’re trying to figure out how to do key combinations so you can do some type of keyboard shortcut? So basically you can have your Arduino board copy something, maybe like Command + Shift + C and then maybe paste it, Command + Shift + V, or whatever hot key combination you’re trying to achieve with your Arduino board. You’re trying to figure out how to do that. Well, in this lesson, we’re gonna walk through a simple example of combining some key presses in order to do one of those hot key functions on a computer. Now, we also have another video about using the keyboard library. We’ll make sure to link to that video in the description, but this video is gonna get specific on key press combinations. Now, the reason we’re making this video is because one of our YouTube subscribers had a follow on question from that previous video, and I’m just trying to answer it as best as I can. All right. Let’s get started. Well, before we dive in, I’d really appreciate if you could take a moment and click that subscribe button. It doesn’t cost you but a click, but it really helps us grow our YouTube channel and bring you great content like this. All right, so here I am in a blank sketch. And what I wanna do is write some code that is gonna copy a line of text and then it’s gonna paste that line of text directly after that line, all right? So basically let’s say I’ve got a line of text here and I wanna write a program for my Arduino. And just so we’re clear, we’re talking about an Arduino that can simulate a USB device and that’s not all Arduinos. Like an Arduino Uno can’t do that but an Arduino Leonardo can, for example. So I wanna write a program where I can put my cursor somewhere and what this program will do is highlight all the text to the right of the cursor on a single line. It’ll copy that line using Command + C or Control + C. It’ll arrow down to the next line and then it will press Command or Control + V to paste that line. So, I want to write a program that’s gonna do that, obviously, all hands off. The Arduino’s gonna do it for me. So let’s go ahead and do this. All right, so the first thing I want to do is include the keyboard library. So let me do that. All right, so the keyboard library is included and then I just threw the setup and the loop in here. If you don’t have setup and loop, you can’t compile the program. And just about after every line of code I write, I like to do a verify, you know? Compile the program just to make sure I haven’t done any errors. So, go ahead and verify it now. All right, it’s done compiling. So so far, I haven’t screwed anything up. That’s good. Now the next thing I’m gonna do and set up is a little precautionary thing. ‘Cause what happens when you start telling your computer to emulate the keyboard and let’s say you tell it to copy and paste stuff. Well, it does that really, really, really super fast. And if you’re not careful, you can end up in a situation where anytime you plug in your Arduino board it starts copying, pasting code really quick. And it’s hard to get a new sketch on there to stop that behavior. So I’m gonna do something that a viewer actually recommended, which was just put a delay in the setup that way anytime you plug in the board, power goes on to it, you’re gonna have five seconds of nothing happening so you can upload a blank sketch or something like that. So here we go. You probably know that pretty much everything you do online is getting tracked. From what YouTube videos you’re watching to where you’re going online. Somebody’s got access to that. But you may not know how easy it is to protect your privacy, secure all that browsing data and access content that otherwise wouldn’t be available in your location using a virtual private network, like Private Internet Access. All you have to do to set up a VPN is sign up, download some software, do a super easy install, and that’s it. So when you do this and set up your VPN, your internet service provider or the network admin, can’t log on or store any of your browsing data and the information is unreadable and untraceable. If you wanna get set up with your own VPN through Private Internet Access and support our channel, use the link in the description and you can get 82% off their service. Plus an extra three months. Just use that link below to set up your own VPN through Private Internet Access. It’s super easy and you’ll be able to support this channel at the same time. All right, so now here I am in the loop. And what I wanna do, like I said, is I wanna be able to put my cursor right here and when I plug in my Arduino board, and again I’m using Arduino Leonardo, what I wanna have happen is I want this line of code to be highlighted. I want then on the next line after it, I want this line pasted in. And I only want that to happen once. So what I’m gonna do in loop is I’m gonna set up some control flow. I’m gonna use an if statement and a flag variable so that this only happens once. So let me do that. All right, so I have this little control flow here. Again, I only want this if statement. I only want the code in here to execute once and that’s doing it. I won’t go into like how this works. We have some other videos on control flow. Or if you really want to dive into this stuff make sure to check out our website, programmingelectronics.com. We’ve got all types of training available for learning this kind of thing. All right, okay so basically, we come into the loop and we’re gonna run some code once. Well, what code is it we’re gonna run? So the first thing I want to do is I wanna highlight this line of code. So how would I, what keyboard keys do I have to press in order to highlight it? Well, right now I’m developing on a Mac and to do that, I would do Shift + Command + right arrow. Like that. So if I press Shift and then I press Command and then right arrow, it highlights that entire line. And for a PC, I’m pretty sure it’s just gonna be Shift + Control + right arrow. So I gotta find a way to do that keyboard combination. So let me do that and we’ll talk about it. Okay, so I’ve got this code right here to do this highlight command. So basically, I’ll have my cursor right here and I want to press the Shift key, and I’m developing on a Mac so I wanna press the Command key. This whole key right here is how you press the Command key on the Mac. And then I wanna press the right arrow key. And this key right here is how you do that. This code right here, KEY_RIGHT_ARROW. So keyboard press is gonna press and hold this button. It’s not gonna release it. It’s gonna press and hold this button. It’s not gonna release it. And it’s gonna press and hold this button and it’s not gonna release it. And then when all of those buttons are pressed that is when that hot key action is initiated on the operating system. So it’s gonna simulate, so here’s my cursor up here. Now I’m pressing Shift. I’m pressing Command and now I’m pressing right arrow. And now you can see that entire line highlights. And that’s exactly what I wanted to have happen, but I want to have my computer do it. And then after that, I want to release all those keys. So in order to release the keys you need to have a command that does that, right? So again, if you just press it, it’s gonna press and hold. You need the release or release all command. In this case I’m just doing release all because it’s convenient. I could do release KEY_LEFT_SHIFT, release KEY_LEFT_GUI, release KEY_RIGHT_ARROW, but that would be like three lines of code. So here we go. I’m just using the release all function. Now you might be like, Mike, this is cool but where’d you get these key codes? Well, I went to my favorite page on the internet and that is the Arduino reference page. And I went to the keyboard library page. And then the keyboard library page has a reference to keyboard modifiers. So keyboard modifiers and special keys. And this tells me these little codes, these little keys I need in order to press different buttons on my keyboard, which is pretty sweet. So I am on a Mac right now. I love PCs too, but I happen to be developing on a Mac. And so if I want to press that Command key, which is a very common key if you develop with Macs, then I need to use this code, KEY_LEFT_GUI or I suppose I could use this one, KEY_RIGHT_GUI. There’s two command keys on a typical Mac keyboard. And so that’s where I got that. If I wanted to press the down arrow, I think there’s, yeah, here we go. See, look at that, KEY_DOWN_ARROW, but you can see if there’s, you know, there’s different keys that you want to press that aren’t like the letters then there’s tons of different codes on here. Now, if you were on a PC or I was trying to do this for a PC and I wanted to highlight that line of code, I’d need that Control key. So this would just be KEY_RIGHT_CTRL instead of KEY_LEFT_GUI in this keyboard combination. Okay, so I think you get the idea. All right, so now what I want to do is I want to copy that line of code. So let me write some code to do that and we’ll talk about it. All right, so now what I want to do is once I have this line highlighted, now what I wanna do is the hot key to actually copy it. So on a Mac that would be Command + C. So the way I do that in the code is I’m just using the press function from the keyboard library. I’m calling that KEY_LEFT_GUI again, which is the Command key on the Mac. And then I’m doing a keyboard.write and this is how I’m gonna press an actual key. And single quotes and I’m just pressing C. And then I do a release all. Now one thing I should have done is tested this first little bit right here before I moved on to this second little bit. Little behind the scenes secret though, I’ve already written all this code. I’m just kind of rewriting it as I think through it some more. But anyway, so let me just, let’s just check. You should always be checking your code every step of the way to make sure you haven’t screwed something up, right? Like I really should have checked this little protection up here first and I should have checked all of this. I mean, that’s just a habit you need to be into when you’re coding, is constantly checking what you’re writing. That way you can kind of stay on top of the errors you make. So what I’m gonna do is I’m gonna put my cursor right here. I’m gonna upload this to my Arduino Leonardo and what I should expect to see is this line highlighted. And then we’re pressing the copy key so I really won’t see anything there, but, and also we’ll speed up through this delay so we don’t have to wait this delay. And I do note, you’ll notice I put these delays right here. This is just so we can kinda see quote, unquote, see things happening, right? All right. So let’s do it. So upload the code. And now copied. All right, so that’s good. Now what’s good, you can notice is I move my cursor around other stuff isn’t highlighting. So that tells me, at least roughly, that this copied flag is doing its job and this code is only happening once. Okay. So now what we wanna do is I wanna press the down arrow. So I’m gonna do copy that, press the down arrow, and then I’m gonna paste it in. So I’m gonna do the down arrow and paste it in next and then we’ll talk about that. But based on what we’ve done so far I think you probably have an idea of how we’d pull this off. So, think about how you might do it and then just watch me do it here real quick. All right, so now I wrote some code to move the cursor down one line. Essentially, I’m just pressing the down arrow. So the press function from the keyboard library with key down arrow, release it, delay just so we can kind of see it. And then now I want to copy the code. So on a Mac that’s Command + C. Again, if you’re on a PC you’d use that different key for control, just using Control + C to paste what’s on the clipboard. Then I wanna past that line. So on a Mac, you’re gonna use Command + V. On a PC, it’s Control + V. So I’m just using the press function and then I’m using the right function to actually write that letter. And again, we’re emulating a keyboard with our Arduino board. That’s the whole idea here. And so then we’re gonna release all and then delay. All right, so hopefully this is all clear. Let’s see if it actually works. I’m gonna put my cursor right up here. It’s on the left. I’m gonna go ahead and upload this. Should highlight the line. Then we copy it. We move down one line and we paste it. All right, sweet. And we’ve got that all automated in our code using this keyboard library. So that’s pretty cool. All right, well, I hope that was helpful. Hope you see how you can do these different keyboard shortcuts or keyboard combination presses and stuff. And hopefully this is clear. If you’re really enjoying this Arduino programming stuff, make sure to check out our website, programmingelectronics.com. We have a training program that can really help get you up to speed pretty darn quick learning how to program with Arduino. Also before you go, make sure to subscribe to the channel. And if you enjoyed the video, please do like it. If you’ve got some questions, leave them in the comments. We do read them all and try to do our best to answer them. Thanks a ton. Have a great one. Bye.