Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No, really. What is the proper way to handle keyboard input in a game using Cocoa?

Tags:

macos

cocoa

Let's say you're creating a game for Mac OS X. In fact, let's say you're creating Quake, only it's 2011 and you'd prefer to only use modern, non-deprecated frameworks.

You want your game to be notified when the user presses (or releases) a key, any key, on the keyboard. This includes modifer keys, like shift and control. Edited to add: Also, you want to know if the left or right version of a modifier key was pressed.

You also want your game to have a config screen, where the user can inspect and modify the keyboard config. It should contain things like:

  • Move forward: W
  • Jump: SPACE
  • Fire: LCTRL

What do you do? I've been trying to find a good answer to this for a day or so now, but haven't succeeded.

This is what I've came up with:

  • Subclass NSResponder, implement keyUp: and keyDown:, like in this answer. A problem with this approach, is that keyUp: and keyDown: won't get called when the user presses only a modifier key. To work around that, you can implement flagsChanged:, but that feels like a hack.
  • Use a Quartz Event Tap. This only works if the app runs as root, or the user has enabled access for assistive devices. Also, modifier key events still do not count as regular key events.
  • Use the HIToolbox. There is virtually no mention at all of it in the 10.6 developer docs. It appears to be very, very deprecated.

So, what's the proper way to do this? This really feels like a problem that should have a well-known, well-documented solution. It's not like games are incredibly niche.

like image 787
sarnesjo Avatar asked Apr 30 '11 22:04

sarnesjo


2 Answers

As others have said, there’s nothing wrong with using -flagsChanged:. There is another option: use the IOKit HID API. You should be using this anyway for joystick/gamepad input, and arguably mouse input; it may or may not be convenient for keyboard input too, depending on what you’re doing.

like image 128
Jens Ayton Avatar answered Oct 02 '22 15:10

Jens Ayton


This looks promising:

+[ NSEvent addLocalMonitorForEventsMatchingMask:handler: ]

Seems to be new in 10.6 and sounds just like what you're looking for. More here:

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSEvent_Class/Reference/Reference.html%23//apple_ref/occ/clm/NSEvent/addLocalMonitorForEventsMatchingMask:handler:

like image 22
nielsbot Avatar answered Oct 02 '22 14:10

nielsbot