How do you use mouse wheel to change a variable

edited August 2015 in How To...

I want to create a sizeable paint brush in a paint program I am making, and I want the mousewheel to change the variable I have for the size of the brush I use. How do I go about doing this? :) I am new to programming


  • Your first stop should be the reference:

    That contains an example of how you can get the mouse scroll value. Why don't you try something out with that and post some code if you get stuck on something specific?

  • I used the code and saw that it worked, I am just confused of which value it uses so I can for example make a rect(10,10,10,10); into rect(10,10,20,20); you know, scaling it along with my scrolling.

  • It sounds like you would want to keep track of a variable that holds your rectangle width and height, and then adjust that variable when you detect a mouse wheel event.

  • I have the variable for the rect, but I do not know how to detect the mouse wheel event. could you give an example? that would be helpful :)

  • sigh, I will go for some keypresses instead of mousewheel then.

  • Why? What part of this is confusing you? What code have you tried?

  • //size changer if(keyPressed && key == 'r'){ toolsize = toolsize +1; } else { if(keyPressed && key == 'f'){ toolsize = toolsize -1; } } if(toolsize < 1){ toolsize = 1; } }

    This is what I have, toolsize determines the size of the rect I use for painting. if I can instead switch the keys "r" and "f" for up and down on the mousewheel that would be great.

  • crap, I don't know how to paste code correctly

  • You format code by using the code button in the editor. Just highlight your code and push the code button.

    The reference contains an example for getting the value of the scroll wheel. What have you tried with that? A simple start would be just printing out the value of the scroll wheel event.

  • Idunno, I must be too stupid to figure this out xP

  • Break your problem down into smaller pieces. Can you create a separate sketch that simply prints out the value of the scroll event?

  • edited August 2015


    I got most figured out now. Only problem is that it won't go higher than 1 or lover than -1. it would be nice to have a range of 1 to 250

  • edited August 2015

    Declare a "global" variable to accumulate getCount(): int wheelAccumulator = 1;
    Then inside mouseWheel() use wheelAccumulator = constrain(wheelAccumulator + event.getCount(), 1, 250);

  • The count is only for a single event, so it makes sense that it would only be between -1 and 1.

    What if you added a variable that kept track of the total count of the mouse wheel?

  • edited August 2015
    //scroll wheel
    void mouseWheel(wheelAccumulator = constrain(wheelAccumulator + event.getCount(), 1, 250) {
      float e = event.getCount();

    I must have done something wrong, it keeps coming with errors that I can't understand I do have the global variable.

  • Can you post all of your code? What is the exact text of the error?

    Note that this syntax is wrong;

    void mouseWheel(wheelAccumulator = constrain(wheelAccumulator + event.getCount(), 1, 250) {

    You seem to be putting a statement inside the () parenthesis, which is where the function's parameters would go. You can't put statements in there. Maybe you meant something like this:

    void mouseWheel(MouseEvent event){
       wheelAccumulator = constrain(wheelAccumulator + event.getCount(), 1, 250);
       ///rest of your function goes here...
  • holy crap it finally works, thanks :)

Sign In or Register to comment.