Ramesh
Ramesh

Reputation: 43

Disable the escape key in dojo

I have a requirement to disable the escape key when the dialog is open.currently when i click the escape button the dialog closes and the transaction is submitting.I tried the following code snippet but its not working chrome.

                    dojo.connect(dialog, "onKeyPress", function(e){ 
                    var key = e.keyCode || e.charCode; 
                    var k = dojo.keys; 

                    if (key == k.ESCAPE) { 
                         event.preventDefault();

                     d.stopEvent(event);
                    } 
                    }); 

Could you please help on this..i have searched a lot and havent found a suitable solution for my problem. Thanks inadvance..

Upvotes: 3

Views: 2489

Answers (3)

Tariq
Tariq

Reputation: 2871

You can create an extension for the Dialog widget like this in a new file:

define(["dojo/_base/declare", "dijit/Dialog"],
    function(declare, Dialog){
    return declare(Dialog, {

        //Prevents the 'ESC' Button of Closing the dialog
        _onKey: function() { }

    });
});

save the file into dojo Directory (say: dojo/my/my_dialog.js), and instead of calling: 'dijit/Dialog', just call: 'my/my_dialog'.
this will save you the hard work of editing each Dialog call, And the same thing to the "dojox/widget/DialogSimple" Widget.

Upvotes: 0

bishop
bishop

Reputation: 39414

In the event you want to override the escape key in all dialogs (rather than a particular instance), you can use dojo/aspect:

require(['dojo/aspect', 'dijit/Dialog'], function (Aspect, Dialog) {
    Aspect.around(Dialog.prototype, '_onKey', function (original) {
        return function () { }; // no-op
    });
});

Upvotes: 3

Dimitri Mestdagh
Dimitri Mestdagh

Reputation: 44685

Dojo uses the _onKey event for accessibility. You can override it by using:

dialog._onKey = function() { }

I wrote an example JSFiddle, hitting the Escape key should not work anymore.

Upvotes: 7

Related Questions