# How to stop moving the slider until the key is released

**URL:** <https://discourse.processing.org/t/how-to-stop-moving-the-slider-until-the-key-is-released/30775>\
**Category:** Coding Questions\
**Created:** [June 18, 2021, 5:23am UTC](https://discourse.processing.org/t/how-to-stop-moving-the-slider-until-the-key-is-released/30775 "2021-06-18T05:23:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![noobcoderxx](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@noobcoderxx](https://discourse.processing.org/u/noobcoderxx)\
**Post date:** [June 18, 2021, 5:23am UTC](https://discourse.processing.org/t/how-to-stop-moving-the-slider-until-the-key-is-released/30775/1 "2021-06-18T05:23:16Z")

</div>

```auto
class slider {
int y = 0;

  void draw() {
    keyPressed();
    keyReleased();
    //not going out of frame
    if(y < 0) y += 25;
    if(y > height-25) y -= 25;
    
    rect(width-30, y, 10, rectSize);
  }
  
  void keyPressed() {
      if (key == CODED) {
        if (keyCode == UP) {
          y = y - 10;
        } else if (keyCode == DOWN) {
          y = y + 10;
          } 
      } 
    }
  //void keyReleased() {
  // if (key == CODED) {
  // if (keyCode == UP) {
  // y = y;
  // } else if (keyCode == DOWN) {
  // y = y;
  // } 
  // } 
  // }
}

```

the slider is never stops in the direction.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [June 18, 2021, 5:44am UTC](https://discourse.processing.org/t/how-to-stop-moving-the-slider-until-the-key-is-released/30775/2 "2021-06-18T05:44:30Z")

</div>

you need to take keypressed() out of draw.  
keyPressed is already bound to an event handler so does not need to be called in draw. It already runs in the background.

also keyPressed is a system function. So keep the code outside of the class otherwise you will have to add extra logic to release the keypress.

---

<div class="post-metadata">

**Author:** ![noobcoderxx](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@noobcoderxx](https://discourse.processing.org/u/noobcoderxx)\
**Post date:** [June 18, 2021, 5:58am UTC](https://discourse.processing.org/t/how-to-stop-moving-the-slider-until-the-key-is-released/30775/3 "2021-06-18T05:58:51Z")

</div>

I need that logic I think…but I can’t figure out…

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [June 18, 2021, 6:06am UTC](https://discourse.processing.org/t/how-to-stop-moving-the-slider-until-the-key-is-released/30775/4 "2021-06-18T06:06:10Z")

</div>

Then you either need another bit of code which makes use of the keyEvent listener, this will allow you to handle the logic without a crazy boolean scenario.

Alternatively and simpler yet, just use the keypressed method in the main sketch and pass the slider instance into keypressed.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [June 18, 2021, 7:58am UTC](https://discourse.processing.org/t/how-to-stop-moving-the-slider-until-the-key-is-released/30775/5 "2021-06-18T07:58:58Z")

</div>

```auto
void setup(){
  
};

// this loops unless noLoop() is called
void draw(){
  
}

//this does not loop but it triggered when key is pressed
void keyPressed(){
  //instead of placing keypressed in your class, add required code here
}

//this does not loop but is triggered when mouse is pressed
void mousePressed(){
  
}

class someClass{
  
  // if this is in draw it will keep looping
  void draw(){
    //keypressed will also keep looping
    keyPressed();
  };
  
  
  void keyPressed(){
    
  }
};

```

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [June 18, 2021, 8:02am UTC](https://discourse.processing.org/t/how-to-stop-moving-the-slider-until-the-key-is-released/30775/6 "2021-06-18T08:02:42Z")

</div>

alternatively you can can create a class which handles keyEvents

```auto
package windowsGui;

import processing.core.PApplet;
import processing.event.KeyEvent;

public class KeyboardFunctions {
	PApplet p;

	public KeyboardFunctions() {

	};

	public void init(PApplet p) {
		this.p = p;
		p.registerMethod("keyEvent", this);  
	};
	

	public void destroy(){
		//destroys the registermethod or something like that 
	};

	public void keyEvent(final KeyEvent evt) {
		switch(evt.getAction()) {
		case KeyEvent.PRESS:
			keyPressed();
// p.println(p.keyCode);
			//getKeyCode();
			break;
		case KeyEvent.RELEASE:
// keyReleased();
			//getKeyCode();
			break;
		}
	};

	public void keyPressed() {

	};

	public void keyReleased() {
	};
};

```

Now your slider class can extend keyBoardfunctions and make use of keyEvent logic, meaning your code shouldnt loop anymore.
