Esc Key to terminate a running application

Esc Key to terminate a running application

Anonymous
Not applicable
467 Views
5 Replies
Message 1 of 6

Esc Key to terminate a running application

Anonymous
Not applicable
Hi All
I am having trouble detecting when the user presses the esc key. I placed
the following code in the userform...

Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = vbKeyEscape Then Unload Me
End Sub

but for some reason it's not working. Any help?

Thanks
Avantika
0 Likes
468 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
Try this:

Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

If KeyAscii.Value = vbKeyEscape Then Unload Me

End Sub
0 Likes
Message 3 of 6

Anonymous
Not applicable
Hi,

This only works if there are no controls in the form ,if any controls present say textbox or combobox then you have to call this code every time.
0 Likes
Message 4 of 6

Anonymous
Not applicable
Simple.

To fix that problem, change the KeyPreview property of the form (only works in VB), and use the Form_KeyDown event to catch the KeyStroke.

But the REAL way to do this is to have a button that you normaly use to close the window, and set its 'Cancel' property to true. If you don't have sutch a button, crate one and set its left position to -10000 (or anything that will bring it out of the form's visible field), and set its 'Cancel' property to true.

BTW.: Setting the Cancel property to True makes this button's click event execute when the default "cancel" button is pressed (in this case the Esc key).

If your stuck in VBA, then your only option is number 2 (Cancel property.)

Hope this helps
0 Likes
Message 5 of 6

Anonymous
Not applicable
How you do this depends on when you want to quit.

1. If you are attempting to execute your form's close code when the user hits the escape
key all you have to do is set the Cancel button's (or quit button) on your form .Cancel
Property to true. This will cause the button to activate when the user presses the excape
key.

2. If you want to stop executing a lengthy block of code when the user hits the escape key
then you can use the Windows API GetAsyncKeyState function (see the following example:

[code]
...
' Declarations section
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Public Const VK_ESCAPE = &H1B
...
Sub LongProcess()
Dim oEnt As AcadEntity ' Hodling object
Dim Aborted As Boolean ' Flag to indicate loop was aborted by user
Dim ForCnt As Long ' Set a loop counter
GetAsyncKeyState VK_ESCAPE ' Reset the buffer to clear last escape key press
Aborted = False ' Preset flag
ForCnt = 0 ' Preset counter
For Each oEnt In ThisDrawing.ModelSpace ' in a big drawing this may take a while
ForCnt = ForCnt + 1
If (ForCnt Mod 10) = 0 Then ' Process every 10 loops
DoEvents ' Allow windows to process other events
If GetAsyncKeyState(VK_ESCAPE) Then ' User hit excape key since last call
Aborted = True ' Tell the rest of the program we stopped
Exit For ' Stop processing
End If
End If
...
' code to work with oEnt
...
Next oEnt ' Process next entity
...
' The rest of the program Aborted tells us we quit the loop
...
End Sub
[/code]

Phil Custer, P.E.
Custer Services, Inc.
custer@landfillgas.com
0 Likes
Message 6 of 6

Anonymous
Not applicable
Thanks all of you.The cancel property helps.
0 Likes