Accessing MFI Key/Axis Events From a Separate Module

Trying to access (in story.lua) an event (MFI controller key/axis events) assigned to a variable within a separate module (controller.lua).  Every attempt made either through creating composer variables (setting the function to a composer variable in controller.lua and getting/attempting to run in story.lua) or assigning the controller key/axis events to global variables in controller.lua and trying to access them in story.lua, has been without success.

—set composer variable example— 

controller.lua:
 

local controller = {} -- Get Input Device Descriptor local inputDevices = system.getInputDevices(); for i = 1,#inputDevices do local device = inputDevices[i]; print(device.displayName); end -- Check the Status of the Input Device local function onInputDeviceStatusChanged( event ) if ( event.device.isConnected ) then --if ( event.connectionStateChanged ) then if ( event.device.isConnected ) then -- Device has been connected print(event.connectionStateChanged); print(event.device); print(event.name); print(event.reconfigured); print("Device is connected"); else -- Connection has been lost print("Device not connected"); end end end Runtime:addEventListener( "inputDeviceStatus", onInputDeviceStatusChanged ); ---------------------------------------------------------------------- -- BEGIN COMPOSER SETVARIABLE EXAMPLE ---------------------------------------------------------------------- -- Handle all Key Events function onKeyEvent( event ) setDevice( event.device, event.device.displayName ); if (event.phase == "down") then keyPos = event.keyName; end return keyPos; end composer.setVariable( "function\_onKeyEvent", onKeyEvent ) Runtime:addEventListener( "key", onKeyEvent ); return controller; ---------------------------------------------------------------------- -- END COMPOSER SETVARIABLE EXAMPLE ----------------------------------------------------------------------

story.lua:
 

-- include the controller module local controller = require( "scripts.controller" ); ---------------------------------------------------------------------- -- BEGIN COMPOSER GETVARIABLE EXAMPLE ---------------------------------------------------------------------- local function\_getKeyEventFunc = composer.getVariable( "function\_onKeyEvent" ) —Call the function... function function\_getKeyEventFunc( event ) if ( event.phase == "down" ) then print( event.keyName ) end end ---------------------------------------------------------------------- -- END COMPOSER GETVARIABLE EXAMPLE ---------------------------------------------------------------------- 

 
 

—or global variable example—
 
 
 
controller.lua:

---------------------------------------------------------------------- -- BEGIN ASSIGN GLOBAL VARIABLE EXAMPLE ---------------------------------------------------------------------- local controller = {} -- Handle all Key Events function controller.onKeyEvent( event ) setDevice( event.device, event.device.displayName ); if (event.phase == "down") then keyPos = event.keyName; end return keyPos; end Runtime:addEventListener( "key", controller.onKeyEvent ); return controller; ---------------------------------------------------------------------- -- END ASSIGN GLOBAL VARIABLE EXAMPLE ---------------------------------------------------------------------- 

 
 
 
story.lua:
 

---------------------------------------------------------------------- -- BEGIN READ GLOBAL VARIABLE EXAMPLE ---------------------------------------------------------------------- -- include the controller module local controller = require( "scripts.controller" ); -- ... other code ... Runtime:addEventListener( “key”, controller.onKeyEvent ) ---------------------------------------------------------------------- -- END READ GLOBAL VARIABLE EXAMPLE ---------------------------------------------------------------------- 

 
I am either getting a “nil” value or a blank value or (function: 0x7fe905467e60 - which I should be able to parse like tables?) when I press a key on the controller or turn the thumb axis’.  I can print out the key/axis events from controller.lua with no problems, but I want to be able to access those events in story.lua so that I can handle all user interactions from story.lua (both files are in the scripts directory).  I have tried searching for all kinds of examples (here - Rob’s examples and stackoverflow - ranging from accessing functions/variables from different modules to global variables etc…). Can anyone recommend a working solution?

Finally got this to work using the dispatchEvent (Runtime Object), where I set the Runtime:dispatchEvent in the onKeyEvent function and assigned a custom event from the keyPos = event.keyName variable (in controller.lua):

-- Handle all Key Events function onKeyEvent( event ) setDevice( event.device, event.device.displayName ); if (event.phase == "down") then keyPos = event.keyName; print(keyPos); -- assign the keyPos to the Runtime:dispatchEvent Object Runtime:dispatchEvent( { name = "controllerKeyPos", key = keyPos } ); end return keyPos; end

and accessed it in the story.lua via:

local keyEventListFunc = function( event ) print( event.name ) print( event.key ) end ------------------------------------------------------------------ -- START RUNTIME EVENT DISPATCH LISTENER -- listen for the keyPos Runtime:dispatchEvent Object events from controllerClass Runtime:addEventListener( "controllerKeyPos", keyEventListFunc );

Finally got this to work using the dispatchEvent (Runtime Object), where I set the Runtime:dispatchEvent in the onKeyEvent function and assigned a custom event from the keyPos = event.keyName variable (in controller.lua):

-- Handle all Key Events function onKeyEvent( event ) setDevice( event.device, event.device.displayName ); if (event.phase == "down") then keyPos = event.keyName; print(keyPos); -- assign the keyPos to the Runtime:dispatchEvent Object Runtime:dispatchEvent( { name = "controllerKeyPos", key = keyPos } ); end return keyPos; end

and accessed it in the story.lua via:

local keyEventListFunc = function( event ) print( event.name ) print( event.key ) end ------------------------------------------------------------------ -- START RUNTIME EVENT DISPATCH LISTENER -- listen for the keyPos Runtime:dispatchEvent Object events from controllerClass Runtime:addEventListener( "controllerKeyPos", keyEventListFunc );