Correct method for using key press event (1 Viewer)

tmyers

Well-known member
Local time
Today, 17:40
Joined
Sep 8, 2020
Messages
1,090
What would be the acceptable method for calling buttons on click event using key press? I have typically done the If KeyAscii = (some code) Then call _Click. This seems like a bad way to handle 12 different key events on a form. I feel like there is a much better method than multiple IF statements, but have not seen any just yet browsing here and stackoverflow.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 17:40
Joined
May 21, 2018
Messages
8,525
In this case you can not use a single function as an event handler, so you will have to write the individual key press event procedures. The reason you cannot write the single event handler is because you need to pass in event arguments.

But if you wrote the 12 event procedures they can call a single procedure and you could pass to it your keyascii and or other arguments. I do not understand A key press event procedure calling a click event procedure though.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 17:40
Joined
May 21, 2018
Messages
8,525
FYI. If it was something more than 12, you could write a single class to trap all of these. Not saying this is "better", but would save you from writing the event procedures. I can demo, but for this few the amount of time and code saved would be minimum. If you had 20 or 50 you savings would be a lot.
 

Gasman

Enthusiastic Amateur
Local time
Today, 22:40
Joined
Sep 21, 2011
Messages
14,238
What would be the acceptable method for calling buttons on click event using key press? I have typically done the If KeyAscii = (some code) Then call _Click. This seems like a bad way to handle 12 different key events on a form. I feel like there is a much better method than multiple IF statements, but have not seen any just yet browsing here and stackoverflow.
I would use a Case statement, not If ?
 

tmyers

Well-known member
Local time
Today, 17:40
Joined
Sep 8, 2020
Messages
1,090
In this case you can not use a single function as an event handler, so you will have to write the individual key press event procedures. The reason you cannot write the single event handler is because you need to pass in event arguments.

But if you wrote the 12 event procedures they can call a single procedure and you could pass to it your keyascii and or other arguments. I do not understand A key press event procedure calling a click event procedure though.
I am just doing key shortcuts. I captioned several buttons like "F1 SAVE" "F2 OPEN FORM" etc. So when they press F1, it calls that buttons click event.
 

tmyers

Well-known member
Local time
Today, 17:40
Joined
Sep 8, 2020
Messages
1,090
I would use a Case statement, not If ?
I was leaning towards using case statements, but I am still learning the correct way to use that method. I have not fully grasped writing that just yet.
 

tmyers

Well-known member
Local time
Today, 17:40
Joined
Sep 8, 2020
Messages
1,090
After digging around some more, I found an example and went with:
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

Select Case KeyCode

Case vbKeyF2
    Call SaveJobForm_Click

Case vbKeyF3
    DoCmd.Close

Case vbKeyF4
    Call OpenExcelbtn_Click

Case vbKeyF5
    Call CreateRevisionbtn_Click
    
Case vbKeyF6
    Call SearchRepBtn_Click

Case vbKeyF7
    Call DrawingLink_Click

Case vbKeyF8
    DoCmd.Requery

Case vbKeyF9
    Call VndrQuoteBtn_Click
    
Case vbKeyF10
    Call OpenQuoteBtn_Click
Case Else

End Select

End Sub
However, this does not work in respect to when the given key is pressed, nothing happens.
 

Minty

AWF VIP
Local time
Today, 22:40
Joined
Jul 26, 2013
Messages
10,368
Does your form have Key Preview in the event properties set to Yes?
It will need to.
 

tmyers

Well-known member
Local time
Today, 17:40
Joined
Sep 8, 2020
Messages
1,090
Does your form have Key Preview in the event properties set to Yes?
It will need to.
Yup.
It seems to only work for a few of them and while holding shift. I would prefer to handle it by just the function keys without having to also press shift/alt/cntrl
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 22:40
Joined
Sep 12, 2006
Messages
15,641
What are you actually doing that you want to use key presses, rather than just, for example, command buttons, or click events?
I don't think I have ever used a key press event.
 

Isaac

Lifelong Learner
Local time
Today, 14:40
Joined
Mar 14, 2017
Messages
8,777
Yup.
It seems to only work for a few of them and while holding shift. I would prefer to handle it by just the function keys without having to also press shift/alt/cntrl
For what it's worth, the reason I never really got into creating 'shortcut keys', or started and then quickly stopped, is it just seemed like a non-starter for me. I realized that your typical end-user comes with shortcut keys all their own in the first place. They already have stuff in place. It will be stuff you don't (cannot) know about it going in. It could be anything from:

1) Google shortcut keys that conflict with yours
2) Desktop actual shortcuts with a shortcut key combination that conflicts with yours
3) Some other program, where the designer also thought shortcut keys would be cool
...And the list goes on and on.

This is why generally speaking, shortcut keys in programs are only as good as the happy coincidence of that particular end user having no other conflicts with it at the present moment. It's like a shot in the dark, playing the lottery. They may work sometimes, not other times. Conflicting shortcut keys have various ways of behaving...from not working at all, to working some times, to working all the time for only one of the hosts. It's a mess.

IMHO.
 

tmyers

Well-known member
Local time
Today, 17:40
Joined
Sep 8, 2020
Messages
1,090
What are you actually doing that you want to use key presses, rather than just, for example, command buttons, or click events?
I don't think I have ever used a key press event.
I am just calling the on click events from buttons to open forms, do saves, refresh etc. Thought it would be nice to have hot keys but its turning out to be a pain.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 17:40
Joined
May 21, 2018
Messages
8,525
Since it is just buttons. You can use the ampersand in the caption to launch the click event with a alt+Letter.
So
Open E&xcel
becomes
Open Excel

and fires on alt+X

or

Save Job
Open Quote
Create Revision

cannot be any easier
 

Attachments

  • AltShortCut.accdb
    400 KB · Views: 186
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 22:40
Joined
Sep 21, 2011
Messages
14,238
Yup.
It seems to only work for a few of them and while holding shift. I would prefer to handle it by just the function keys without having to also press shift/alt/cntrl
I can get F2 to F6 outputting to Debug, but F7 is a spellcheck and F8 to F10 to debug
 

Isaac

Lifelong Learner
Local time
Today, 14:40
Joined
Mar 14, 2017
Messages
8,777
Love the ampersand. Great for users doing heads-down data entry, banging out new records x many an hour
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 17:40
Joined
May 21, 2018
Messages
8,525
Love the ampersand. Great for users doing heads-down data entry, banging out new records x many an hour
I am so mouse centric, that I never use features like this. I forget they even exist. I think this only works with buttons and tabs. I tired on a label caption and it gives the underline, but I could get nothing to happen.
 

Isaac

Lifelong Learner
Local time
Today, 14:40
Joined
Mar 14, 2017
Messages
8,777
I'm the opposite. No particular reason, just somehow grew into preferring the keyboard over the mouse. When I open a window I even do Alt+Space+X to maximize it rather than using the mouse! But yeah - at least it's a nice option to give users, in (I think) higher-volume situations

Only ever used it on buttons
 

tmyers

Well-known member
Local time
Today, 17:40
Joined
Sep 8, 2020
Messages
1,090
Since it is just buttons. You can use the ampersand in the caption to launch the click event with a alt+Letter.
So
Open E&xcel
becomes
Open Excel

and fires on alt+X

or

Save Job
Open Quote
Create Revision

cannot be any easier
I did not know this was a thing. I will just to this!
 

Users who are viewing this thread

Top Bottom