Flutter Focus onKeyEvent triggers for both key down and up

324 Views Asked by At

The Focus widget in Flutter can detect and handle KeyEvents. However, it will trigger on both key down and up events (so it calls its assigned callback twice for each press). Given a KeyEvent, how can I detect if it was a key down or up event so I can make the callback only react to one of those two events?

Thanks!

1

There are 1 best solutions below

0
Microbob On

This really needs to be in the docs.

The KeyEvent passed is really one of the few subclasses that include KeyDownEvent and KeyUpEvent so you can check the type with

if(event is KeyDownEvent) {
  // ...
}