Detecting modifier keys inside a button's On Click event

J_Orrell

Registered User.
Local time
Today, 10:17
Joined
May 17, 2004
Messages
55
Afternoon folks. Apologies if this is the wrong subforum; as always in my case, this question could be either construed as a form question or a VBA question, for once I've stuck it in the VBA section.

A button's On Click event on one of my forms runs a For...Next loop that I want to break out of if the user clicks a particular key, such as modifier key or escape. I know that's not the greatest programming practice in the world, but sometimes needs must. So I need to I need to detect a keypress while the code inside an On Click event is busy, therefore I can't rely on the Key Down event etc. To detect modifier keys from inside an active event I usually slap something like:
Code:
Public Declare Function GetKeyState Lib "user32" (byVal nVirtKey As Long) As Integer
...in a module and refer to that, e.g.: intX = GetKeyState(vbKeyControl) tests the control key. But this doesn't seem to work inside the For...Next loop. It's as though the keyboard isn't being scanned while the event is active. Any better ideas?

Thanks
 
Last edited:
Within your For...Next loop, you might try inserting a DoEvents statement before your GetKeyState statement.
 
It isn't clear what the loop is for.

Are you aware that the Change event of a textbox runs once for each character typed? You can look at the keystrokes in the Change event to determine if some particular character was typed.
 
Within your For...Next loop, you might try inserting a DoEvents statement before your GetKeyState statement.
That worked for me, thanks! I had no idea there was a DoEvents command. :cool:
 
@Pat,

Regardless of what the loop is for, there are two things that are clear from the OP:

One: It is a loop of code running from a button's Click event. In that scenario, the button control has the the focus, and a button does not have a Change event to be triggered as you describe.

Two: J_Orrell is looking for a "break" key to exit/terminate the code loop once started. The idea to use the GetKeyState function is a good one; the addition of the DoEvents statement allows for system/form events to take place so that the function can register the change.

You are correct, though, that a textbox's Change event is a very useful code trigger. I've used it in a number of Access applications for dynamic form manipulation and navigation.
 
It is a loop of code running from a button's Click event. In that scenario, the button control has the the focus, and a button does not have a Change event to be triggered as you describe.
I could see that. Running the code inside the button click might have been the only method of running the code that the OP thought of.
 
For the record for others who search on this question, the problem is that event routines are normally managed in a linear fashion. If you are in event A, then event B cannot fire until event A exits. So while the OnClick event is still active, the KeyPress event cannot take effect.
 

Users who are viewing this thread

Back
Top Bottom