Removing Focus in a Window

I’m having a lot of fun trying to get Client Keystroke Event Scripts to be the entities that capture Left & Right Arrow Key Presses. It seems like almost everything consumes those before the Client Event system gets to them. In my window there is a slider that the operator used to set motor speed. The motor drives a pump and the system controls the flow by opening and closing analog check valves. The Left/Right arrow keys are supposed to control these check valve positions. Unfortunately once the operator adjusts the speed the slider has focus and consumes the arrow button presses.

If the arrow keys are pressed right after the window is loaded they work as desired.

In an effort to figure out what to give focus to after the motor speed was set I added the following code to my internalFrameActivated event script for the window:

from java.awt import KeyboardFocusManager
focusedComponent = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()
print focusedComponent.getName()

After adding this and navigating to the window the console output for the component name was “None”.
At a point I saved, published, updated again while still on this window and the component name was different. It was actually the project name.

So the question, is there a way to set the focus to NOTHING (remove it from all screen components)?
What about setting it to the project itself?

1 Like

I’m a total noob with java, but this link for no focus button seems to maybe offer a clue?
EDIT
I actually found this and it does work on removing focus. Just put it in an appropriate event handler.

event.source.setFocusable(0)

Hey dkhayes,

I tried that, but it just moves the focus to the next item in the focus order.
In my case that is a button and it seems that buttons consume the left/right arrow key pressed events, so it isn’t a fix for my issue.

I appreciate the help though!

According to this, you can't have nothing focused, so try the script in the above post for focusing to the root container
EDIT
Also, consider using different keys. the function keys, which I tested, worked no matter which component had focus
Finally, you can use control+left and control+right with a button in focus and it works

From my testing, using the code snippet in my original post in the internalFrameActivated event script, when the window first loads there is no focused component. That code segment actually throws an error stating that “NoneType” has no getName() method.

My hope is that if, when the window initially loads, there is nothing focused, that there would be a way to remove focus from everything via a script method. My hopes may be misplaced, but, as they say, it springs eternal! :smile:

You can try the following to set focus back to the root container.

root = event.source.parent
root.setFocusable(True)
root.requestFocusInWindow()
root.setFocusable(False)