Script question: exit handlers and how to trap the Esc key?

 From:  Larry Fahnoe (FAHNOE)
9918.1 
I'm working on a script that creates leaders with coordinate values which are relative either to the world or a conditionally created CPlane. If a CPlane has been created, I'd like to be able to revert to the prior coordinate frame upon exiting the script. In other environments I'd be thinking of establishing an exit handler, but so far I've not found how to do that in MoI/Javascript. While clicking the Done and Cancel UI buttons doesn't pose much of a problem, the challenge I'm running into is that pressing the Esc key seems to throw an exception that has thus far bypassed my attempts to reliably trap it. Amusingly, adding some debugging moi.ui.alerts seems to disrupt the exception handling such that I can get the CPlane to be reset, but if I remove or turn off the debugging calls, it doesn't work; a little quantum uncertainty...

My first attempt was simple:

code:
function doCoordinates {
    if ( !WaitForDialogDone() )
        return;

    if ( moi.ui.commandUI.AdjustOrigin.value)
        moi.view.setCPlaneInteractive();

    [...]
}

var savedcf = moi.view.getCPlane();
doCoordinates();
moi.view.setCPlane( savedcf);


Pressing the Esc key would bypass restoring the CPlane on exit, so I wrapped the call to doCoordinates() in a try/catch/finally:

code:
function doCoordinates {
    if ( !WaitForDialogDone() )
        return;

    if ( moi.ui.commandUI.AdjustOrigin.value)
        moi.view.setCPlaneInteractive();

    [...]
}

var savedcf = moi.view.getCPlane();
try {
    doCoordinates();
}
catch( err) {
}
finally {
    moi.view.setCPlane( savedcf);
}


This didn't work reliably either (I suspect because there are nested functions calling the same pointpicker) so I started wrapping calls to pointpicker.waitForEvent() in try/catch but this seems excessively cumbersome. I've been using the scripts provided with MoI as a style guide and none seem to resort to this sort of effort to trap an Esc. DoCurve.js is amongst the most explicit in its handling of UI events, but even it doesn't address the Esc key. pointpicker.allowNestedCancel() doesn't seem to help either. I'm chasing my tail and clearly not understanding the MoI way of doing things!

EDITED: 6 Jul 2021 by FAHNOE