AnKor
Topic Author
Posts: 21
Joined: 22 Jul 2022, 16:14

How to prevent keyboard focus from cycling?

04 Apr 2023, 10:25

Hi!
We are using Window class similar to the one in your app framework. And I'm trying to prevent the keyboard focus from cycling at its edges.
I've already tried KeyboardNavigation.DirectionalNavigation="Once" or even "None" and it doesn't seem do anything.
Overriding MoveFocus has no effect, the method seems to be bypassed.
Overriding OnPreviewLost(Got)KeyboardFocus and setting e.handled = true still allows the focus to be moved, albeit weirdly.

The only solution I've found so far is to set IsFocusEngagementEnabled=True on the whole window. This makes DirectionalNavigation="Once" work, but it messes up other things, so I'd like to avoid it if possible.

Are there other ways?

We are making customized keyboard/gamepad navigation, so I'm not averse to writing code to achieve it, but I don't see any place to plug my code in.
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: How to prevent keyboard focus from cycling?

04 Apr 2023, 12:16

Hi, have you tried to set the navigation mode to "Contained" in your Window? That mode indicates that focus won't get out of the Window and that it won't cycle.
 
AnKor
Topic Author
Posts: 21
Joined: 22 Jul 2022, 16:14

Re: How to prevent keyboard focus from cycling?

04 Apr 2023, 12:38

"Contained" works. Thank you!

I didn't think about it because docs say "focus returns to the first or the last item when the end or the beginning of the container is reached", so I thought it would be the same as "Cycle".
Now I see that this confusion comes from Microsoft's documentation. Curiously, .NET reference sources describe "Contained" slightly better: "Like cycle but does not move past the beginning or end of the container.", but every other place I see has that confusing description.
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: How to prevent keyboard focus from cycling?

11 Apr 2023, 12:50

Yeah, the documentation for Contained is a bit complex to understand. We will update it to make it clearer, like the one in .NET sources.
Thanks for your feedback!

Who is online

Users browsing this forum: camimamedov, Semrush [Bot] and 40 guests