This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
skarlitz's profile picture

Listening for monitor events with a custom API and throwing events in response

Started by skarlitz, 05 February 2014 - 07:09 PM
skarlitz #1
Posted 05 February 2014 - 08:09 PM
Okay so I've got a touchscreen region API in the works at the moment, and one of the things I need my custom API to do is listen for monitor_touch events. Of course, doing that would require use of coroutines or the parallel API (to my knowledge). Could anyone give me an example of how to work this? I know what I need to use, just not how to use it…

So basically…

A user would set a region using a function within the API. The API should listen for monitor_touch events and throw its own event ("region_touch") whenever it finds that the touch was within the defined region.

So how could I have the API listen for touch events while not blocking the rest of the program?
OReezy #2
Posted 05 February 2014 - 08:20 PM
Whenever someone right clicks an attached monitor, this will tell you the event type, which side of the computer the monitor is on, and the x and y coords. To check for a certain area you just see if x and y are between two values.

 local event, side, x, y = os,pullEvent("monitor_touch")

EDIT: Sorry, somehow i missed the last question. The simplest way to run a code like this without halting your program is to run this part and the other part with the parallel API.

parallel.waitForAny(function1(), function2() )
Edited on 05 February 2014 - 07:23 PM
skarlitz #3
Posted 05 February 2014 - 08:25 PM
I know that. But my point is, when you do os.pullEvent, it blocks the rest of the program, meaning it pauses execution of the program until os.pullEvent has a return value. And since this is an API and works mainly through events, I'd like to have the API not force the program using it to halt execution because the API is trying to look for a monitor_touch event to be thrown.
Lyqyd #4
Posted 05 February 2014 - 08:42 PM
You might find it interesting to look at the handleEvents function in my Touchpoint API. For reference, it is:


handleEvents = function(self, ...)
    local event = {...}
    if #event == 0 then event = {os.pullEvent()} end
    if event[1] == "monitor_touch" and event[2] == self.side then
        local clicked = self.clickMap[event[3]][event[4]]
        if clicked and self.buttonList[clicked] then
            return "button_click", clicked
        end
    end
    return unpack(event)
end

The usage of it is to wrap any os.pullEvent calls with it. The documentation will explain further if you're interested in that sort of solution.
skarlitz #5
Posted 05 February 2014 - 09:08 PM
Out of curiosity, why do you use a colon in t:rename("label", "newLabel") ?
Lyqyd #6
Posted 05 February 2014 - 09:27 PM
The Touchpoint API is sort of object-oriented. You create a new instance (the examples use "t" for the instance variable), which can be thought of as a single "page" of buttons. That variable is a table that holds all of the information, so the functions need that data to work. The colon notation means that the "t" table gets passed to the function as the first argument. These two lines mean the same thing, but one uses colon notation and the other doesn't:


t:rename("label", "newLabel")

t.rename(t, "label", "newLabel")