Results 1 to 9 of 9

Thread: [RESOLVED] Hitting the "Esc" key to close program

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    Resolved [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.

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Hitting the "Esc" key to close program

    Simple way

    in the form properties.. set KeyPreview = True

    VB Code:
    1. Private Sub Form_KeyPress(KeyAscii As Integer)
    2.     If KeyAscii = 27 Then Unload Me
    3. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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).

  4. #4
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Re: Hitting the "Esc" key to close program

    Or...
    VB Code:
    1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    2.         If KeyCode = vbKeyEscape Then
    3.                 Unload Me
    4.         End If
    5. 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...

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    Re: Hitting the "Esc" key to close program

    Thanks guys. That worked. Much appreciated.

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [RESOLVED] Hitting the "Esc" key to close program

    Quote 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:
    1. Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    2.   Debug.Print "KeyDown value: " & KeyCode
    3. End Sub
    4.  
    5. Private Sub Text1_KeyPress(KeyAscii As Integer)
    6.   Debug.Print """Ascii " & KeyAscii & " = " & Chr(KeyAscii)
    7. 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.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    Re: [RESOLVED] Hitting the "Esc" key to close program

    Thanks Hack for explaining that to me. Very interesting.

  8. #8
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Re: [RESOLVED] Hitting the "Esc" key to close program

    Quote 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:
    1. Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    2.   Debug.Print "KeyDown value: " & KeyCode
    3. End Sub
    4.  
    5. Private Sub Text1_KeyPress(KeyAscii As Integer)
    6.   Debug.Print """Ascii " & KeyAscii & " = " & Chr(KeyAscii)
    7. 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...

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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
  •  



Click Here to Expand Forum to Full Width