Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski 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
Feb 18, 2007
32
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
 
Replies continue below

Recommended for you

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