Cross Browser Event Handler in Dart

I use the M1 release and wrote a key handler that looks like this:

void _onKeyPress(KeyboardEvent e) { switch (e.keyIdentifier) { case KeyName.UP: case KeyName.LEFT: e.preventDefault(); prev(); break; case "U+0020": // FIXME Must be a better way, or? case KeyName.DOWN: case KeyName.RIGHT: e.preventDefault(); next(); break; } } 

It works fine in Chrome, but not in FF or IE. There must be a better way to deal with the gap and still keep it in one switch, right? I know that I can look at other fields to get space, but this is also not a good alternative (since then I would decompose the code into two switch statements.) In any case, the problem with it does not work in FF and IE is worse If I rewrite using keyCode , then it works fine in all browsers.

 void _onKeyPress(KeyboardEvent e) { switch (e.keyCode) { case 38: case 37: e.preventDefault(); prev(); break; case 32: case 40: case 39: e.preventDefault(); next(); break; } } 

My problem is that I cannot find constants for virtual key codes. Am I doing something wrong? What is the best way to handle Dart-compatible cross-browser key events?

+2
source share
2 answers

Based on your question, another answer, this source code , and this error (which you should put), here are my recommendations:

Updated . This has been fixed .

+1
source

I think this is a similar problem for this issue . KeyIdentifier seems to return null in Firefox.

By the way, instead of "U + 0020" you can use KeyName.SPACEBAR

0
source

Source: https://habr.com/ru/post/973133/


All Articles