Specifying DisplayObject as receiver of KeyboardEvents

I would like to specify that KeyboardEvents are sent to a specific DisplayObject. But it seems that after setting KeyboardEvent.KEY_UP and KeyboardEvent.KEY_DOWN handlers, that DisplayObject doesn’t receive those events until after something else occurs. It will receive other events.

For example, I made an onscreen keyboard that responds to button clicks and keypresses. When that onscreen keyboard is created and displayed, it won’t receive any KeyboardEvents. But after a button is clicked, then it does.

What would cause this?
What’s the fix?

Thanks in advance!

Perhaps set stage.focus?

stage.focus = targetDisplayObject;

A display object must be focused to receive KeyboardEvents. If you click on another display object with the mouse, or press the Tab key on the keyboard, the focus will change to the other display object.

However, you can also listen for the FocusEvent.MOUSE_FOCUS_CHANGE or FocusEvent.KEY_FOCUS_CHANGE events. If you call event.preventDefault() in your listeners for these events, it will prevent the focus from changing to another display object.

Tried that and even though I can verify that stage.focus has been changed to the desired DisplayObject, keyboard events are not being received until after I click a button on it.