Hey there! 👋
It's been a while since I added to the Let's Learn Flutter series. I've been quite busy over the last months, but have some time to start writing again.
In this short post, I'll go over a little problem I encountered recently with using LogicalKeySet
to define keyboard shortcuts on a web app we're building at work. I'll explain what the issue was, how to fix it and in the end, explain some differences between LogicalKeySet
and SingleActivator
.
I recently needed to use shortcuts on a flutter project at work. I needed to have keyboard shortcuts, so I did the usual thing you do, look at the docs for them here.
I was in a bit of a rush and just glanced over the guide (a mistake on my part). So by the example in the guide, I decided to use LogicalKeySet to define the ShortcutActivators.
After implementing the feature, while testing, I found a weird behaviour with the LogicalKeySet in MacOS using the meta key, as reported in this issue
If you define your ShortcutActivator using LogicalKeySet, for example, let's say you want to detect cmd+b
(note this only happens when using the meta (or cmd) key on mac). If you press cmd+b and before 1 second press just cmd again, the action is called. Even if the b key is not pressed in conjunction with cmd.
I reported this behaviour and they told me, this was a known flaw of using LogicalKeySet and advised me to use SingleActivator instead. They also pointed me to this comments, which explains a bit more about the issue and why it works as it does.
Takeaways
Read the docs carefully
If I had read the docs carefully I would have read a note kinda hinting at this.
Prefer using SingleActivator instead of LogicalKeySet
This is, of course, if you can. There might be situations where you might need to define more than a single key.
SingleActivator limitations
As mentioned above, SingleActivator can only take a LogicalKeyboardKey as the trigger, as opposed to LogicalKeySet, which accepts a series of LogicalKeyboardKey instead.
More resources
Cheers!