# mousePressed with multiple windows

**URL:** <https://discourse.processing.org/t/mousepressed-with-multiple-windows/31947>\
**Category:** Processing.py\
**Created:** [August 26, 2021, 11:56am UTC](https://discourse.processing.org/t/mousepressed-with-multiple-windows/31947 "2021-08-26T11:56:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stef0206](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@stef0206](https://discourse.processing.org/u/stef0206)\
**Post date:** [August 26, 2021, 11:56am UTC](https://discourse.processing.org/t/mousepressed-with-multiple-windows/31947/1 "2021-08-26T11:56:47Z")

</div>

I am working with processingpy and wanted to make a drawing program, I’ve created a basic color picker in one window, and then created a seperate one for drawing, but when I attempt to use mousePressed in the new window it only triggers when I click within the bounds of the color picker.

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [August 28, 2021, 3:20am UTC](https://discourse.processing.org/t/mousepressed-with-multiple-windows/31947/2 "2021-08-28T03:20:34Z")

</div>

Welcome @stef0206

Because you’re using two windows, I assume you’ve structured your sketch using a class that extends `PApplet` (for the second window)? If so, this code might help, which includes a `mousePressed()` for both windows –

```python
def setup():
    size(200, 200)
    window2 = Window2()
    switches = '--sketch-path=' + sketchPath(), ''
    PApplet.runSketch(switches, window2)
 
last_clicked_window = '' # holds a 'red' or 'blue' string

# red window

def draw():
    background(255, 0, 0)
    text('last window you clicked:', 20, 20)
    text(last_clicked_window, 20, 40)

def mousePressed():
    global last_clicked_window
    last_clicked_window = 'red'

# blue window

class Window2(PApplet):
    def settings(w2):
        w2.size(200, 200)

    def draw(w2):
        w2.background(0, 0, 255)
        w2.text('last window you clicked:', 20, 20)
        w2.text(last_clicked_window, 20, 40)
    
    def mousePressed(self, w2):
        global last_clicked_window
        last_clicked_window = 'blue'

```

---

<div class="post-metadata">

**Author:** ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)\
**Post date:** [August 30, 2021, 4:12pm UTC](https://discourse.processing.org/t/mousepressed-with-multiple-windows/31947/3 "2021-08-30T16:12:29Z")

</div>

Thank you so much for posting this @tabreturn! I struggle with a second window from time to time and this helps enormously, I didn’t know how to handle the clicks!

I have fiddled with your example adding some ideas I learned from @GoToLoop I suppose:

```python
last_clicked_window = '' # holds a 'red' or 'blue' string

def setup():
    global window2
    size(200, 200)
    window2 = Window2('blue') # now also runs & sets the title
    this.surface.setTitle('red') 
    print(dir(window2))
 
# red window

def draw():
    background(255, 0, 0)
    text('last window you clicked:', 20, 20)
    text(last_clicked_window, 20, 40)

def mousePressed():
    global last_clicked_window
    last_clicked_window = 'red'
    print(mouseX, mouseY)

# my window2 will sometimes stick around without this:    
def stop(): # undocumented hack, use exit() in Java mode
    window2.exit()
        
# blue window

class Window2(PApplet):
    def __init__ (self, title=''):
        switches = '--sketch-path=' + sketchPath(), ''
        PApplet.runSketch(switches, self)
        self.surface.setTitle(title)
    
    def settings(self):
        self.size(200, 200)

    def draw(self):
        self.background(0, 0, 255)
        self.text('last window you clicked:', 20, 20)
        self.text(last_clicked_window, 20, 40)
      
    def mousePressed(self, mouse_event):
        global last_clicked_window
        last_clicked_window = 'blue'    
        print(self.mouseX, self.mouseY)

```
