.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Capturing escape key during a command

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
misterblobby
5213 Views, 7 Replies

Capturing escape key during a command

Hi all,

 

I have a vb.net Autocad add-in which runs an iterative routine with a while..wend loop that often takes a few minutes to finish.

 

The routine is very stable and always restores user control eventually, but if the user hits the escape key while it is running, a fatal error occurs in acad.exe.

 

Does anyone know if there is a way to detect that the escape key has been pressed so the routine is exited?

7 REPLIES 7
Message 2 of 8
SENL1362
in reply to: misterblobby

 

 

 

bool EscapePressed=false;

//before your while loop

Autodesk.AutoCAD.ApplicationServices.Application.PreTranslateMessage += new PreTranslateMessageEventHandler(myEscWatcher);

 

while(true)

{

    //do your work

   if (EscapePressed)

       break;

}

 

 

const int KEYDOWN= 0x0100;

void myEscWatcher(object sender, PreTranslateMessageEventArgs messageDetails)
{
if (messageDetails.Message.message == KEYDOWN && ((Keys)(int)messageDetails.Message.wParam & Keys.KeyCode) == Keys.Escape)
{
    //do something when ESCAPE has been pressed, for example

   EscapePressed=true;
}

Message 3 of 8

Message 4 of 8

Thanks guys,

 

Easy as that!

 

Just for convenience, below is the relevant piece of code from the first link above in VB. I had to place it in a few different places to catch the keypress reliably, but it works well if in the right spot.

 

 

If HostApplicationServices.Current.UserBreak = True Then
           Throw New Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.UserBreak, "Escape pressed")
            Exit Sub

End If

Message 5 of 8

It looks like it's not working anymore since AutoCAD 2015.

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
Message 6 of 8
ActivistInvestor
in reply to: FRFR1426


@FRFR1426 wrote:

It looks like it's not working anymore since AutoCAD 2015.


If you're calling UserBreak() from a non-interactive process, it will not work because it requires AutoCAD to process windows messages in order to detect the ESC keypress. While a non-interactive process is underway, AutoCAD normally doesn't process windows messages until the process returns control to it.

 

In that circumstance, the native, undocumented acedUsrBrkWithMessagePump() API has always been required, but this was never exposed to managed code.

 

acedUsrBrkWithMessagePump() is essentially nothing more than this:

 

 

   public static class HostApplicationServicesExtensions
   {
      public static bool UserBreakWithMessagePump(this HostApplicationServices hostapp)
      {
         System.Windows.Forms.Application.DoEvents();
         return hostapp.UserBreak();
      }
   }

 

 

Message 7 of 8
FRFR1426
in reply to: ActivistInvestor

I'm pretty sure I've tried with a DoEvents.

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
Message 8 of 8
ActivistInvestor
in reply to: FRFR1426


@FRFR1426 wrote:

I'm pretty sure I've tried with a DoEvents.


It works here (in-use on AutoCAD 2013-2017).

 

It's also used extensively to force AutoCAD to refresh the display after calls to Editor.WriteMessage() so they appear immediately.

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost