Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Focus not initially set in OKScene #11

Closed
chessboy opened this issue Oct 18, 2020 · 2 comments
Closed

Focus not initially set in OKScene #11

chessboy opened this issue Oct 18, 2020 · 2 comments

Comments

@chessboy
Copy link

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!

@ShinryakuTako
Copy link
Member

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!

@chessboy
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants