I have many functions where I absolutely need to have the ability for the user to hit the escape key. Take a look at the following code where I put the user into a continuous loop to erase objects off the drawing which then deletes records from a database. For efficiency (like most of the commands in my application), I need to keep the user always in the function until escape is hit.
Dim acdoc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = acdoc.Database
Dim ed As Editor = acdoc.Editor
Dim HandleNm As String
Dim ShortNm As String
Dim TempStr As String
Dim FullDir As String
Using acdoc.LockDocument()
ShortNm = TempStr
Me.Hide()
Dim counterC As Short
Dim msgOut As String
Dim selEntity As Autodesk.AutoCAD.Interop.Common.AcadEntity
Dim rstFlag As New ADODB.Recordset
Dim Handle_Renamed As String
Dim sset As Autodesk.AutoCAD.Interop.AcadSelectionSet
Dim ArraySize As Short
Dim selPts() As Object
Dim Pt As Object
Do While counterC < 1000
Using acTrans As Transaction = db.TransactionManager.StartTransaction()
'' Request for objects to be selected in the drawing area
Dim selectionOpts As PromptSelectionOptions = New PromptSelectionOptions
selectionOpts.MessageForAdding = "Select the System Flag for deletion."
Dim acSSPrompt As PromptSelectionResult = acdoc.Editor.GetSelection(selectionOpts)
'' If the prompt status is OK, objects were selected
If acSSPrompt.Status = PromptStatus.OK Then
Dim acSSet As SelectionSet = acSSPrompt.Value
'' Step through the objects in the selection set
For Each acSSObj As SelectedObject In acSSet
If Not IsDBNull(acSSObj) Then
Dim acEnt As Entity = acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite)
If Not IsDBNull(acEnt) Then
If TypeOf (acEnt) Is BlockReference Then
Dim blkDel As BlockReference
blkDel = acEnt
If InStr(1, blkDel.Name, "FLAG") > 0 Then
HandleNm = blkDel.Handle.ToString
'MsgBox(HandleNm)
acEnt.Erase()
acEnt.Dispose()
rstFlag.Open("SELECT * FROM FLAG WHERE HANDLE = '" & HandleNm & "' AND PID = '" & ShortNm & "';", dbs, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
If rstFlag.EOF = False Then
rstFlag.Delete()
rstFlag.Update()
Else
End If
rstFlag.Close()
End If
End If
End If
End If
Next
End If
acTrans.Commit()
End Using
rstFlag = Nothing
counterC = counterC + 1
Loop