When I use the onPress or onRelease functions on my buttons it triggers as soon as it gets created, before I even press it.
local widget = require("widget") local function listener() print( "pressed" ) end local button = widget.newButton{ onPress = listener() }
Is this supposed to happen or is it a bug? Is there a way to prevent it from triggering before getting pressed?
I noticed it only happens when there is a parenthesis following the function at “onPress” but I have a function that requires parenthesis, so does anyone how to get around this?
Yes
onPress = listener() is actually executing the listener
Omit the () to correctly assign the function reference
onPress = listener
If you have to send parameters to the function, use: OnPress = function() listener (params) end
That means this:
local button = widget.newButton { onPress = listener() } -- This calls listener
needs to be this:
local button = widget.newButton { onPress = listener } -- This assigns reference to listener to onPress
I noticed it only happens when there is a parenthesis following the function at “onPress” but I have a function that requires parenthesis, so does anyone how to get around this?
Yes
onPress = listener() is actually executing the listener
Omit the () to correctly assign the function reference
onPress = listener
If you have to send parameters to the function, use: OnPress = function() listener (params) end
That means this:
local button = widget.newButton { onPress = listener() } -- This calls listener
needs to be this:
local button = widget.newButton { onPress = listener } -- This assigns reference to listener to onPress