Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Accessing MFI Key/Axis Events From a Separate Module
Started by douglascavanaugh Apr 29 2017 11:06 AM

1 reply to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

douglascavanaugh

[GLOBAL: userInfoPane.html]
douglascavanaugh
  • Enthusiast

  • 32 posts
  • Corona SDK

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?



[TOPIC: post.html]
#2

douglascavanaugh

[GLOBAL: userInfoPane.html]
douglascavanaugh
  • Enthusiast

  • 32 posts
  • Corona SDK

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 );



[topic_controls]
[/topic_controls]