ApplicationCommandTarget and duplicate defaultKeypresses

Hi Jules,

I am experiencing something strange in my app.
There are two different widget which inherits SimpleApplicationCommandTarget.
Those two share some key shortcut for some command. Cut/copy/paste in my case.

As soon as the second widget register those, the first one never get those back even if it takes back the focus.

What would be the best ways to achieve shortcut key for command on the currently focused widget ?


When a key is pressed, it needs to first work out which command should be invoked, and then tries to find a target for that command, so it can only have one binding for a keypress.

Why not just make both widgets respond to the same command in different ways?

Those two widget do not exactly works together they just happened to use the same key shortcut and have not been developed at the same time.
They both have their own ApplicationCommandTarget, so returns their own list with getAllCommands.
So when pressing the shared key, only the last ApplicationCommandTarget registered receive its perform notification.

Not sure how I can keep those two code quite separate and still have both respond to key shortcut when having focus.

Maybe if KeyPressMappingSet would handle multiple command for the same keypress, it would be able to achieve this.


I don’t think it’d make any sense to allow a key to trigger multiple commands - what would happen when you press it? Would all the commands get dispatched? That could be very confusing…

Well, yes all the command will be dispatched.
Then you can handle those or not in them if they have the focus in the callback but it’s up to you.
It’s not that weird to have two function react to the same shortcut.

But maybe I should just not use ApplicationCommandTarget and just override keyPressed
Or do my own ApplicationCommandTarget which use KeyListener and implement the same kind of mechanism.


That just doesn’t feel right to me…

For example, you could implement the “cut” command as a set of different commands, e.g. “cut text”, “cut image”, etc., and give them all the same keypress, but that’d be a crazy way to do it! Much better to have a single generic “cut” command that performs different tasks depending on the thing that receives it.

I see. if I use the same command id then it works fine.
I missed that.