Hi,
I’m trying to read what will eventually be touch input on a mobile device screen. So I’m testing on a Windows 10 PC with the following code:
function on_input(self, action_id, action)
if action_id == hash(“mb1”) then
msg.post(“mygui”,“xy”,action)
else
msg.post(“mygui”,“touchup”)
end
end
MB1 is the action for a mouse_trigger 0 for mouse button 1.
Then in my GUI script, I handle the message xy to set a text node to the XY coordinates and another one to say touch down or I set it to touch up, when the mouse button isn’t being pressed.
The thing is, when I run this on Windows, when the mouse button is pressed, I get what I expect. But if I hold the button down and move the mouse, the input reacts as the mouse button is being pressed and released.
When I test the app on an Android device, it works as I expected it to.
You must look at action.pressed and action.released to detect when the mouse button is pressed and released. There’s also action.repeated that gets triggered with some interval.
Thanks, I can see that action.released gets set to true when the mouse button is released. However, action.pressed released & repeated are mostly false. I do see a released set to true when I release.
Should action.pressed be true?
action.pressed will be generated once. If you need to keep track of the state of the mouse button you typically do this in a variable on the script. Example:
function on_input(self, action_id, action)
if action_id == hash("mb1") then
if action.pressed then
self.mb1_pressed = true
elseif action.released then
self.mb1_pressed = false
end
end
if self.mb1_pressed then
print("mb1 is pressed")
end
end