InvadingOctopus/octopuskit

Focus not initially set in OKScene

chessboy opened this issue · 2 comments

Category: 'Behavior'

Describe the bug
I wouldn't categorize this as a bug, but I cannot for the life of me figure out how to set the focus (first responder) to the main window containing the OKScene without clicking inside it first. The result is that key commands are not being picked up until you click anywhere in the scene. I'm sure it's something simple I'm missing.

To Reproduce
Steps to reproduce the behavior:

  1. Pull my app's codebase: https://github.com/chessboy/biots
  2. Build and run the app (marvel at the nerd-beauty :) )
  3. Try to use the arrow keys to pan around the world
  4. Note the arrow keys do nothing and the Mac actually plays a beep tone to let you know you're typing into the void
  5. Click anywhere in the main window
  6. Use the arrow keys to pan around the world
  7. Now you can now pan around

Expected behavior
OKScene should be first responder when the scene is shown (I think)

Build Environment (what you're developing on)
- OctopusKit Version: '3.2.0'
- macOS Version: '10.15.7 (build)'
- Xcode Version: '12.0.1 (12A7300)'
- Swift Version: 'Apple Swift version 5.2.4'

Target Device (what you're compiling for)
- Device: 'macOS app'
- Target OS Version: 'macOS 10.15'

Additional context
Loving this framework... hope you like my app which builds heavily on OK!

Woo! Nice to see an actual project that uses my stuff! \(^▽^)/

Have you tried looking in the didMove(to:) method in OKScene.swift?

// Steal the focus on macOS so the player doesn't have to click on the view before using the keyboard.

I'm not sure how reliable this trick is, though; try searching around the net for how to force a view to steal focus/become the First Responder, and let me know when you find a solution that works for all cases!

Well that was easy!

	override func didMove(to: SKView) {
		super.didMove(to: to)
		
		// from ShinryakuTako: Steal the focus on macOS so the player doesn't have to click on the view before using the keyboard.
		#if os(macOS)
		to.window?.makeFirstResponder(self)
		#endif
	}

I will keep my eyes out for a more general solution. Thanks!