|
-
Feb 16th, 2007, 01:17 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Hitting the "Esc" key to close program
Hi Everyone,
What is the code that when you hit the "Esc" key, the program will end?
Thanks in advance.
-
Feb 16th, 2007, 01:28 PM
#2
Re: Hitting the "Esc" key to close program
Simple way
in the form properties.. set KeyPreview = True
VB Code:
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 27 Then Unload Me
End Sub
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Feb 16th, 2007, 01:33 PM
#3
Re: Hitting the "Esc" key to close program
Another way is to drop a standard command button on the form and enter 'Close' as its Caption property and have Unload Me in its click event. Then set the control's Cancel property to True. When you do, VB executes this button's Click() event whenever you press the [Esc] key (as well as when you click it).
-
Feb 16th, 2007, 01:38 PM
#4
Hyperactive Member
Re: Hitting the "Esc" key to close program
Or...
VB Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyEscape Then
Unload Me
End If
End Sub
I use this thread, to ask another question:
Is there a difference between KeyDown and KeyPress ?
DubweiserTM

If your question has been answered, you can mark a thread as resolved...
-
Feb 16th, 2007, 01:39 PM
#5
Thread Starter
Addicted Member
Re: Hitting the "Esc" key to close program
Thanks guys. That worked. Much appreciated.
-
Feb 16th, 2007, 02:03 PM
#6
Re: [RESOLVED] Hitting the "Esc" key to close program
 Originally Posted by DubweiserTM
I use this thread, to ask another question:
Is there a difference between KeyDown and KeyPress ?
Yes, but they can refer to the same key.
For example, the KeyCode for "A" is vbKeyA and this would be used in the KeyDown event.
The KeyAscii code for "A" is 65, so using KeyAscii Chr(65) in the Keypress event would be the same as using vbKeyA in the KeyCode of event of Keydown. It is confusing, I know.
Start up a standard exe project with a single textbox and do some tests.
VB Code:
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Debug.Print "KeyDown value: " & KeyCode
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Debug.Print """Ascii " & KeyAscii & " = " & Chr(KeyAscii)
End Sub
Play with pressing the various keys on the keyboard and the numeric keypad and the function keys. You'll see that KeyDown always gives you results. KeyPress only gives you results when there is a valid ASCII character being generated. In addition, the KeyAscii and KeyCode values don't always match, but some, if not most, are similar enough to be confusing.
-
Feb 16th, 2007, 02:15 PM
#7
Thread Starter
Addicted Member
Re: [RESOLVED] Hitting the "Esc" key to close program
Thanks Hack for explaining that to me. Very interesting.
-
Feb 16th, 2007, 02:15 PM
#8
Hyperactive Member
Re: [RESOLVED] Hitting the "Esc" key to close program
 Originally Posted by Hack
Yes, but they can refer to the same key.
For example, the KeyCode for "A" is vbKeyA and this would be used in the KeyDown event.
The KeyAscii code for "A" is 65, so using KeyAscii Chr(65) in the Keypress event would be the same as using vbKeyA in the KeyCode of event of Keydown. It is confusing, I know.
Start up a standard exe project with a single textbox and do some tests.
VB Code:
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Debug.Print "KeyDown value: " & KeyCode
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Debug.Print """Ascii " & KeyAscii & " = " & Chr(KeyAscii)
End Sub
Play with pressing the various keys on the keyboard and the numeric keypad and the function keys. You'll see that KeyDown always gives you results. KeyPress only gives you results when there is a valid ASCII character being generated. In addition, the KeyAscii and KeyCode values don't always match, but some, if not most, are similar enough to be confusing.
Thanks for this lesson !
DubweiserTM

If your question has been answered, you can mark a thread as resolved...
-
Feb 16th, 2007, 02:19 PM
#9
Re: [RESOLVED] Hitting the "Esc" key to close program
Another "gotcha" with ascii codes is there are different codes for capital and small letters.
Example: A = Chr(65)
a = Chr(97)
vbKeyA doesn't care whether you chose a capital or a small, so when dealing with single letters (like making a hot key combination), I always use the KeyCode.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|