edit:
To prevent any additional confusion… I started writing this just shortly after Balthamel's first reply, but was required to step away from my computer half way through. So if you are going to read it, read it as if there were no other replies to the topic. Otherwise there might be confusion.
Allow me to correct a little confusion with the touchpoint api.
os.loadAPI("touchpoint")
local t = touchpoint.new("top")
t:add("left", nil, 2, 2, 16, 19, colors.red, colors.lime)
t:add("left", nil, 21,14, 37, 19, colors.red, colors.lime)
t:draw()
while true do
local event, left = t:handleEvents(os.pullEvent())
if event == "buttons_click" then
t:toggleButton("left")
rs.setOutput("right", true)
end
end
You the code above is the original code that you posted from your attempt. I will go through pointing out a few things that stand out to me.
t:add("left", nil, 2, 2, 16, 19, colors.red, colors.lime)
t:add("left", nil, 21,14, 37, 19, colors.red, colors.lime)
You define two buttons in 2 very different places, but you give them the same name. The first argument that you pass to the t:add function is the reference you use to access the button at any point, so having two buttons with the same name will for obvious reasons not work (how will it decide which of the two to activate?). My first suggestion would be to change the name of at least one of these two buttons to something different.
local event, left = t:handleEvents(os.pullEvent())
So what is going on here? Anytime a button is pressed the "event" variable is being set to the "button_click" event, and the "left" variable is being set to the name of the button that was pressed (Another reason why having differently named buttons is important).
t:toggleButton("left")
This stands out due to the "" around left. left is a variable, so putting "" around it causes it to treat it as a string rather than a variable.
With the slight confusion cleared up about the usage of the API, I am going to point out if you are going to use the api and its added events, then you will not need to use p1, p2, p3, p4, p5 to catch all of the events. If you are not going to worry about the other events that are thrown, then catching everything really does not matter. So personally I would go with something along the lines of:
local event, button = t:hhandleEvents(os.pullEvent())
edit:
As for telling the computer which button does what is simply a series of if statements checking which button was pressed.
os.loadAPI("touchpoint")
local t = touchpoint.new("top")
--Notice each of them have a different name
t:add("button1", nil, 2, 2, 10, 3, colors.red, colors.lime)
t:add("button2", nil, 2,4, 10, 5, colors.red, colors.lime)
t:add("button3", nil, 2, 6, 10, 7, colors.red, colors.lime)
t:add("button4", nil, 2,8, 10, 9, colors.red, colors.lime)
t:draw()
while true do
local event, button = t:handleEvents(os.pullEvent()) --# Grabs the next event that is fired, example being mouse_click, or key_press
if event == "button_click" then --checking to see if the event was a button_click event opposed to a different type of event.
if button == "button1" then --# If the button pressed is equal to "button1"
print("button1 was pressed")
elseif button == "button2" then --# if the button pressed is equal to "button2"
print("button2 was pressed")
elseif button == "button3" then --# if the button pressed is equal to "button3"
print("button3 was pressed")
elseif button == "button4" then --# if the button pressed is equal to "button4"
print("button4 was pressed")
end
end
end
Keep in mind I have never used this api, so the example above might have overlapping buttons if i did not interpret the arguments correctly. But the example is a valid way of checking against button labels.