KeyDown
By Richard Rost
10 months ago
KeyDown Event for Specific Keystrokes in MS Access In this Microsoft Access tutorial, we will learn how to use the KeyDown event to detect and react to specific keystrokes, including non-character keys such as F1, Control, and arrow keys. Learn how to handle Shift, Control, and Alt key combinations effectively without affecting your Access form inputs. MembersThere is no extended cut, but here is the file download: 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!
PrerequisitesLinksRecommended Courses
Keywords TechHelp Access, KeyDown Event, Key code vs ASCII, Non-character keys, Shift key value, Reacting to keystrokes, Control-Alt combinations, Stop default actions, VB key code constants, Access form key events, Debugging KeyDown, Detect special keys, Bypass cut copy paste, Tab character handling
Subscribe to KeyDown
Get notifications when this page is updated
Transcript
Today we're going to talk about the key down event and how you can use it to react to specific keystrokes in Microsoft Access. In yesterday's video, we learned about the key press event, and key press is cool. It has a lot of cool uses, but it generally only works with characters that you type on the keyboard like letters, numbers, and such, but it doesn't work with special keys like F1, Control-T, arrow keys, and those kinds of things.
The key down event will detect non-character keys such as F1, arrow, and control keys. This applies to stuff that you normally wouldn't fill into a text box in your Access form, but you might want to know when the user presses it. Key down occurs before key press in case you're using them both together, which you can. Key down is not case sensitive. For example, A is always going to return a 65 because it's tied to the key, not the actual value that's returned.
Let's see it in action. First, if you haven't watched yesterday's key press video, go watch that first so this will make a whole lot more sense after you've watched that one. Click the link down below. Let's take a look at a key down event. We'll do the same thing. We're going to go into the first name field as we did yesterday, but this time, go to key down. Yes, there's a key up tool. We'll talk about key up in a different video. Today's focus is on key down.
Key down doesn't get a key ASCII, like key press does. It gets a key code and a shift value. Now, key code is similar to key ASCII, but it's not exactly the same thing. A lot of the key codes are the same, but not all of them. Let's just message box the key code. Then we're going to set key code equal to zero. If you remember yesterday, if we set key ASCII to zero, it nullifies the key. We're going to do the same thing here. I want to see what the key code is and then just blank it. That way it doesn't actually press it. If I press F1, I don't want the help system popping up.
Save that. Let's come back out here. Let's click on this and I'm going to hit the A key on the keyboard. A 65. That's the same as what I was expecting before. Most of these are the same. I press space 32. Most of them are the same, but not always. What about shift A? Let me hit shift. As soon as I hit the shift key, I got a 16. What's that? Well, 16 is the key code for the shift key itself. It's got its own key code. That's why it's different from the ASCII characters. There's a 16 ASCII data link. I don't know what the sum of these are. So it's not the same. Some of them are different. If I do F1, it's 112.
Now, there is a whole list of these constants. If you really want to use those instead of the numbers, they're on Microsoft's website. I'll put a link to the VB key code constants down below. Here they are. You've got the shift key, enter key, and others like VB key down. I mean, I never use these. I'll be completely honest with you. I never use these because I just look it up. I just have access. Tell me what it is. So if I want F1, I just come in here, message box, and it shows F1. Okay, it's 112. Then I just come in here and put 112 in here.
What about that 16 before? Well, 16 is the actual key code for the shift key. But the shift parameter also gets a value too, indicating whether or not the shift control and or alt keys are pressed. That looks like this: if just the shift key is pressed, the value comes in as one. If just the control is pressed, it's two. If just alt is pressed, it's four. And you can mix and match them. For example, three means shift and control are pressed. Five means shift and alt are pressed. Seven is all three of them, four plus two plus one. Alt shift would be five. Control alt would be six and so on.
Let's do, give me the key code and a dash and the shift value. Save that. Let's come back out here, click in here and I'm going to press just the A again. I got a 65 and a zero because the shift key wasn't pressed. How about if I hit just the shift key? 16 and a one. See that? Because the shift key is pressed if I'm pressing the shift key. Makes sense? How about the control key? See, 17 and two, alt, 18 and four.
Now, how do you react to these keys being pressed? Well, instead of message boxing it, which I'm going to leave here because later on, you might want to pop in here and say, hey, what is this key that I want to know? So leave that there, you can always unremit later. What we're going to do now is we're going to say, okay, I only want to react to, let's say the A key. So if key code equals 65, which is A, then message box A was pressed. And I'm going to cancel the key code then key code equals zero. And if, so anything else, if any other key gets pressed, it's going to ignore it. But if an A was pressed, it's going to say A was pressed and ignore the key.
Ready? So I can come in here, I'm going to hit other keys, I'm going to hit other keys, if I hit an A, boom, A was pressed, see? And then it blanks the key, the A doesn't go into the box. Makes sense? What if I only want to do that with a capital A? Well, then shift will be one. So, and shift equals one, then, let's say what it is here. Well, we got it, that right in here. And it's shift A was pressed. Save it, come back out here. I'm typing characters, I'm typing lowercase A's, I'm typing characters, I'm doing shift A, oh, look at that, shift A was pressed. Because the shift is one and the key code is 65.
What if you want control F2? Well, let's see, control is two. So we'll say if shift, it's going to be shift as the name of the variable. Shift is two, and then what did I say, control? We'll put it right in here, control F2. Now we gotta find out what F2's code is. So we'll unrem this real quick. And I'll put an exit sub here too. So I want to see what it is, save it. What is F2's code? Let me click in here and hit F2. It's 113 is F2. Now armed with that information, come in here and put 113. And then I'll rem this stuff back out again because we don't need it anymore. That's just, you could make a second button or a second field for this if you want to. We're going to be doing a lot of it.
Let's see now, shift F2. I'm typing in some stuff, I'm hitting some other keys, I hit just regular F2, what is regular F2? Okay, there we go. And then I'll hit shift F2 and, oh, it's zoomed in. Shift F2, zoomed in. Oh wait, okay, that was not right. Shift F2, zoomed in. It opened up the zoom box. That was my mistake. I don't want shift F2, I want control F2. My mistake. So now I'm gonna hit control F2. Oh, there it is, okay. See, the lesson works when I do the right thing.
How about a difficult one? How about control alt space bar? See if you can figure out how to do control alt space bar. Pause the video, figure it out. And if you're able to figure it out, either way, post a comment down below and let me know if you got it or not. Go ahead and hit pause, go, do it.
So, control and alt together, add up to six. Control plus alt is six. So the shift value is going to be six. And we know that space is 32. Space is still 32 on this thing. That's space. It's the same as the ASCII chart. So what I want here is key code to be 32 and shift is going to be equal to six. And that's going to be control alt space bar. Do it in there. Save it. Throw a debug compile in once in a while. Come back out here. Ready? Control alt space bar, there it is.
Yes, I tried, you cannot capture control alt delete. That's a special Windows key that is programmed at the system level. Nothing can grab control alt delete by design; the system doesn't want anybody having that. That's a special character. Interestingly enough, you can capture the Windows key if I hit the Windows key, but your start menu will still open. It's on my other monitor. The Windows key is 91. But, and then my shift, my Windows start menu did pop up on my other screen. I record on screen two, so you wouldn't see it, but you can capture it.
I mentioned before, I do cover a key down in a bunch of other videos. This one here handles the arrow keys and text boxes. This video will allow you to put tabs into a long text field by hitting the tab key, which normally would move you to a different field. This will actually insert four spaces in there. So that's one way you can capture the tab character and use it to insert spaces instead. This one shows you how to build a context-sensitive help system. So, if your user does hit F1, which you know how to capture now, then you can pop up a form and have help just for that field, or click a button.
In this video's extended cut for the members, I show you how to use the key down event to move up, down, left, and right in a continuous form, just like in an Excel spreadsheet. There's a trick you gotta do to get that to work. And again, that's in the extended cut for the members in this video.
That's going to do it for today. I've got a couple more videos on some similar topics coming up in the next couple of days. I'm gonna teach you how to hit control A to select long text. That's gonna be good. I'm gonna show you how to bypass and to disable cut copy and paste, which that's gonna be interesting. One of the problems with key press is that it doesn't stop the user from pasting values in. We might do a little bit with the key up event. We'll see about that one.
But that's gonna do it for your TechHelp video for today. Hope you learned something. Live long and prosper my friends. I'll see you next time.
TOPICS: Key Down event in Microsoft Access Detecting non-character keystrokes Key Down vs Key Press differences Handling key codes and shift values Message box to display key code Suppressing key actions with Key Down Shift, Control, Alt key combinations Reacting to specific key combinations Custom actions with Key Down event Handling Control Alt Space Bar combination Limitations of capturing certain key combos Using Key Down for context-sensitive help Key Down event for navigating forms
COMMERCIAL: In today's video, we're discussing the key down event in Microsoft Access and how it can detect non-character keys like F1, control, and arrow keys. Unlike the key press event, key down isn't limited to just characters you type. We'll look at how the key down event works with key codes and shift values to identify which keys are pressed, and how you can use these to perform specific actions, like recognizing control alt combos or space bars. You'll also learn about capturing keys for custom functionalities such as creating a context-sensitive help system or using arrow keys in a continuous form like Excel. Make sure to watch yesterday's video on the key press event for a comprehensive understanding. You'll find the complete video on my YouTube channel and on my website at the link shown. Live long and prosper my friends.
Quiz
Q1. What is the main advantage of using the key down event in Microsoft Access over the key press event? A. Key down can capture text input more accurately. B. Key down can detect non-character keys such as F1, control, and arrow keys. C. Key down is faster in execution than key press. D. Key down is more suited for capturing ASCII values.
Q2. When does the key down event occur relative to the key press event? A. After the key press event. B. Simultaneously with the key press event. C. Whenever a non-character key is pressed. D. Before the key press event.
Q3. How does the key down event determine which key was pressed? A. By using the key ASCII value. B. By using the shift key status. C. By using the key code. D. By using the Unicode character value.
Q4. If the shift key is pressed, what key code does it return in the key down event? A. 65 B. 0 C. 112 D. 16
Q5. How can you nullify a key press using the key down event? A. Set key ASCII to zero. B. Set key code equal to zero. C. Unassign the key in the settings. D. Use a control structure to ignore the key.
Q6. What combination of shift values would represent shift and alt keys being pressed? A. 0 B. 3 C. 5 D. 7
Q7. How can you react to a specific key combination, such as control alt space bar, using the key down event? A. Check the key values in Windows settings. B. Use key press event for the combination. C. Determine the key code and shift value for the combination. D. Use third-party software to capture the combination.
Q8. Why is it not possible to capture control alt delete using the key down event? A. It is not recognized by Microsoft Access. B. It is programmed at the system level by Windows. C. It requires special permissions from the operating system. D. It is considered a non-standard key combination.
Q9. What would the shift value be if both control and alt keys are pressed? A. 1 B. 2 C. 4 D. 6
Q10. What action does key code 113 execute in Microsoft Access? A. F1 key action B. F2 key action C. Ctrl A key action D. Space key action
Answers: 1-B; 2-D; 3-C; 4-D; 5-B; 6-C; 7-C; 8-B; 9-D; 10-B
DISCLAIMER: Quiz questions are AI generated. If you find any that are wrong, don't make sense, or aren't related to the video topic at hand, then please post a comment and let me know. Thanks.
Summary
Today's TechHelp tutorial from Access Learning Zone explores the key down event in Microsoft Access and how to use it to track specific keystrokes on the keyboard. Previously, we discussed the key press event, which is great for detecting alphanumeric characters. However, it has limitations since it cannot capture special keys like F1, Control-T, or the arrow keys.
The key down event is useful because it recognizes non-character keys, such as the F1 key, arrow keys, and control keys. This is particularly applicable in scenarios where you want the program to react to keys that wouldn't typically be used in text box input on an Access form. Notably, the key down event precedes the key press event if both are in use simultaneously. It's also not case-sensitive, meaning the letter 'A' will consistently return the integer 65, corresponding to its key on the keyboard rather than the character's actual value.
For those who haven't seen the previous lesson on the key press event, I encourage you to do so as it will provide a better understanding. Now, let's examine how the key down event works. Begin by moving into the first name field in your Access form and accessing the key down event. The key down event differs from key press because it returns a key code and an associated shift value instead of a key ASCII. Although many key codes and key ASCII values overlap, they're not identical. Using the message box, we can display the key code and nulify the key by setting the key code to zero, much like how setting key ASCII to zero disables a key in key press events.
Upon saving and testing, you'll find that pressing the 'A' key will yield the code 65, as expected. The space bar returns 32. If you press the shift key alone, the resulting key code will be 16. Unlike the ASCII values which focus on the character, key codes identify the key itself. The F1 key, for instance, results in 112.
For those interested in a list of these key code constants, Microsoft has them accessible on their website. Personally, I tend not to use these listings, as I prefer to determine key codes directly within Access.
The shift value provides further context about whether the shift, control, or alt keys are also pressed. A shift key alone will register as one, control as two, and alt as four. These values can be combined: for example, a shift and control combination results in three, while a combination of all three modifier keys yields a seven.
To see this in action, I typically combine the key code with the shift value and return them together in a message box. For example, without pressing the shift key, pressing 'A' will show 65 and zero. Pressing the shift key alone shows 16 and one, while pressing the control key will display 17 and two.
To respond to specific keys being pressed, instead of using message boxes, it's straightforward to create conditional statements reacting to particular keys, such as an A being pressed. If you wish to react only to a capital 'A', consider incorporating the shift value of one as part of your logic. Similarly, you can customize responses for combinations like Control-F2, once you've identified F2's key code as 113.
For a more complex task such as Control-Alt-Spacebar, the combined shift value would be six alongside the key code for the spacebar, which is 32. Unfortunately, system-level combinations like Control-Alt-Delete can't be captured by design due to Windows security constraints, but the Windows key can be detected with a code of 91, albeit still producing the Start menu effect.
Beyond these basics, some of my other tutorials touch on using key down events for handling arrow key inputs, capturing tab characters for long text fields, or setting up a help system activated by the F1 key. For members, there's an extended tutorial on using key down events to navigate like a spreadsheet within continuous forms.
That's the overview for today, and I invite you to check back for more tutorials covering useful Access tips like implementing Control-A to select all text or disabling cut, copy, and paste functionalities. Stay tuned for those. You can find a complete video tutorial with step-by-step instructions on everything discussed here on my website at the link below. Live long and prosper, my friends.
Topic List
Key Down event in Microsoft Access Detecting non-character keystrokes Key Down vs Key Press differences Handling key codes and shift values Message box to display key code Suppressing key actions with Key Down Shift, Control, Alt key combinations Reacting to specific key combinations Custom actions with Key Down event Handling Control Alt Space Bar combination Limitations of capturing certain key combos Using Key Down for context-sensitive help Key Down event for navigating forms
|