Results 1 to 9 of 9

Thread: Short Cut Key

  1. #1
    Guest
    If I set Menu on a Form, I can also set a short cut for it, like Ctrl-S for Save, however, can I create a short cut key, like Ctrl-S to call the save routine of the document, but this "Save" item isn't exist in the Menu Bar?

  2. #2
    Addicted Member
    Join Date
    Mar 2000
    Posts
    168

    If I understand your question correctly, yes.

    You would setup a keypreview in the form and trap the control-s key to run your save procedure. Example:

    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    
      DIM ControlDown
    
      ControlDown = (Shift And vbCtrlMask) > 0
    
      If ControlDown Then
        Select Case KeyCode
          Case Is = S
            '<call your save procedure here!>
        End Select
      End If
    End Sub

  3. #3
    Addicted Member
    Join Date
    Mar 2000
    Posts
    168

    oops

    That the line that says..

    Case Is = S
    to
    Case is = vbKeyS

    Thai

  4. #4
    Guest
    Yes, you can.

    Use the Form_KeyDown event to "capture" the key-combination pressed. If applicable, you could also use a different object's KeyDown event.

    Example:

    Private Sub (KeyCode As Integer, Shift As Integer)
    If (Shift And 3) = 2 And KeyCode = vbKeyS Then
    ' Code to save here, or call DoSave() or whatever
    End If
    End Sub

    This code will catch the CTRL-S combination on the current form.

    If you want multiple key-combinations, use a select case statement:


    Private Sub (KeyCode As Integer, Shift As Integer)
    If (Shift And 3) = 2 ' CTRL is down
    Select Case KeyCode
    Case vbKeyS
    'Code for CTRL-s
    Case vbKeyA
    ' Code for CTRL-a
    End Select
    End If
    End Sub


    Hope this helps.

    Imar


  5. #5
    Guest
    Which is about the same as Thai wrote. Sorry Thai, hadn't seen your post yet.

    Imar

  6. #6
    Addicted Member
    Join Date
    Mar 2000
    Posts
    168
    heh, no worries =)

  7. #7
    Guest
    Thank Thai and Imar, I am tried your method, but I found that it is only what if I don't have any control that will got focus, e.g. If I have a text box on it, then the text box will got the focus so the key down event of the form will not fired. i.e. I need to write code for each object's key down, it will be difficult to maintain when I have many object on it. So I am now try to looking for another method that work no matter how many control on it, just like the Menu bar, it work no matter which control is got focus.

  8. #8
    Addicted Member
    Join Date
    Mar 2000
    Posts
    168

    do this..

    set your keypreview on the form to TRUE and the form will look at any keydown event FIRST, voila.

    Thai

  9. #9
    Guest
    Thai, that is what I want, Thank!

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