Zachycení stisku klávesy ESC

Postup:
V deklarační části formuláře zapište:

Private Type POINTAPI
    x As Long
    y As Long
End Type

Private Type MSGTYPE
    hWnd As Long
    message As Long
    wParam As Long
    lParam As Long
    time As Long
    pt As POINTAPI
End Type

Private Declare Function GetInputState Lib "user32" () As Long
Private Declare Function PeekMessage Lib "user32" Alias _
    "PeekMessageA" (lpMsg As MSGTYPE, ByVal hWnd As Long, _
    ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, _
    ByVal wRemoveMsg As Long) As Long

Private Const WM_KEYFIRST = &H100
Private Const WM_KEYLAST = &H108
Private Const PM_REMOVE = &H1

Public Function EscapePressed(Optional msgText As String) As Boolean
    Dim mess As MSGTYPE

    If GetInputState() Then
        PeekMessage mess, 0, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE
        If mess.wParam = vbKeyEscape Then
            If Len(msgText) = 0 Then
                ' Byla stisknuta klávesa ESC, vrátí True bez dotazu
                EscapePressed = True
            Else
                ' Byla stisknuta klávesa ESC, zobrazí se dotaz
                EscapePressed = (MsgBox(msgText, vbQuestion + vbYesNo) = vbYes)
            End If
         End If
    End If

End Function

Funkce testuje stisk klávesy ESC a může se dotázat uživatele na potvrzení stisku s textem definovaným pomocí proměnné msgText.
Příklad použití:
Do
   ' Zpracovávaný kód
   If EscapePressed("Opravdu si přejete přerušit zpracovávání ?") Then Exit Do
Loop

Zpět

Autor: The Bozena