hey all, i wan't ask..
how make shortcut in form..,
did i've to use keyup or keydown event ..???

i need some reference pliss...

Dani AI

Generated

Short, practical plan: handle shortcuts at the form level and set the form’s KeyPreview property to True so the form gets keyboard events before the focused control. Use KeyDown/KeyUp for physical keys (F1, arrow keys, function keys) and KeyPress when you need the generated ASCII character (ESC is an ASCII character). This matches ’s and ’s points but gives a single, consistent place to keep all shortcuts. (learn.microsoft.com)

Example you can drop into the form (set KeyPreview in Load, then use KeyDown). This avoids wiring the same shortcut on every control and works for F1/ESC consistently:

Private Sub Form_Load()
    Me.KeyPreview = True
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyEscape
            Unload Me
            KeyCode = 0      ' stop further processing
        Case vbKeyF1
            Form2.Show vbModal
            KeyCode = 0
    End Select
End Sub

Use the VB key constants (vbKeyF1, vbKeyEscape) rather than hard numbers for clarity. You can stop default processing by clearing KeyCode (or KeyAscii in KeyPress). (learn.microsoft.com)

Notes and troubleshooting: detect modifier combos by testing the Shift bitmask (vbCtrlMask, vbAltMask, vbShiftMask) with If (Shift And vbCtrlMask) <> 0 Then .... Also remember a form’s Default/Cancel button can swallow Enter/Esc behavior — if a CommandButton has Cancel=True, Esc behavior may be different; in that case adjust the button or rely on the form-level handler. If a particular control still captures keys (multiline textboxes, ActiveX controls), either handle the control’s key events or keep the logic in the form and clear KeyCode to prevent propagation. (learn.microsoft.com)

Acknowledgements: this builds on ’s KeyPress idea and ’s F1 example, and follows ’s KeyPreview tip while showing a cleaner, form-level pattern.

Recommended Answers

All 7 Replies

I don't get what you want to exactly.
Please more specify.

hmmm....
i have one form, when i press esc in my keyboard form will close...
how do that with code...???

See if this helps :

Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 27 Then
    Unload Me
End If
End Sub
commented: helper +3

From memory, I think you also need to set the keypreview property on the form otherwise the keypress will be sent to the control that has focus

it's help me...

and i was have code for that too, this my code :

Private Sub TxtID_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbkeyF1 Then
Form2.Show 1
End if
End sub

This Thread Solve, thanks...

Good. :D
Don't Forget to mark this thread as Solved.

form_keypress
if keyascii=1 then
unload me
end sub

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.