开发者

Keyboard handling ++

开发者 https://www.devze.com 2023-02-13 17:51 出处:网络
My app has a datagrid as well as many other UIComponents (buttons, menus, etc). I have a general stage KeyDown handler as well as a specific one for the datagrid. The behavior I\'m trying to i开发者_运

My app has a datagrid as well as many other UIComponents (buttons, menus, etc). I have a general stage KeyDown handler as well as a specific one for the datagrid. The behavior I'm trying to i开发者_运维知识库mplement is turning out to be trickier than I thought: basically, when using the keyboard arrows I would like the datagrid to always hear about such commands, even if it's not in focus.

Possible approaches I thought of:

  • have the DG be in focus at all times: seems stupid / tricky - basically would need to remember to give focus back to DG after any interaction with the rest of the UI, which obviously doesn't scale.

  • try to set the DG in focus from stage KeyDown handler: seems like the timing doesn't work since the DG gets focus too late to "hear" the event.

  • from the stage keydown handler, check if the DG is not in focus, and if so pass it the event. This seems to make sense in theory, but I'm not sure how to do this properly since my stage keyDown handler uses capture, and thus seems to catch the same event over and over.

I'm wondering if there's something much simpler that I'm simply missing due to inexperience. Would love to hear any of your thoughts.

thank you!

f


I would go with the third option, passing the stage keydown event to the DataGrid… But only if the event's default action hasn't been prevented. Then simply event.preventDefault() in the DataGrid handler and you should be good to go.

For example:

stage.addEventListener(..., function(event:KeyboardEvent):void {
    if (event.isDefaultPrevented())
        return;
    dataGrid.dispatchEvent(event);
});

dataGrid.addEventListener(..., function(event:KeyboardEvent):void {
    event.preventDefault();
    ... handle event ...
});
0

精彩评论

暂无评论...
验证码 换一张
取 消