This depends on your implementation. When you use the keyboard sensors you can reconfigure the sensors with python.
mappingA:
import bge
scene = bge.logic.getCurrentScene()
cube = scene.objects["Cube"]
cube.sensors["turn CCW"].key = bge.events.UPARROWKEY
cube.sensors["turn CW"].key = bge.events.DOWNARROWKEY
or another mappingB:
import bge
scene = bge.logic.getCurrentScene()
cube = scene.objects["Cube"]
cube.sensors["turn CCW"].key = bge.events.IKEY
cube.sensors["turn CW"].key = bge.events.KKEY
Executing such a script will override the default setting that you entered via GUI.
To store/restore the sensors of all objects you can use this code:
import bge
try:
keyboardSettings = bge.logic.globalDict["keyboardSettings"]
except KeyError:
keyboardSettings = {}
bge.logic.globalDict["keyboardSettings"] = keyboardSettings
def storeKeyboardSensors(controller):
if not all(sensor.positive for sensor in controller.sensors):
return
scene = controller.owner.scene
for object in scene.objects:
sensorSettings = {}
keyboardSettings[object.name] = sensorSettings
for sensor in object.sensors:
try:
sensorSettings [sensor.name] = sensor.key
except AttributeError:
pass
def restoreKeyboardSensors(controller):
if not all(sensor.positive for sensor in controller.sensors):
return
scene = controller.owner.scene
for objectName in keyboardSettings:
object = scene.objects[objectName]
sensorSettings = keyboardSettings[objectName]
for sensorName in sensorSettings:
try:
object.sensors[sensorName].key = sensorSettings[sensorName]
except KeyError:
pass
The resulting change to the storage (globalDict) can be saved to file by using a saveActuator. You can load form file with the loadActuator. Be aware you need to store the keyboard mapping BEFORE saving and restore the mapping AFTER loading.
Remarks:
- this code does not care additional keys (there can be up to two modifier keys)
- this code does not provide an keyboard mapping editor (the snippet is simple code)
- this mapping snippets expect “Cube” with the keyboard sensors “turn CCW” and “turn CW”
Here is a simple demo:
Attachments
SaveKeyboard.blend (433 KB)