.NET

.NET

Reply
Active Contributor
misterblobby
Posts: 28
Registered: ‎07-19-2004
Message 1 of 4 (391 Views)
Accepted Solution

Capturing escape key during a command

391 Views, 3 Replies
12-17-2013 10:33 PM

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?

Mentor
SENL1362
Posts: 232
Registered: ‎07-20-2011
Message 2 of 4 (379 Views)

Re: Capturing escape key during a command

12-18-2013 12:40 AM 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;
}

ADN Support Specialist
augusto.goncalves
Posts: 390
Registered: ‎04-30-2009
Message 3 of 4 (361 Views)

Re: Capturing escape key during a command

12-18-2013 05:28 AM in reply to: misterblobby

Hi,

 

AutoCAD has a property to determine whenever the ESCAPE was pressed, check the posts below:

 

http://adndevblog.typepad.com/autocad/2013/02/return-to-command-prompt-when-escape-is-pressed-using-...

http://adndevblog.typepad.com/autocad/2012/07/allowing-users-to-escape-from-long-operations-in-autoc...

 

Regards,



Augusto Goncalves
Autodesk Developer Network
Active Contributor
misterblobby
Posts: 28
Registered: ‎07-19-2004
Message 4 of 4 (338 Views)

Re: Capturing escape key during a command

12-18-2013 05:25 PM in reply to: augusto.goncalves

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

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.