registerMethod() of keyEvent not compiling in custom library...

I'm trying to add some useful keyboard functions to my XYscope library and hitting a strange problem within Eclipse to use a registerMethod() and keyEvent.

In the initialization of the library class, I'm calling the registerMethod() which works: p.registerMethod("keyEvent", this);

but I can only compile successfully if I create a public function of keyEvent without any parameters inside:

public void keyEvent(){ p.println("test"); }

however when trying to run a sketch using the library in Processing, I get this error: "There is no public keyEvent() method in the class xyscope.XYscope"

If I try to include the KeyEvent parameter like I've read about, the library won't compile:

public void keyEvent(KeyEvent evt){ p.println("test"); }

The error I get while trying to compile is such:

102: error: cannot find symbol [javac] public void keyEvent(KeyEvent evt){ [javac] ^ [javac] symbol: class KeyEvent [javac] location: class XYscope

Any ideas out there what I might be doing wrong?

Answers

  • The correct method signature is

    public void keyEvent(KeyEvent event)

    Difficult to be certain without seeing your code but the code

    p.registerMethod("keyEvent", this);

    suggests that p is an object of type PApplet or child class of PApplet and that this code is in the constructor for the class XYscope. If true then the handler must also a method of the class XYscope.

    It could be that Eclipse thinks that the KeyEvent parameter in your handler method is java.awt.event.KeyEvent rather than processing.event.KeyEvent

    Check your import statements.

  • edited August 2017

    Sorry, the p. refers indeed to the PApplet that calls the library. EDIT: In the github source linked below, it was still called called 'myParent', but after reading some tips today, realized I should have just given it a single char variable... now 'p'.

    I'm having no problem using registerMethod("dispose", this); – so it must be something about Eclipse using the java.awt.event.keyEvent being used instead of the Processing one... the code (without my attempt to add the keyEvent is on github here.

    I was trying to add this for example to line 91 for the register method.. and just below that function the keyEvent.... at the top I've done the core import as suggested in the library template:

    package xyscope; import processing.core.*;

  • Answer ✓

    You need to import it with

    import processing.event.*;

    which will make all Processing event classes available when you add the event handler to the class.

    realized I should have just given it a single char variable... now 'p'.

    Why change it to p? The benefits of using descriptive identifiers far exceed the small effort to do a bit more typing, especially since Eclipse provides auto-completion.

    In most computer applications the most expensive part of the software-life-cycle is maintenance so having well documented source code with clear semantics is sooooo... very important.

  • edited February 2018

    Why change it to p?

    • p (or pa) is very known to refer to PApplet within the Processing community. ;;)
    • Even in p5.js, p is the preferred choice when using the instance mode approach. :P
    • Other notable variable names: i, j, k, x, y, w, h, vx, vy, etc.
    • pg for PGraphics, img for PImage, mov for Movie. B-)
  • import processing.event.*; Ahaaa! Argh.. somehow that didn't turn up on any tutorials I was following for libs.. thought I only needed the core. Thanks!

    Side note.. in my code for accessing other functions constrain, map, println – i was placing the PApplet reference in front of it. Could this have been avoided if I imported the correct Processing lib for those things??

    Regarding 1 char vars.. didn't mean to open up that can in this thread.. but I agree with both of you. In the case of PApplet – using a standardized variable name (p or pa) is useful, especially considering how often one will type it in. When it comes to other variable names, it's a fine art of balancing descriptiveness and brevity.

    int iLikeToTellStudentsThatSoLongAsTheyUseCamelCaseTheyCanCallThingsWhateverTheyLike = 1;

    but foo is probably better = wtf is foo? -> end of class.

  • edited August 2017

    ... in my code for accessing other functions constrain(), map(), println() – I was placing the PApplet reference in front of it.

    • Those are static methods from class PApplet.
    • B/c they're stateless (no access to instance members) and the only thing they can access are arguments passed to them, they can be directly invoked w/ no need to a PApplet instance reference.
    • If you want, you can statically import the whole PApplet like this: *-:)
      import static processing.core.PApplet.*;
    • Doing so, we can access all static members w/o prefixing them: :ar!
      println("static call:", sqrt(THIRD_PI));
  • edited February 2018

    edit – guess this just worked for some of the math functions, not many of the other functions Processing has (fill, pushMatrix, translate, stroke, etc etc)...

  • I I can see those methods belong to the current canvas, which in your case will be the object assigned to p or pa. So those are associated to an object aka. they are not stateless and nop, they are not static:

    https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L13203

    https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L13146

    https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L13168

    Kf

Sign In or Register to comment.