Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   Index   Templates   Seminars   TechHelp   Forums   Help   Contact   Join   Order   Logon  
 
Home > TechHelp > Directory > Access > KeyDown < KeyPress | Ctrl-A Select Long Text >
Back to KeyDown    Comments List
Pinned    Upload Images   Link   Email  
Transcript
Richard Rost 
            
32 days ago
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.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in KeyDown.
 

 
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2025 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 1/23/2025 10:05:58 AM. PLT: 1s