Caps Lock On Off
By Richard Rost
5 years ago
Turn Caps Lock, Num Lock, and Scroll Lock ON / OFF
Learn how to toggle Caps Lock, Num Lock, and Scroll Lock using Access VBA. We'll see how to turn them ON and OFF, and to read their current state so that you can either set them to ON, or set them to OFF.
Nancy from Swanton, Maryland (a Gold Member) asks: How can I use SendKeys to turn Scroll Lock ON. Right now, it only toggles between ON and OFF. I need to just set it ON when I click a button.
Members
I will show you how to turn these keys on and off WITHOUT using SendKeys!
Silver Members and up get access to view Extended Cut videos, when available. Gold Members can download the files from class plus get access to the Code Vault. If you're not a member, Join Today!
Links
Original Question Thread: https://599cd.com/a?27338
Intro to VBA: https://599cd.com/IntroVBA
Get Caps Lock: https://599cd.com/GetCapsLock
MS SendKeys Reference: https://tinyurl.com/y57rx8ba
Set Caps Lock: https://599cd.com/SetCapsLock
Transcript Welcome to another TechHelp video brought to you by AccessLearningZone.com. I am your instructor, Richard Rost.
In today's video, I am going to show you how to turn on or off the Caps Lock, Num Lock, and Scroll Lock keys. Today's question comes from Nancy from Swanton, Maryland, one of my Gold members.
Nancy says, how can I use Send Keys to turn Scroll Lock on? Right now, it only toggles between on and off. I need to just set it on when I click a button.
Nancy posted her original question in my Access Forum. You can read the original thread if you'd like to. I'll put a link to it down in the link section below.
Basically, when she clicks a button, she wants to use Send Keys to turn the Scroll Lock on. She turns it on right there. Then she uses this Activate Keyboard Layout to change some settings on her keyboard. This is some third-party app she's using. Then she goes to a record, finds something, and turns it back off again.
The problem is, if it's already on, this here will turn it off and then back on again. If it's off, it'll turn it on and then off again. She wants this to just simply turn Scroll Lock on.
One thing we talked about is that I personally hate the Send Keys command. I used to use it a lot when I was first learning how to develop with Access years and years ago, but there is almost always a better way to do something without using Send Keys. I'll talk about that at the end of the video. For now, let's just get Nancy's code working.
Here I am in my TechHelp free template. You can download a copy of this template if you want to. It's on my website. I'll put a link to the free template down below.
Let's go into the Customer Form here because it's a nice big note field that I can use. Let's replicate what Nancy's got going on.
Let's go to Design View. If you've never done any programming before, go watch my Intro to VBA video first. If you've never done any VBA before, it's a good introduction for you.
Let's put a button in here. Command button right down here in the bottom. Cancel the wizard. Let's call this my Send Keys button. Right click, Build Event.
I'm not going to bother naming the button. We're not going to keep it around for long.
What am I going to do in here? What she's got going on is she's using the Send Keys command to send a key to the key, as if you typed it at the keyboard.
I'm going to do a whole separate video on Send Keys. How people commonly use it, why you shouldn't use it, and some different popular alternatives for the Send Keys command.
For example, if you want to go to the Notes field and then send some keys, you'd say this. You'd say Notes.setFocus. That moves the focus over to the Notes field. Then you can say Send Keys. Let's just send the key A.
CommitTrue says wait until the keys are done being pressed, like if they were being pressed at the keyboard, before continuing on with the next line. If you don't specify true there, Send Keys will just spew the information and it might continue running and mess things up.
Let's see what this does here real quick. Let's save this. Let's close it and reopen it, and I'll just hit the Send Keys button. See that? It just goes to the Notes field and sends an A.
You could send more stuff if you want to, send a whole bunch of strings like that. If you want to send some value, you could send the first name field. Send Keys puts Richard in there, so he gets it right out of there, puts it in there.
Nancy is using Scroll Lock, which is a special key. Scroll Lock, like that inside of curly braces. That will actually toggle the Scroll Lock on and off. If you send Scroll Lock, it's like you push the Scroll Lock key.
I'm going to use Caps Lock for the purposes of class, so you can see some differences of what's going on. Scroll Lock really doesn't do much and she's using it with a special program, but we'll use Caps Lock for class. It's the same exact thing. Num Lock works the same way too.
If you want to see a list of a whole bunch of special keys that you can use with Send Keys, I'll put a link down below to Microsoft's Send Key reference. Later on in the future, when I actually record my Send Keys video, I'll put a link down there too. I can edit it later. I haven't recorded it yet. It's not really high on my priority list because I don't really like Send Keys, but there's some stuff to be learned with Send Keys.
Anyway, let's use Caps Lock. Let's just toggle Caps Lock on and off. I just realized I got a goof in there. We're going to put that quotes right there. I'm not at the end here. That's much better.
Send Caps Lock, comma true. Let's see what happens. I'll hit Send Keys and nothing appears to happen because it goes there, but it didn't do much. But notice now, Caps Lock is on. See that? If I type in A, A, A, A, A on the keyboard, see I'm getting that. If I hit it again, Caps Lock went off. Click, and then click. It's going on and off.
The problem is, if you want to assist the user by hitting the button and then it always turns Caps Lock on, you have no idea what state it's at when you get here. You need some way of reading the keyboard state and saying, hey, is Caps Lock on or off? If it is on, don't do anything. If it's not on, turn it on. So there we need some code to read the keyboard state.
Now, this is where we get a little more complicated, but just follow along with me. I'll show you everything you need to know.
First, we have to create our own module to get the keyboard state. Now, I'm going to give you some code. You can either type it in yourself or you can copy it from my code vault. I'm going to make this a free page for everybody in my code vault. It's usually a paid feature, but this one I'm going to make for free.
OK, here it is in the code vault. It's called Get Key State. Now, I'm not going to explain how all of this code works. For now, just worry about copying this and pasting it into a module in Access.
I'll put a link to this page down below in the links section. Go here, copy everything from this option compare database all the way down to the bottom right there in front of usage. Copy all that stuff. Ctrl C, put it in your clipboard.
Now back in your database, go to Create and then Module. Regular module, not class module. Module will open up the VBA window like this. Get rid of this stuff in here because it's going to copy and paste in, and then paste like that. There's the code right out of the code vault. That's all you need to do.
Save this, Ctrl S, save this, then get, let's say yes here, sorry, call this the Get Key State Mod. Get Key State Module.
What is this? Basically, this is just some functions that allow you to determine whether or not Caps Lock is on - that's Get Caps Lock, Num Lock is on, or Scroll Lock is on. You don't have to worry about all this stuff up here. Sometimes it's like driving a car. You don't have to know how the engine works to drive the car. All we have to know right now is this, this, and this. So we're going to use the Get Caps Lock function right here.
Go back over to your customer form, right click, Design View. Let's make a button that will tell us whether or not Caps Lock is on.
I'm just going to take this guy. I'm going to copy this, copy, paste. State of Caps Lock. And I can spell today. Lock, oh, see.
Right click, Build Event. We're just going to say in here, Message Box Get Caps Lock. That's going to Message Box whatever this returns, Get Caps Lock. It'll be a true or a false.
Is Caps Lock on or off? Save it. Come back out here, let's close this, open it back up again. State of Caps Lock, false. Right now, it's off. Do you see it on down here? No.
Let's Send Keys. Boom. And now check it, notice right there, see. Caps Lock is now true. So my function's working, see, hit it, and now it's false.
Now we know how to read whether Caps Lock is on or not. Knowing that, up here in our little routine that Nancy's trying to run, all we have to do is read Get Caps Lock. If it's already on, don't send the Caps Lock key.
So right here, I'm going to say, if Get Caps Lock then do nothing, else, Send Keys, Caps Lock, end if.
Save it. Come back out here. Let's rename this button now from Send Keys. Let's rename this button to Turn Caps Lock on. It'll only turn it on now. Save it, close it, open it back up again.
Let's come in here and type. Caps Lock is off, turn Caps Lock on. Boom. Type, type, type, type, type, type. Turn Caps Lock on again. Click. It's still on because it didn't resend the Caps Lock key. See that? It's just turning it on.
And you can very easily reverse that button to turn it off if you wanted to. Make another button that says Turn Caps Lock off. You could make your own toggle button if you want to. That's kind of silly because you could just press the Caps Lock key yourself. But this way you can force the Caps Lock key to always go on instead of toggling between on and off. And that's what Nancy was missing.
Nancy, you could do the same thing with Scroll Lock. Just replace that with the Scroll Lock command.
Now, me personally, I hate Send Keys. In the extended cut for members, which Nancy, you're a member, so you can watch it, I'm going to show you how to do this without using Send Keys. I hate Send Keys. I can't stand Send Keys. I've had all kinds of problems on my databases because Send Keys is unreliable. This will work for you just fine because it sounds like you have a very simple thing going on there, but for serious databases, you don't want to use Send Keys.
It involves one more programming library that I'll have to have you copy from the website. You're a Gold member, so you can just download the template if you want to. But we can do this without Send Keys. We can actually set the keyboard state using another function. We can set Caps Lock on and off without using Send Keys. That'll be in the extended cut for the members.
Members, extended cut. I'll show you how to use a function instead of Send Keys to turn the Caps Lock, Num Lock, Scroll Lock keys on and off. We don't like Send Keys. Stop using Send Keys. I hate it. So we'll learn how to do it without Send Keys in the extended cut.
As a reminder, Silver Members and up get access to all of my extended cut videos. Gold Members get access to the complete code vault that you saw today and the download folders. You can just grab the template.
How do you become a member? Click the join button below the video. After you click the join button, you'll see a list of all the different types of membership levels that are available.
Silver Members and up will get access to all of the extended cut TechHelp videos, live video and chat sessions, and more. Gold Members get access to a download folder containing all the sample databases that I build in my TechHelp videos, plus my code vault where I keep tons of different functions that I use. Platinum Members get all the previous perks plus access to my full beginner courses and some of my expert courses.
These are the full length courses found on my website and not just for Access. I also teach Word, Excel, Visual Basic, ASP, and lots more.
These free TechHelp videos are going to keep coming as long as you keep watching them. I'll keep making more.
If you liked this video, please give me a thumbs up and feel free to post any comments that you have. I do read them all.
Make sure you subscribe to my channel, which is completely free. Click the bell icon and select all to receive notifications when new videos are posted. Click on the show more link below the video to find additional resources and links. You'll see a list of other videos, additional information related to the current topic, free lessons, and lots more.
YouTube no longer sends out email notifications when new videos are posted. If you'd like to get an email every time I post a new video, click on the link to join my mailing list.
If you have not yet tried my free Access Level 1 course, check it out now. It covers all the basics of building databases with Access. It's over three hours long. You can find it on my website or on my YouTube channel. If you like Level 1, Level 2 is just $1. It's also free for all members of my YouTube channel at any level.
Want to have your question answered in a video just like this one? Visit my TechHelp page and you can send me your question there.
Click here to watch my free Access Beginner Level 1 course, more of my TechHelp videos, or to subscribe to my channel.
Thanks for watching this video from AccessLearningZone.com.
|