1
votes

I have a site which uses heavy key events. For certain actions, it also opens a bootbox confirm box. The confirm box is shown over a transparent layer so all mouse click actions are not executed unless the user clicks on cancel or ok in the bootbox confirm. However I also want it that all keypress events should be disabled when the confirm box is active.

Current I have:

bootbox.confirm('blah blah'? function(yesPlease){
  if(yesPlease) {
     goAndClimbAVolcano();
  }
});

I can wrap this confirm under a disableAllKeyPress() and enableAllKeyPress() but I was wondering if there is an easier way to achieve this...

Any pointers are greatly appreciated.

1
@WhiteHat why are you struggling with why?Undefined Variable
sorry, I wasn't clear. what is the harm? what does key press allow the user to do? can they still get to other parts of the page when box is displayed?WhiteHat
$('').off('keyup keydown keypress'); $('').on('keyup keydown keypress'); stackoverflow.com/questions/15245294/…Korgrue
@WhiteHat being a single page application,it is difficult to explain all that happens in the page. All I can say is that its necessary for key press to be disabled for the duration that the confirm is shown.Undefined Variable
sounds tough if key press is already bound several places, you may need to hook into those processes to ignore when confirm box is displayed, or disconnect those events, then reconnect after. more code would helpWhiteHat

1 Answers

1
votes

I would use a boolean to lock events. Put a locked=true just before your confirm message appears, and locked=false after it closes.

Then you only have to ad if(locked) return; on your key event functions.

Hope that it helped.

EDIT: I know there are so many ways to do it, I just posted a different one :)