# How do I draw a line when the mouse is pressed, and then have that line change type when the mouse and a certain key are pressed at the same time?

**URL:** <https://discourse.processing.org/t/how-do-i-draw-a-line-when-the-mouse-is-pressed-and-then-have-that-line-change-type-when-the-mouse-and-a-certain-key-are-pressed-at-the-same-time/21956>\
**Category:** Coding Questions\
**Created:** [June 17, 2020, 4:28pm UTC](https://discourse.processing.org/t/how-do-i-draw-a-line-when-the-mouse-is-pressed-and-then-have-that-line-change-type-when-the-mouse-and-a-certain-key-are-pressed-at-the-same-time/21956 "2020-06-17T16:28:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![keith](https://avatars.discourse-cdn.com/v4/letter/k/f05b48/32.png) [@keith](https://discourse.processing.org/u/keith)\
**Post date:** [June 17, 2020, 4:28pm UTC](https://discourse.processing.org/t/how-do-i-draw-a-line-when-the-mouse-is-pressed-and-then-have-that-line-change-type-when-the-mouse-and-a-certain-key-are-pressed-at-the-same-time/21956/1 "2020-06-17T16:28:49Z")

</div>

For example, I would like to have a “normal” line drawn when only the mouse is pressed, but have that line change color and/or size when the mouse and a certain key are pressed at the same time.

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 17, 2020, 4:54pm UTC](https://discourse.processing.org/t/how-do-i-draw-a-line-when-the-mouse-is-pressed-and-then-have-that-line-change-type-when-the-mouse-and-a-certain-key-are-pressed-at-the-same-time/21956/2 "2020-06-17T16:54:38Z")

</div>

Here I just change the color every time you press the space key. You can adapt the class to keep track of width or some other factor of your line.

Kf

```java
//===========================================================================
// GLOBAL VARIABLES:
ArrayList<ColorPoint> points;
color currColor=150; //Some sort of grey

//===========================================================================
// PROCESSING DEFAULT FUNCTIONS:

void settings() {
  size(400, 600);
}

void setup() {

  textAlign(CENTER, CENTER);
  rectMode(CENTER);

  fill(255);
  strokeWeight(2);

  points=new ArrayList<ColorPoint>();
}

void draw() {
  background(0);
  
  fill(250);
  text("Mouse pressed and drag to draw points, space to change color", width/2,10);
  
  for(int i=0;i<points.size()-1;i++){
    ColorPoint p1 = points.get(i);
    ColorPoint p2 = points.get(i+1);
    
    //Draw lines from this point to point+1
    stroke(p1.colorPoint);
    line(p1.point.x,p1.point.y,p2.point.x,p2.point.y);
  }
}

void keyReleased() {
  
  if(key==' '){
    int newRed=int(random(256));
    int newGreen=int(random(256));
    int newBlue=int(random(256));
    currColor=color(newRed, newGreen, newBlue);
  }
}

void mouseDragged() {
  points.add(new ColorPoint(mouseX, mouseY, currColor));
}

//===========================================================================
// OTHER FUNCTIONS:
class ColorPoint {
  color colorPoint;  
  PVector point;
  ColorPoint(float x, float y, color c){
    colorPoint=c;
    point=new PVector(x,y);
  }
}

```
