My mistake, my module also does joystick axes and mouse axes. I just used a keyboard key and a joystick button as an example. For BGInput, the supported events that bindings execute on are:
KEYDOWN = 0
KEYUP = 1
KEYPRESSED = 2
KEYRELEASED = 3
JOYBUTTONDOWN = 4
JOYBUTTONUP = 5
JOYBUTTONPRESSED = 6
JOYBUTTONRELEASED = 7
JOYHATDOWN = 8
JOYHATUP = 9
JOYHATPRESSED = 10
JOYHATRELEASED = 11
JOYAXIS = 12 # Return the percentage that the axis is being pressed (the axis values themselves)
JOYAXISDOWN = 13
JOYAXISUP = 14
JOYAXISPRESSED = 15
JOYAXISRELEASED = 16
MOUSEAXIS = 17 # Return the delta for the mouse’s movement on a specified axis
EDIT: Your code above is good, but it assumes that the player both has connected and wants to use a joystick. If you wanted to add an options screen where you could just tick “Joystick Enabled” or not, it would require rewriting your movement function.
My Python module’s pretty much the same, but pushes the joystick, keyboard, and mouse stuff together into a single class, and abstracts out the input handling from the input binding. So pressing left is pressing left, whether you use a joystick analog stick, or a D-Pad, or use WASD or the arrow keys on the keyboard, etc.
Also, pressing left on the joystick or pressing A on the keyboard can both trigger “left”, but can also treat an analog input as an axis value (a percentage) that gets returned. That way, the same code that handles moving for the keyboard can also handle slower movement for a partially “engaged” joystick.
EDIT 2: Here’s a quick movement function from a project that uses BGInput. It ties in friction, acceleration, maximum speed, and analog sensitivities (a lot simpler than I had it months ago, haha). Adding keyboard input should be trivial, as it’s already set up to work with a joystick movement mode, and keyboard movement would be pretty much the same, but just on or off.
def PlayerUpdate(cont, obj):
sce = obj.scene
camera = sce.active_camera
controls = logic.input.bindings
g_obj = obj['game_object']
m_vel = obj.worldLinearVelocity.xy
cam_rot = camera.worldOrientation.to_euler()
cam_rot.x, cam_rot.y = 0, 0
if m_vel.magnitude > g_obj.friction:
m_vel.magnitude -= g_obj.friction
move_vec = mathutils.Vector([controls['move_right'] - controls['move_left'], controls['move_up'] - controls['move_down'], 0.0])
analog_tilt = move_vec.magnitude
move_vec *= g_obj.acceleration
if (m_vel + move_vec.xy).magnitude <= g_obj.speed * analog_tilt:
m_vel += move_vec.xy
obj.worldLinearVelocity.xy = m_vel.xy
A down-side of my current system is that joystick, key, and axis presses and releases are all different bindings, along-side the existin button presses. So, you would have to add different bindings for each state of the keys that you need (i.e. “JumpReleased”, “ShootPressed”, etc.). Changing the system to add an ability to check what state a single binding is in, rather than adding new bindings for each status as it is currently shouldn’t be hard at all, though.
EDIT 3: It would appear I’ve forgotten to add mouse button detection to my module, though. Gotta add it to the to-do list.