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?
Printable View
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?
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
That the line that says..
Case Is = S
to
Case is = vbKeyS
Thai
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
Which is about the same as Thai wrote. Sorry Thai, hadn't seen your post yet.
Imar
heh, no worries =)
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.
set your keypreview on the form to TRUE and the form will look at any keydown event FIRST, voila.
Thai
Thai, that is what I want, Thank!