Keymapping: should I just attach them all?

As mentioned in another thread, I’m working on my key-mapping module, trying to add support for Panda’s new input-device support.

My uncertainty right now is this: Since Panda requires that we manually attach any input devices that we want to use, am I correct in taking it that I shouldn’t just attach all devices when the game is run? (And automatically attach anything that’s connected?)

If so, then how do I determine which devices I should attach? (Especially at startup.)

(To be clear, I’m not talking about whether or not to attach devices when asking the player for key-bindings–in that case, I’m confident in attaching everything, as I don’t know which devices the player might want to use. The question is that of whether all devices should be attached at other times.)

It might help to look at ShowBase.py and see what attachInputDevice actually does. In fact, you’ll see that “attaching” just means “adding a node to the data graph that reads out the device, and then adding a ButtonThrower under that node that fires events”. So it’s quite simple: if you want to fire events automatically for every device, attach it, if you don’t want the events to be fired, don’t attach it.

You can also avoid the data graph altogether and manually read the button events from the device by periodically calling device.get_button_events().

Hmm… I’m not sure that that quite answers my uncertainty. Nevertheless, I do appreciate answer, and the information!

That said, I think that I now see what I might do:

  • When binding a key, attach all devices
    • If the device used to produce the binding is neither keyboard nor mouse, store it
    • Once binding is done, detach all unused devices
  • Each key-binding entry records the device-type that it’s associated with
    • This is saved in the key-binding file
  • When loading key-bindings:
    • Check the device-type. If it’s neither keyboard nor mouse, check whether we’ve already used a device of this type; if so, use that; if not, get the first device of that type (if available), and use that.
    • Store all devices so used, as when binding keys
      • (In fact, I think that my loading-method actually calls the key-binding method, so this should happen automatically)

I think that the above should do the job…