TheSwamp

Code Red => VB(A) => Topic started by: David Hall on February 12, 2007, 02:14:17 PM

Title: ESC in sub
Post by: David Hall on February 12, 2007, 02:14:17 PM
OK, maybe I dont get it, but how can you trap the ESC key in a form?  What I'm trying to do is trap whether a user hits ESC in one of my subs.  Let me back up first, I have a form, that when a user clicks a button, it calls a sub in a module.  What I'm trying to avoid is 50 public subs.  What I want is to call a private sub.  Since you cant call a private sub in a differnt module, do I need to get rid of my modules and put everything in the code portion of the form?  Is there anything wrong with this approach?  When I tried to declare a constant for the ESC key, you can't declare constants in the form object. (Or else I'm doing that wrong as well)  Im trying to use RR's routine from this (http://www.theswamp.org/index.php?topic=9301.0) post.

Any ideas....
Title: Re: ESC in sub
Post by: Keith™ on February 12, 2007, 03:17:29 PM
I still don't understand what you are trying to do ... Are you trying to stop a person from hitting the ESC key and exiting a form? I don't think it works that way .. but, you can grab the value in queryclose and keep the form open, if you simply want to know if the user pressed the esc key, you should be able to return the value from the KeyDown event, but it is pretty funny and I don't trust it
Title: Re: ESC in sub
Post by: David Hall on February 12, 2007, 03:39:02 PM
I still don't understand what you are trying to do ...
I had no error handler for If the user pressed ESC.  If you do, it generates an Unhandled Err Exp.  Thus, I was trying to handle it gracefully.  What I have ended up doing as a test is to put the code in each module (1 for now as a test) and I have begun testing every which way to press the ESC key and seeing if it catches it.

On a side note, I have changed all my subs to Functions, and that removed them from the list of macros that can be run.  Any downside to this?  I always use subs when there is no value returned, and Functions when I want to return a value.  In C#, you can declare it VOID, but VBA doesn't use that (not that I'm aware of).
Title: Re: ESC in sub
Post by: Keith™ on February 12, 2007, 04:12:59 PM
Have you tried keyboard hooks or GetAsyncKeyState?

Try looking at these two articles, I would post an example, but it is much to complex to post here.
http://www.codeguru.com/vb/gen/vb_system/keyboard/print.php/c4829/
http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=41506&lngWId=1
Title: Re: ESC in sub
Post by: David Hall on February 12, 2007, 04:27:13 PM
Have you tried GetAsyncKeyState?

Yes, thats what I used