Results 1 to 3 of 3

Thread: Simple question...to which I don't know the anwser!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    Berkshire
    Posts
    121

    Question Simple question...to which I don't know the anwser!

    How can I create an event procedure that responds to a particular key press? For example, exit the program when the escape key is pressed?

    Also, following on from that how to set up procedure that are activated when for example, ctrl+shift+S is pressed?

    Thanks Darren.

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    First Question :

    VB Code:
    1. Private Sub Form_KeyPress(KeyAscii As Integer)
    2.     If KeyAscii = 27 Then End
    3. End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    There is a couple of ways on how you can do this.
    The first is to create a menu and asign the shortcut keys to that menu item. The click event for the menu item will then be raised when the shortcut is pressed.
    The other way is to use the KeyDown event of the form (you must set the KeyPreview property to True for this to work) and check what key combination that have been pressed in that event and then call any procedure you like.
    VB Code:
    1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    2.     'the KeyPreview property of the form must be set to True
    3.     'this property can be set during design-time
    4.     Select Case KeyCode
    5.         Case vbKeyEscape
    6.             Unload Me
    7.         Case vbKeyS
    8.             If (KeyCode And vbShiftMask) = vbShiftMask And (KeyCode And vbCtrlMask) = vbCtrlMask Then
    9.                 MsgBox "Ctrl+Shift+S have been pressed", vbInformation
    10.             End If
    11.     End Select
    12. End Sub
    Best regards

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