Try this out and adapt it. It has all the error trapping taking out.
The basics are; set a boolean when the command has started then set it to false when it ends. If the boolean is true at begincommand then the endcommand wasn't fired.
Option Explicit
Private PrevLayer As AcadLayer
Private CmdActive As Boolean
Function EscPrompt() As Boolean
Dim varCancel As Variant
varCancel = ThisDrawing.GetVariable("LASTPROMPT")
If InStr(1, varCancel, "*Cancel*") <> 0 Or _
InStr(1, varCancel, "") <> 0 Then 'this takes care of vbarun, toolbarsetc w/ ^C^C
If CmdActive > 0 Then
EscPrompt = True
End If
End If
End Function
Public Property Get oPrevLayer() As AcadLayer
If IsObject(PrevLayer) Then
If PrevLayer Is Nothing Then
Set oPrevLayer = ThisDrawing.Layers("0")
Set PrevLayer = ThisDrawing.ActiveLayer
End If
End If
Set oPrevLayer = PrevLayer
End Property
Public Property Let oPrevLayer(l As AcadLayer)
Set PrevLayer = l
End Property
Public Sub AcadDocument_BeginCommand(ByVal CommandName As String)
Dim oLayer As AcadLayer
If CmdActive Then
If EscPrompt Then
ThisDrawing.ActiveLayer = oPrevLayer
End If
End If
Select Case UCase(CommandName)
' blnActive = False
Case "BHATCH"
Set oLayer = ThisDrawing.Layers("T-Hatch")
CmdActive = True
ThisDrawing.ActiveLayer = oLayer
End Select
End Sub
Private Sub AcadDocument_EndCommand(ByVal CommandName As String)
If CmdActive Then CmdActive = False
Select Case UCase(CommandName)
Case "BHATCH"
ThisDrawing.ActiveLayer = oPrevLayer
Case Else
Exit Sub
End Select
Set PrevLayer = Nothing
End Sub
Draw rectangles and watch the layer properties manager as you start the Hatch command. Later try escaping. The fix isn't immediate, draw another rectangle and watch it change.