Panel keyboard focus

How can I give my widgets text fields keyboard focus when they’re inside a panel?
Text fields already receive focus (they’re activated) when clicked but keyboard focus remains on previously activated window.

I’ve scoured the docs for hours, finding nothing about how to give a Plasma applet keyboard focus when inside a panel. Nor have I found a related setting.

I’m not sure you can, IIRC we do some trickery there to make sure that keyboard focus doesn’t stick around on the panel if the user is clicking something there.

You can try manually giving the panel focus with the appropriate shortcut (I think Meta+Alt+P works by default to cycle between panels) and see if it works while the panel has keyboard focus.

Thank you for replying! Sorry for my late reply.
Shortcut indeed works to switch focus amongst panels, sadly only activates the text fields, keyboard focus remain elsewhere (previously focused window).

Any way to programmatically (and temporarily) get around this?
Or if you could point to the trickery in the code and I can recompile Plasma with this removed for myself (widget isn’t gonna be published anyways).

To clarify; this isn’t about widget not having focus at all (it does), it’s specifically about keyboard focus for using text fields inside widget.

Another thing worth clarifying again, this is a widget that is fully and always be inside a panel.

Hm, it should move full keyboard focus - after pressing Meta+Alt+P, at least arrow keys and (Shift+)Tab and Space and Return should definitely be passed to the widget, so I’d be surprised if other input wouldn’t.

I just tried it with putting a Keys.onPressed handler in a compact representation on a panel, and it definitely sees regular letter presses as well.

(Maybe the text field itself doesn’t have focus within its FocusScope?)

Well, we do for Meta+Alt+P, so obviously yes. If there’s a convenient way to do it from a widget, I wish I knew, there’s at least one open bug I could fix with that.

Ah yes, it does!
Just didn’t try hard enough :grinning_face:
But it’s many keypresses before I get my field to have keyboard focus.
So Meta+Alt+P multiple times until panel gets focus. Then TAB many times until my widget is focused, then tabs multiple times yet again before my text field Finally gets focused.

I suppose that’s the next step, how to at least make sure the text fields are the “first focused” once inside widget. Do you know the attribute by chance? Do I declare on global widget attributes (Everything is wrapped in a PlasmoidItem) or on the TextField?
I’m using standard PlasmaComponents.

Actually it’s the widget itself I’d like to be default focused once panel has keyboard focus. Because I can just click the text field once widget itself is focused.
Easiest way is ofc to just move the widget to be first inside panel but if there’s another way it’d be optimal.
Can one set up Meta+Alt+P (or some other shortcut) to focus a specific panel? Nothing in plasmashell is exposed for this by default from what I can see

I suppose that’s the next step, how to at least make sure the text fields are the “first focused” once inside widget. Do you know the attribute by chance?

focus but the specifics depend a bit on the nesting. You can probably do something on the relevant representation like onActiveFocusChanged: if (activeFocus) { the IdOfTheThingIWantToFocus.forceActiveFocus(Qt.TabFocusReason)} to always get focus immediately there as soon as you focus the widget.

How this would interact with the panel’s own tab navigation and the arrow key navigation etc. would require testing and potentially a bit of trickery.

Actually it’s the widget itself I’d like to be default focused once panel has keyboard focus.

I’m not sure if it’s possible to do that.

Can one set up Meta+Alt+P (or some other shortcut) to focus a specific panel?

You can set a focus shortcut for specific panels in the Show Panel Configuration dialog.

Thank you very much for your help, forceActiveFocus did the trick.
I did it on Component.onCompleted though.
Also had no idea about focus shortcut in Show Panel Configuration, a classic “So obvious you miss it”.

So working setup for future reference;
I set Meta + N to focus the panel. Widget is now first item in panel so widget itself receives focus right away. I then set the aforementioned forceActiveFocus to the column containing my textfields.
So Meta + N from anywhere and I can start typing right away.

Only improvement would be some way of choosing Which widget to focus inside panel so it can be in any order (a future config option?), but I suppose I can get used to the layout.

It should be available from qml, so that the widget can decide to do that when its keyboard shortcut is pressed (which sends activated, so could just implement that in the handler).

The ability to set a shortcut to Lock/Logout and System tray widgets is much less useful now that they don’t automatically get focus anymore (and they were obviously written with that in mind).