0

I am developing a Javascript library (AnyList) in which users may press the ESC key to abort editing an input field. This is implemented by catching the ESC keyup event (using jQuery):

inp_elem.on("keyup", init_opt, $.proxy(this._processKeyup,this));
...
$.any.DataView.prototype._processKeyup = function (event)
{
  if (event.preventDefault)
    event.preventDefault();
  if (event.type == "keyup" && event.which == 27) { // ESC. In Vivaldi, we never get here.
    ...
  }
  ...

This works fine in Firefox, Edge, Chrome, etc. but not in the Vivaldi browser - the _processKeyup method is never called.

Vivaldi uses the ESC key to stop the loading of a html page but so do other browsers, and even if I delete the ESC keyboard mapping (there is an option for this in Vivaldi), I am not able to catch the ESC event in my Javascript.

Can anybody help? I really like the Vivaldi browser and would like to see it succeed, but this behaviour breaks my script unneccessarily and is quite annoying.

Arne M
  • 87
  • 8
  • Tried to preventDefault ? – bZezzz Oct 21 '21 at 10:14
  • Well, preventDefault is called on the event that is delivered to the _processKeyup event listener method, but I never get that far. It is not clear to me how I should call preventDefault *before* _processKeyup is called. – Arne M Oct 21 '21 at 10:22
  • `onkeyup = function (event) { if (event.type === "keyup" && event.key === 'Escape') { // WORK IN VIVALDI alert('esc') } }` dunno if it's help, maybe you need to use your _processKeyup fnc. – bZezzz Oct 21 '21 at 11:13
  • @bZezzz Your method works because it catches keyup events on the global "document" object, whereas I try to catch events on a specific input element. If I redefine the onkeyup method on my input element, it still works for all keyup events but not for ESC: `inp_elem.get(0).onkeyup = function (event) { if (event.type === "keyup" && event.key === 'Escape') alert('esc - not in vivaldi'); else alert('not esc - in all browsers'); }` – Arne M Oct 21 '21 at 11:57
  • Vivaldi catch the `keyUp` so use `keyDown` :) `elem.onkeydown = function (event) { if (event.type === "keydown" && event.key === 'Escape') alert('esc - everywhere'); }` tell me if it working, I will make clean answser. – bZezzz Oct 21 '21 at 12:18
  • 1
    @bZezzz Yes, you are right! It is indeed possible to catch the keydown event in Vivaldi (but not keyup). I didnt even think to try it myself... :P This smells like a Vivaldi bug to me. Anyway, catching keydown for ESC is a reasonable workaround, so your answer is (or will be) accepted. :) – Arne M Oct 21 '21 at 18:57

1 Answers1

1

It seems that Vivaldi browser is catching the keyUp event, so you'd better to use keyDown event instead.

A working solution could be something like that:

elem.onkeydown = function (event) {   
    if (event.type === "keydown" && event.key === 'Escape') {
        alert('esc - everywhere'); 
        }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
bZezzz
  • 972
  • 9
  • 22