local controller = { device="", displayName="" }
local function setDevice( device, displayName )
-- Set current controller
controller["device"] = device
controller["displayName"] = displayName
-- Remove event listeners
Runtime:removeEventListener( "axis", onAxisEvent )
Runtime:removeEventListener( "key", onKeyEvent )
end
local function onKeyEvent( event )
setDevice( event.device, event.device.displayName )
end
local function onAxisEvent( event )
if ( math.abs(event.normalizedValue) > 0.5 ) then
setDevice( event.device, event.device.displayName )
end
end
Runtime:addEventListener( "axis", onAxisEvent )
Runtime:addEventListener( "key", onKeyEvent )
I do not understand what the removeEventListener call should do and what is it for
I first thought that this would remove the events once the gamepad is detected, but the setDevice function is called each time the controller is used!
finally, I wonder if it is really useful for something. I wonder if the function removeEventListener really does something (in this precise case).
The only function for that code is to detect a device’s input, be it key or axis event.
The moment that it has received either input, it will store the device that the input came from. Once it knows what device the input came from, then there’s no more need to listen for events and so the event listeners are removed.