Custom event listener like mousPressed or keyPressed

I have been thinking to write my own custom event listener like mousePressed or keyPressed in java. I have seen similar events in androids too where when an acitivity loads it goes through a cycle such as onCreate, onCreateView, onStart, onPause, onResume, onStop etc. these are the methods are independent of main and act when these events fire.

In processing, mousePressed, keyPressed etc. are independent of main method [draw & setup] and fires only when any key is pressed or mouse is pressed or moved. My question is can I write something my own methods which will be independent of main function but it will trigger when event gets fired.

For example - I would like to write a serial port listener method or headphone attached method for processing so that programmer don't need to write them in setup and they just need to call once like mousePressed and it will do the work. Ofcouse these are just example and it can be done for something else for example if my Arraylist is exceeding over some size it would automatically start emptying itself without any method which can trigger to some other event such as add new objects in the arraylist. Please take these as example. Of couse I might not need custom event listener to do this but I would still like to know how and where to start.

I have been doing my research and stumble upon observer pattern in java but I couldn't get my head around it. I feel I need something simple which is understandable by beginners too.

Observer Pattern Example

Answers

  • edited November 2016

    In Processing, mousePressed(), keyPressed(), etc. are independent of main method [draw() & setup()] and fires only when any key is pressed or mouse is pressed or moved.

    Actually, those events aren't called at the moment they happen in Processing! @-)
    They're not asynchronous but synchronous. In the same way as setup(), draw() & settings()! :-$

    Indeed, Processing got a separate Thread which polls and enqueues those input events.
    But that Thread doesn't dequeue at all! Except under noLoop(). L-)

    Each time draw() finishes, still under the "Animation Thread", the sketch begins to synchronously dequeue all of those accumulated events until their container gets empty, calling back each designated event function in FiFo order. #:-S

    After all of that is done, Processing finally renders the current content of the PGraphics canvas into the PSurface window. :)>-

    Then the whole callback cycle repeats over if noLoop() isn't active. ~O)

  • edited November 2016

    Can I write something my own methods which will be independent of main function but it will trigger when event gets fired.

    Yes, you can! However, if those are simply mouse & keyboard events, it's highly advisable that you just registerMethod() them.

    If your wish your library to be synchronous w/ sketch's "Animation Thread", so it got no problems directly changing the canvas, use registerMethod() too.
    Some libraries which do just that are ControlP5 & G4P.

    For hardware access, some other non-canvas related stuff, or if you simply prefer your library to run under a separate Thread, you may try out the reflection callback method.

    Some libraries using that approach: video, sound, serial, network, etc.
    https://Processing.org/reference/libraries/
    Study their source code in order to learn how to do that.

    ... and stumbled upon observer pattern in Java. But I couldn't get my head around it.

    That approach is rarely used by Processing's 3rd-party libraries, b/c it demands that we create some class which implements the callback class. Then manually register it to the caller class. :(|)

Sign In or Register to comment.