Author Topic: UIKeyNavigation keyboard control loses focus  (Read 8381 times)

diederik

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 31
    • View Profile
UIKeyNavigation keyboard control loses focus
« on: July 18, 2015, 10:55:59 AM »
Hi all,

I'm using the UIKeyNavigation for both controller and keyboard controls through my menu.
It seems, when I use the controller to select a Next button in my menu, the startsSelected on the next panel is neatly updated in the hoverObject.
And thus it becomes selected! Great!
But, when I use the keyboard, it seems that the hoverObject becomes null (which is pretty weird since it looks like UIKeyNavigation treats the keyboard as a controller!).
And nothing is selected!

Weird thing: when I disable the mouse events from my UICamera, it seems to work.
But as you've guessed: I also want mouse controls, and I don't want to create something that enables/disables this all the time.
I'd also like to avoid nasty scripts setting the hoverObject and selectedObject all the time (if this would even work as it might be overridden my the mouse).

Please help!

Diederik / Xform





Paul

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: UIKeyNavigation keyboard control loses focus
« Reply #1 on: July 18, 2015, 11:14:50 AM »
As far as I know, surprisingly NGUI doesn't support both mouse AND controller input simultaneously. I feel like this would be a common enough feature for anyone making a PC game that simply checking the Mouse, Keyboard and Controller bools in the UICamera under Event Sources would work, but I've found that it's possible to lose focus. This causes keyboard & controller inputs to no longer work until the mouse is used to update the selectedObject again.

As you've eluded to, I ended up using a custom UICamera.onHover, but I also had to add a hacky check to UICamera.selectedObject which doesn't allow selecting something without a UIKeyNavigation component. (as time goes on and I understand NGUI more, this may change)

To my surprise this seems to be working (I'm using buttons, checkboxes & sliders). For example, when I mouse over a ui button, it becomes selected and the previous ui button becomes deselected. When I mouse OUT of a ui button, it will REMAIN selected until I mouse over another button (or use the keyboard/controller to navigate).

If there is a better way, I'm all ears! Running Windows Unity 4.3 & NGUI 3.8.

EDIT: Damn, after reading the NGUI 3.9 updates, it seems like this issue was solved! At least worth trying anyway. =)

Quote
3.9.0
- NEW: Completely redesigned how controller-based input was handled. It's now much more robust and handless seamless transitions from one method of input to another.
- NEW: Added UICamera.controllerNavigationObject that explicitly tracks controller-based selection.
- NEW: NGUI now automatically finds and focuses on an appropriate UIKeyNavigation object if none has focus while receiving controller-based input.
« Last Edit: July 18, 2015, 11:24:09 AM by Paul »

diederik

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 31
    • View Profile
Re: UIKeyNavigation keyboard control loses focus
« Reply #2 on: July 19, 2015, 06:36:55 AM »
Hi Paul,

Thanks for reading my post and sharing your thoughts on this.

The quirky thing is: using the CONTROLLER it works pretty okay, but the moment I select/submit something by pressing Return, the mouse overrides the hover again.
And the startsSelected is cancelled out somehow.

I sense a dirty hack in my near future :)

I was already running the 3.90 version, but there's an even newer update I might try.

I'll report back when I've put some more work in this. FYI, I'm using the Unity native UI on a different game, and running into similar stuff :(.

Diederik / Xform

diederik

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 31
    • View Profile
Re: UIKeyNavigation keyboard control loses focus
« Reply #3 on: July 19, 2015, 10:23:39 AM »
Update:

Setting the UICamera.eventHandler.useMouse to false when keyboard use is detected (and enabling it when mouse use is detected again) seems like an okay workaround for now.

However... a new problem seems to pop up now. I'm not sure if I'll start a new thread for it.

When the Time.timeScale = 0 (game is paused) the keyboard input axis are not processed / coming through to navigate buttons.
I'm not sure if I should consider myself lucky that the CONTROLLER input IS working or if I should start crying.... ;)

I'm messing about with the InputManager axes, but cannot seem to trace how to fix it.

More help is much appreciated!

Diederik / Xform

Update 2:
I'm guessing the issue with the keyboard not working when the timescale is zero is not an NGUI related issue, but a more general Unity one. I have now hacked something in UICamera but I can imagine that setting up the Input manager axes properly somehow could make the Horizontal axis go to -1 or 1 immediately during pause. Although it remains strange why the controller input IS working. I probably need to investigate further....sigh...


« Last Edit: July 19, 2015, 10:56:01 AM by diederik »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIKeyNavigation keyboard control loses focus
« Reply #4 on: July 21, 2015, 11:36:22 AM »
There is no "keyboard" form of input. There are 3 forms of input NGUI understands:

1. Keyboard+Mouse
2. Touch
3. Controller

Note that Unity's input lets you assign keyboard keys to specific axes, such as "Horizontal" and "Vertical". The same axes are specified on the UICamera and are used for movement. If you do this, then as far as NGUI is concerned your keyboard actions result in controller input, and are treated as such.

diederik

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 31
    • View Profile
Re: UIKeyNavigation keyboard control loses focus
« Reply #5 on: July 22, 2015, 04:20:05 AM »
Hi Arenmook,

Thanks for dropping by!

The question was:

Why is the mouse position (hoveredObject) suddenly being sampled/raycast when I'm navigating my buttons by keyboard and I press submit?

This unwanted behavior does not occur when I'm using my controller. I've now fixed it by disabling the mouse input on UICamera when keyboard navigation is detected and enabling it when mouse activity is detected.

Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIKeyNavigation keyboard control loses focus
« Reply #6 on: July 24, 2015, 10:09:22 AM »
Well as I mentioned, pressing a keyboard key switches you to Keyboard+Mouse navigation scheme, so the mouse gets used.

diederik

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 31
    • View Profile
Re: UIKeyNavigation keyboard control loses focus
« Reply #7 on: July 26, 2015, 05:22:15 AM »
OK thanks for your reply!

So if you want to have keyboard navigation in your menu, you just need to disable the mouse input, otherwise the mouse hover will mess stuff up. Got it

I'm not sure if this is a great idea for a future change / fix, but you could consider enabling the mouse (+keyboard control scheme) only if mouse activity is detected, and not when just submitting a button with your keyboard.

Diederik / Xform