Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TugboatEng on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listening keyboard events on UserForm (eg :UserForm_KeyPress)

Status
Not open for further replies.

Florent

Mechanical
Joined
Feb 18, 2007
Messages
32
Location
FR
Hi ;

All is in the title.
On a macro with one UserForm, I want to catch some keyboard events.
EG : Escape key to end the macro.

I tried to use "Private Sub UserForm_KeyPress( ByVal KeyANSI As MSForms.ReturnInteger)" but unfortunatly, it doesn't work.
I copy/paste my sub on an excel macro, and it perfectly works.

I would like to avoid to listen keyboard events on each controls ...

Thanks for your tips.


SW2007 SP5
Worksatation HP wx4300 2GB
NVidia Quadro FX 3450
 
You may need to watch for event on all controls. It is the control that has focus that will see the keypress event.

The best way to do this is to make a function that would react to keys, then have each control keypress event simply call that function. This is less tedious than writing full code for each control. If you need to interrupt a loop, be sure the loop has DoEvents call within its body.

Sometimes, when testing a form, I put a temporary "Break" command button on the form. Easier than monitoring for KeyPress events.

[bat]Honesty may be the best policy, but insanity is a better defense.[bat]
-SolidWorks API VB programming help
 
Thanks TheTick,

That's what I've ever done, but I think it isn't clean ...

Whereas it works on Excel, I don't understand why it doesn't on SW ... !!!

Ho ! Thanks for your tip about DoEvents that I didn't know ; sure it's useful ...



SW2007 SP5
Worksatation HP wx4300 2GB
NVidia Quadro FX 3450
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top