From: David on
Mr. Petersons "Key Preview" reminder resolved things easily.

Mr. Toews and MikeD.

Out of curiousity and future need, is there anyway to code for the "Esc" key
in the command_click event?
If you can trap it in command_click, would definitely reduce need for
multiple command buttons.

David


"MikeD" <nobody(a)nowhere.edu> wrote in message
news:%23jruDc6zKHA.5548(a)TK2MSFTNGP06.phx.gbl...
>
>
> "David" <NoWhere(a)earthlink.net> wrote in message
> news:OdAtbL4zKHA.4656(a)TK2MSFTNGP05.phx.gbl...
>> I have a form with several controls.
>>
>> Rather than writing code for each keypress or a single
>> procedure call by every controls keypress event,
>> what is the best way to globally trap the escape key?
>
>
> I'd go with Tony's suggestion (set a command button's Cancel property to
> True). There are circumstances, however, where it may not suit your
> purposes. You'll just need to test for those. Make this an EXTRA command
> button on the form unless you've already got a button that should run this
> code whenever the Escape key is pressed. Either at run-time (before the
> form is shown) or design-time (I'd recommend run-time), move it way off
> the form by setting its Left or Top property to something like -10000.
> You can't make it invisible; otherwise, its Click event won't fire when
> the Escape key is pressed.
>
> --
> Mike
>
> P.S. If it wasn't clear, write the code you want executed when the Cancel
> key is pressed in that command button's Click event. You can even run it
> programmatically by setting that command button's Value property to True
> (the programmatic way of "clicking" a command button).
>
>


From: Tony Toews [MVP] on
"David" <NoWhere(a)earthlink.net> wrote:

>Mr. Toews and MikeD.
>
>Out of curiousity and future need, is there anyway to code for the "Esc" key
>in the command_click event?
> If you can trap it in command_click, would definitely reduce need for
>multiple command buttons.

Not sure so I'll let someone else answer that one. I'm an Access
expert so I only answer VB6 questions about which I'm really sure.
Especially after I answered one question with my experience from
Access which turned out to be totally wrong in VB6. <wry smile>

Tony
--
Tony Toews, Microsoft Access MVP
Tony's Main MS Access pages - http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
For a convenient utility to keep your users FEs and other files
updated see http://www.autofeupdater.com/
Granite Fleet Manager http://www.granitefleet.com/
From: Eduardo on
David escribi�:

> No one suggested a keyboard hook?

There is no need.
From: ralph on
On Mon, 29 Mar 2010 22:34:02 -0400, "David" <NoWhere(a)earthlink.net>
wrote:

>Mr. Petersons "Key Preview" reminder resolved things easily.
>
>Mr. Toews and MikeD.
>
>Out of curiousity and future need, is there anyway to code for the "Esc" key
>in the command_click event?
> If you can trap it in command_click, would definitely reduce need for
>multiple command buttons.
>
>David
>

Well you can't necessary trap the 'Escape' key but you can test to see
if you want to close the form - when the [x] is clicked....

Sub Form_QueryUnload (Cancel As Integer, UnloadMode As Integer)
On Error Resume Next
'UnloadMode possibilities:
'0 The user has chosen the Close command from the Control-menu
box on the form.
'1 The Unload method has been invoked from code.
'2 The current Windows-environment session is ending.
'3 The Microsoft Windows Task Manager is closing the
application.
'4 An MDI child form is closing because the MDI form is closing.

If UnloadMode <> 1 Then
Cancel = True
' free to continue with program
End If
End Sub

You can abuse that to keep from "unloading".

-ralph
From: Helmut Meukel on
David,

setting a command buttons Cancel property to True generates
automatically a click for this command button whenever the user
presses the Escape key.

So the code in the command buttons click event just has to handle
whatever should be done when the user presses Escape.
I you name the command button "Cancel" and make it visible to
the user he can either click on the command button or press
Escape.
If you don't want this Cancel button visible, move it out of sight
by setting either Top or Left to a suffient high negative value.
Its Visible property must stay True, but you should remove it
from the Tab order by setting its TabStop property to False.

Another approach would be - as Karl suggested - to use the
forms KeyPreview property. Look it up in Online Help.

Helmut.


"David" <NoWhere(a)earthlink.net> schrieb im Newsbeitrag
news:eOBR1F7zKHA.6140(a)TK2MSFTNGP05.phx.gbl...
> Mr. Petersons "Key Preview" reminder resolved things easily.
>
> Mr. Toews and MikeD.
>
> Out of curiousity and future need, is there anyway to code for the "Esc" key
> in the command_click event?
> If you can trap it in command_click, would definitely reduce need for
> multiple command buttons.
>
> David