Key and mouse pressed with boolean

Hi, i wonder if it’s possible to use a boolean with the code proposed by @tutantontut

import processing.event.MouseEvent;

boolean[] mouses = new boolean[2];
boolean pressLeft = true;
boolean pressRight = true;
boolean actionLM = false;
boolean actionRM = false;

int leftClick;
int rightClick;

void setup(){
  size(200, 100);
  registerMethod("mouseEvent", this);
}

void draw(){
  background(255);
  
  mouseLeft();
  if(actionLM) leftClick+=1;
  fill(0);
  text("left " + leftClick, 10, 10);
  
  mouseRight();
  fill(0);
  text("right " + rightClick, 10, 25);
}

void mouseLeft(){
  if(pressLeft && mouses[0] == true){
    actionLM = true;
    pressLeft = false;
  }
  if(mouses[0] == false){
    actionLM = false;
    pressLeft = true;
  }
}

void mouseRight(){
  if(pressRight && mouses[1] == true){
    rightClick +=1;
    pressRight = false;
  }
  if(mouses[1] == false){
    pressRight = true;
  }
}
  
public void mouseEvent(MouseEvent e){
  int button = e.getButton();
  int buttonID = e.getAction();
  if(buttonID == MouseEvent.PRESS){
    mousePressedProcess(button);
  }
  if(buttonID == MouseEvent.RELEASE){
    mouseReleasedProcess(button);
  }
}
  
protected void mousePressedProcess(int button){
  if(button == PConstants.LEFT) mouses[0]=true;
  if(button == PConstants.RIGHT) mouses[1]=true;
}
  
protected void mouseReleasedProcess(int button){
  if(button == PConstants.LEFT) mouses[0]=false;
  if(button == PConstants.RIGHT) mouses[1]=false;
}

So as you can see, the mouseRight() function is the solution proposed by @tutantontut
and the mouseLeft() is my (not working) proposal.

My questions are:

1: What is the difference between these two function ?
With my basic knowledge they are more or less the same.
In mouseRight() when the 2 booleans (pressRight and mouses[1]) are true, it increase rightClick by one, but only once. Why ?
In mouseLeft(), when the 2 booleans (pressLeft and mouses[0]) are true, the boolean actionLM become true and so it continues to increase leftClick until i release the left button. But why ? Is it because i call and check the boolean ?

2: Is it possible to set a boolean so it act like the “rightClick+=1” in mouseRight() ?

Sorry if i create a new post since i ask this question in another topic but i can’t find the answer.

Thanks for your time

-a- is that same like this?
Solution for repeating keyPressed while holding key ?

-b- there you posted under the question: repeated keypressed
your mousePressed code, understandable there was no answer.

-c- now you ask again?
KEY pressed with boolean?

but there is no key code i can see?

-d- a boolean can not act like a integer rightClick++;
also sorry, the use of this i do not understand

-e- can you give a detailed description why you start with this
difficult mouse even code (with registered eventcoding ) instead of just using
https://processing.org/reference/mousePressed_.html
possibly in combo with
https://processing.org/reference/mouseButton.html

possibly need just to redesign your program flow.

a: yes sorry about that
b: what do you mean by no answer ? it’s not possible ?
c: sorry again, if i must, i will delete this post

What do you mean ?

d: well i’m looking for a way to make a boolean act like rightClick++;

e: i use this code because i code in java with eclipse and it’s the only one working, https://discourse.processing.org/t/keypressed-and-keyreleased-with-java-eclipse/12994/16, you gave me this solution and it’s work great.

So my question is: Is there a way, with the code i post, to use a boolean and it act like this:
when i press a mouse button, the boolean is set to true and then it’s set to false before i release the mouse button.

  • so the word ‘key’ in the header line means ?mouse button?

  • -d- still not understand

by what logic? timer?
but yes, you can inside draw read that boolean ?

mouses[0]

on if true:

  • use it ( also even for do a int++; ) and
  • reset it

( faster as you can release the mouse button and reset it by this, but it makes that release logic obsolete?)

yes sorry about that, with the code i post, if you use a keyEvent(), the logic stay the same.

d: if you run the code i post, you see that when you press the left button of the mouse, the leftClick number increases until the key is released, when you press the right button of the mouse, the rightClick number increase only by one, even if you keep the right button pressed.
So my question is: Is it possible to use a boolean (actionLM in this example) as a condition and when you press the left mouse button, the leftClick number only increase by one ?

I don’t know, that’s why i’m asking

yes but if i do : if(mouses[0]) leftClick+=1; in draw(), my problem is the same.

sorry if i’m not clear, i’m a rookie in programing

try:

import processing.event.MouseEvent;

boolean[] mouses = new boolean[2];

int leftClick;
int rightClick;

void setup(){
  size(200, 100);
  registerMethod("mouseEvent", this);
  fill(0);
}

void draw(){
  background(255);
  text("left " + leftClick, 10, 10);
  text("right " + rightClick, 10, 25);
  check_click();
}

void check_click() {
  if ( mouses[0] ) {
    mouses[0] = false;
    leftClick++;
  }
  if ( mouses[1] ) {
    mouses[1] = false;
    rightClick++;
  }
}
  
public void mouseEvent(MouseEvent e){
  int button = e.getButton();
  int buttonID = e.getAction();
  if(buttonID == MouseEvent.PRESS)   mousePressedProcess(button);
  if(buttonID == MouseEvent.RELEASE) mouseReleasedProcess(button);
}
  
protected void mousePressedProcess(int button){
  if(button == PConstants.LEFT)      mouses[0]=true;
  if(button == PConstants.RIGHT)     mouses[1]=true;
}
  
protected void mouseReleasedProcess(int button){
  if(button == PConstants.LEFT)      mouses[0]=false;
  if(button == PConstants.RIGHT)     mouses[1]=false;
}

it’s close to my mouseRight(), that’s not what i’m asking.

I don’t want you to gave me a complet solution, i want to learn.

well yes, i try to follow your code

so, sorry no idea what you are up to,
so i can not even say if it is a mouse event coding question or just
? about using the logic info, you have already, correctly ?


void check_click() {
  if ( mouses[0] ) {
    if ( actionLM ) mouses[0] = false;
    leftClick++;
  }
  if ( mouses[1] ) {
    if (actionRM ) mouses[1] = false;
    rightClick++;
  }
}
 

My question is:

1: What is the difference between mouseRight() and mouseLeft() and why they act differently ?
With my basic knowledge they are more or less the same.
In mouseRight() when the 2 booleans (pressRight and mouses[1]) are true, it increase rightClick by one, but only once. Why ?
In mouseLeft(), when the 2 booleans (pressLeft and mouses[0]) are true, the boolean actionLM become true and so it continues to increase leftClick until i release the left button. But why ? Is it because i call and check the boolean in draw() ?

It’s because of the boolean actionLM. As long as you keep your left mouse button pressed, it remains true. Release it, and the second if statement inside void mouseLeft() will switch it back to false:

  if(mouses[0] == false){
    actionLM = false;
    pressLeft = true;
  }

As long as actionLM is true, the if statement inside void draw() will keep increasing the value of leftClick:

if(actionLM) leftClick+=1;

void mouseRight() doesn’t have such a boolean to keep track of its state. Instead it enters the first if statement when the boolean pressRight is true, which thereafter instantly gets switched to false again:

  if(pressRight && mouses[1] == true){
    rightClick +=1;
    pressRight = false;
  }

Does that answer your question?

2 Likes

Yes thank you, i will continue to search a solution for my problem.

Thanks for your time guys

i had a day to think about it and wanted to tell you that
i not understand what you want from a mouse:

  • it is just clicked
  • it is still pressed
  • it is not pressed any more

what else could there be?

now even it is again
“NOT WHAT I WANT”

i give you a ( most likely worthless )
DOUBLE CLICK BUTTON
( because there would need add logic memory and millis timer … )

ButtonPlusPlus.pde

/*
 yes , again about BUTTON
 but this time it is about in deep mouse click operation
 start out from a ready toggle button class
 but give it more abilities like:
 
 DOUBLE CLICK
 
 
 RESET by RIGHT MOUSE CLICK ( or presss the shown other small reset button ) 
 */

Button_pp myButton_pp; //________________________________ make a button of type class Button_pp ( test double click logic )
Button myButton; //______________________________________ make a button of type class Button ( just for ref )

void setup() {
  size(768, 500);
  strokeWeight(3);
  textSize(20);
  myButton_pp = new Button_pp(100, 100, 300, 200, false, "double click test", 0);//xi, yi, wi, hi, seli, texti, idi);
  myButton = new Button(600, 400, 100, 40, false, false, "reset", "--", 1);//xi, yi, wi, hi, seli, visible, text0,text1, idi);
}

void draw() {
  header();
  background(200, 200, 0);
  logo(width-120, 20, 50, 0.01);
  myButton_pp.draw();
  myButton.draw();
  test();
}


void test() { //______________________________________________________test environment connects 2 different buttons
  if ( myButton_pp.seld || myButton_pp.sel ) myButton.vis = true;
  if ( myButton.vis && myButton.sel ) {
    myButton_pp.dreset(); //_________________________________________ reset the double click button
    myButton.sel = false;
    myButton.vis = false;
  }
}

void mousePressed() {
  myButton.mymousePressed();
}





/*
 myTools  KLL
 use extra pde file here in root
 name: myTools.pde
 idea is to develop it here ( master ) and download / upload to other projects.
 content: 
 
 ___________________________________ classes declared:
 
 class Button_pp
 declare:
 Button_pp button(x, y, w, h, sel, text, id);
 
 methods:
 button.draw(); //_______________ from draw
 button.mymousePressed(); //_____ local
 boolean button.over(); //_______ local
  button.dreset(); //_____________ local remote reset

 ___________________________________
 
 class Button
 declare:
 Button button(x, y, w, h, sel, text0, text1, id);
 
 methods:
 button.draw(); //_______________ from main draw
 button.mymousePressed(); //_____ from main mousePressed
 boolean button.over(); //_______ local
 
 ___________________________________ functions to call:
 
 logo(x,y,radius,speed); //_________ just some show
 
 header(); //_______________________ a window title with optional FPS info
 
 */

boolean diag = true; //__________________________________ diagnostic prints global switch

//_______________________________________________________ (DOUBLE CLICK) CLASS BUTTON PLUS PLUS

class Button_pp {   //____________________________________ begin class
  int x, y, w, h, id;
  boolean act, sel, seld;
  String atext, ainfo;
  long click1, dclick1=0, dclicks=0, dclick2=0;
  long dclicklim=200, dclickslim=300;
  int dstage=0;

  Button_pp(int x, int y, int w, int h, boolean sel, String atext, int id) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.sel = sel;
    this.seld = sel;
    this.atext = atext;
    this.id = id;
  }

  void  draw() {
    this.mymousePressed();
    strokeWeight(3);
    if (this.seld)         fill(0, 200, 0);
    else if (this.sel)     fill(0, 200, 200);
    else if (this.act)     fill(200, 0, 200);
    else                   fill(0, 0, 200);
    if (this.over())  stroke(200, 0, 200);
    else              stroke(0, 200, 200);
    rect(this.x, this.y, this.w, this.h);
    noStroke();
    fill(200);
    textSize(30);
    textAlign(CENTER);
    text(this.atext, this.x + this.w/2, this.y + this.h / 2 + 10);
    ainfo = "/ "+dclick1+" \\ "+dclicks+" / "+dclick2+" \\ \n lim "+dclicklim+", "+dclickslim+", "+dclicklim;
    textSize(20);
    if (diag ) text(ainfo, this.x + this.w/2, this.y + this.h - 40);
  }

  boolean  over() {
    return (mouseX > this.x & mouseX < this.x + this.w & mouseY > this.y & mouseY < this.y + this.h);
  }

  void  mymousePressed() { //___________________________ called from this draw()
    if ( this.over()  && mousePressed && (mouseButton == LEFT) ) {
      if ( dstage == 0 ) {
        this.act = true;  //__ left keep pressed
        click1 = millis(); //_ remember start time
        dstage = 1;
      }
      if ( dstage == 2 ) dstage = 3;
    }

    if ( this.act && dstage == 1 )    dclick1 = millis() - click1; //____________________ check timer 1
    if ( this.act && dstage == 2 )    dclicks = millis() - click1-dclick1; //____________ check timer 2
    if ( this.act && dstage == 3 )    dclick2 = millis() - click1-dclick1-dclicks; //____ check timer 3

    if ( this.act && dstage == 2 && dclicks > dclickslim ) {
      this.sel = true;
      this.act=false;
      if (diag ) println("timeout to single click");
    }

    if ( this.over() && this.act && !mousePressed ) {
      if ( dstage == 1 )  dstage = 2;
      if ( dstage == 3 ) { //___________________________ end of a possible double click
        if (diag ) println(" see possible double click ", dclick1, dclicks, dclick2, " msec");
        this.act = false;
        if ( dclick1 < dclicklim && dclicks < dclickslim && dclick2 < dclicklim ) {
          this.seld = true;
          if (diag ) println("double click timing ok");
        } else { 
          this.sel = true; //______________________ treat it as single click?
          if (diag ) println("bad double click timing");
        }
      }
    }

    if ( this.over()  && mousePressed && (mouseButton == RIGHT) ) dreset(); //_______ RIGHT CLICK also RESET
  } //__ end mymousePressed

  void dreset() {
    if (diag ) println("reset");
    dstage   = 0; //________ reset
    dclick1  = 0;
    dclicks  = 0;
    dclick2  = 0;
    this.sel = false;
    this.seld = false;
    this.act = false; //__ end
  }
} //___________________________________________________ end class


//________________________________________________________ FUNCTION HEADER
void header() {
  String tit = "";
  if ( diag ) tit ="<|-- Button Plus Plus --|> "+nf(frameRate, 0, 1)+" fps";
  else               tit ="<|-- Button Plus Plus --|>";
  surface.setTitle(tit);
}

//________________________________________________________ FUNCTION LOGO
float ang = 0;

void logo(int x, int y, int r, float dang) {
  float d1 = 2 * r;
  float d2 = 2 * r / sqrt(2);
  ang += dang; //__________________ animation
  push();
  fill(255); //____________________ fill same all 3 shapes
  strokeWeight(4);
  stroke(200, 200, 0); //__________ here same as main background gives a nice effect
  ellipseMode(CENTER);
  rectMode(CENTER);
  translate(x + r, y + r); //______ CENTER thinking
  push();
  rotate(-ang); //__________________ animate first bigger rect
  rect(0, 0, d1, d1);
  pop();
  ellipse(0, 0, d1, d1);
  rotate(ang); //_________________ animate second smaller rect  
  rect(0, 0, d2, d2);
  //textAlign(CENTER, CENTER);
  //textSize(20);
  //fill(200, 200, 0);
  //text("K L L", 0, 0);
  pop();
}

//_______________________________________________________ (OLD) CLASS BUTTON

class Button {   //____________________________________ begin class
  int x, y, w, h, id;
  boolean sel, vis;
  String atext, btext;

  Button(int x, int y, int w, int h, boolean sel, boolean vis, String atext, String btext, int id) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.sel = sel;
    this.vis = vis;
    this.atext = atext;
    this.btext = btext;
    this.id = id;
  }

  void  draw() {
    if ( this.vis ) {
      strokeWeight(3);
      if (this.sel)         fill(0, 200, 0);
      else                  fill(0, 0, 200);
      if (this.over())      stroke(200, 0, 200);
      else                  stroke(0, 200, 200);
      rect(this.x, this.y, this.w, this.h);
      noStroke();
      fill(200);
      textSize(30);
      textAlign(CENTER);
      String stext = "";
      if ( this.sel )       stext = this.btext; 
      else                  stext = this.atext;
      text(stext, this.x + this.w/2, this.y + this.h / 2 + 8);
    }
  }

  boolean  over() {
    return (mouseX > this.x & mouseX < this.x + this.w & mouseY > this.y & mouseY < this.y + this.h);
  }

  void  mymousePressed() {
    if ( this.over() )   this.sel =!this.sel; //_______ called from main mousePressed
  }
} //___________________________________________________ end class


https://en.wikipedia.org/wiki/Double-click see who invented this:

2 Likes

Were you able to resolve your problem? If not, would you be willing to restate what your problem is? For example “I am trying to count each time that the mouse [does something / is doing something]”.

I’ve looked at the original solution post that you reference – Solution for repeating keyPressed while holding key – and I’m still unclear on what you are trying to do. It seems as if you could simply duplicate the mouseRight code for both buttons and your problem would be solved?

Another option is to just put all your code in mousePressed() and use mouseButton.

If you are counting clicks and don’t care about the attack vs. release, it is also even easier to just put the code in mouseReleased and check mouseButton. Then no boolean guard fields are necessary – you can do the whole thing in 5 lines of code.

1 Like

The difference is that we set actionLM = true; in mouseLeft()

And in draw() we have

if (actionLM) //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   leftClick+=1;

Thus it’s increasing leftClick fast.

In mouseRight() we just have rightClick +=1;


Next question

new version:


import processing.event.MouseEvent;

boolean[] mouses = new boolean[2];
boolean pressLeft = true;
boolean pressRight = true;

//boolean actionLM = false;
//boolean actionRM = false;

int leftClick;
int rightClick;

void setup() {
  size(200, 100);
  registerMethod("mouseEvent", this);
}

void draw() {
  background(255);

  mouseLeft();

  // **********************************************************************
  //if (actionLM) //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  //   leftClick+=1;
  // **********************************************************************

  fill(0);
  text("left " + leftClick, 10, 10);

  mouseRight();
  fill(0);
  text("right " + rightClick, 10, 25);
}

//------------------------------------------------------------------------

void mouseLeft() {
  if (pressLeft && mouses[0] == true) {
    // actionLM = true; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    leftClick+=1;
    pressLeft = false;
  }
  if (mouses[0] == false) {
    //actionLM = false;
    pressLeft = true;
  }
}

void mouseRight() {
  if (pressRight && mouses[1] == true) {
    rightClick +=1;
    pressRight = false;
  }
  if (mouses[1] == false) {
    pressRight = true;
  }
}

public void mouseEvent(MouseEvent e) {
  int button = e.getButton();
  int buttonID = e.getAction();
  if (buttonID == MouseEvent.PRESS) {
    mousePressedProcess(button);
  }
  if (buttonID == MouseEvent.RELEASE) {
    mouseReleasedProcess(button);
  }
}

protected void mousePressedProcess(int button) {
  if (button == PConstants.LEFT) mouses[0]=true;
  if (button == PConstants.RIGHT) mouses[1]=true;
}

protected void mouseReleasedProcess(int button) {
  if (button == PConstants.LEFT) mouses[0]=false;
  if (button == PConstants.RIGHT) mouses[1]=false;
}
//