Python for processing - keyreleased?

edited August 2017 in Python Mode

I am figuring out some stuff. I have created a small program that uses : keyPressed and keyReleased in a conditional inside the DRAW function. The problem is that PFPython doesn't recognize keyReleased by itself, and it wants it as a function. Why is that? For example, I can use either keyPressed() or keyPressed: -- but I cannot use keyReleased() and keyReleased:

Answers

  • ok, here is the code. The challenge I am having is that when the mouse is pressed, it starts to draw - works great. When the keyboard is pressed, it starts to draw, works great as well. The challenge is that if I hold down a key on the keyboard, it will NOT stop drawing. So I am trying to eliminate this issue.

    def setup():
        size(200, 200)
        background(255) # white
        strokeWeight(10)
    
    def draw():
      if mousePressed:
           stroke(155)
           point(mouseX, mouseY)
      elif keyPressed:
           stroke(0,0,255)
           point(mouseX, mouseY)
    
  • it will NOT stop drawing. So I am trying to eliminate this issue.

    My understanding is that different OS provide different behavior for keyPressed. I believe you can configure this behavior in the configuration menu of your input devices provided by your OS. Regardless of this behavior, the solution is to use keyReleased(). However, I am not sure how the Python mode works so I cannot comment on performance there. What I could do is provide some suggestions.

    Firs of all, mousePressed, as a variable available in many modes. however, mouseReleased does not exist. There is nothing stopping you to implement your own mouseReleased nevertheless. For example, in java I will do:

    final int IDLE=0;
    final int MDOWN=1;
    final int MUP=1;
    
    int mState=IDLE;
    
    void setup(){size(200,200);}
    
    void draw(){
    
      if (mousePressed==true){
        mState=MDOWN;
        //Mouse pressed
      }
      else if(mState=MDOWN){
                mState=MUP;
                //Mouse was released
             }
    
      if(mState==MUP){
        //Released event
        //Set to iddle to wait for next release event (only after a mousePressed)
        mState=IDLE;
      }
    
    }
    

    I hope this help,

    Kf

  • Thank you but I want KEY RELEASED. I think the problem is that when a key is pressed, KEY becomes that ASCII number. I need to convert that number back to NOTHING when the key is released. I think that is what I need.

  • I see what you are saying now. I am going to try the boolean, but unfortunately, it ,for some reason, it never makes it to the Keypressed part of the conditional - because I can't get the TEXT to print. Here is what I tried.

    def setup():
        size(200, 200)
        fill(0)
        background(255) # white
        strokeWeight(10)
    
    def draw():
      if mousePressed==True:
           stroke(155)
           point(mouseX, mouseY)
      elif mousePressed==False:
           stroke(0,155,67)
           point(mouseX, mouseY)
    
      elif keyPressed==True:
          text("keypress",10,50)
          stroke(0,0,255)
          point(mouseX, mouseY)
    
      elif keyPressed==False:
          text("key no",10,50)
          stroke(255,3,75)
          point(mouseX, mouseY)
    
  • Your example above doesn't refer to key at all. Try setting the key to zero.

    Kf

    def setup():
        size(200, 200)
        background(255) # white
        strokeWeight(10)
    
    def draw():
      if mousePressed:
           stroke(155)
           point(mouseX, mouseY)
    
      elif keyPressed:
           stroke(0,0,255)
           point(mouseX, mouseY)
           if(this.key==' '):
               background(0)
               this.key='0'
    
  • Thank you, but it doesn't work if you get rid of the background(0). I want it so that when you hold down a key, it draws. When you release the key (doesn't matter which key at this point), it stops drawing. What you sent me doesn't seem to do that.

  • I believe it is finding a way to set this.key to RELEASE. I cannot set it to ZERO or " " because it still considers the key to be pressed for some reason.

  • Answer ✓

    Well, I found a work around, using the this.key command you suggested. I am guessing there isn't really a keyRelease that I can use, so I figured a toggle would work better with two keys being "s" for ON and "q" for OFF when it comes to drawing. It seems to work

    def setup():
        size(200, 200)
        background(255) # white
        strokeWeight(10)
    
    def draw():
      if mousePressed:
           stroke(155)
           point(mouseX, mouseY)
    
    
      elif this.key=="s":
           print(this.key)
           stroke(0,0,255)
           point(mouseX, mouseY)
    
      elif this.key=="q":
           print("werW", 40, 50)
    
  • edited August 2017
    """
     Alien Invader (v2.0.2)
     by Cochraneowns (2013-Apr)
     mod GoToLoop (2017-Aug-04)
    
     https://Forum.Processing.org/two/discussion/23706/
     python-for-processing-keyreleased#Item_9
    
     https://Forum.processing.org/one/topic/
     what-websites-work-best-when-using-processing-js.html
    
     http://Studio.ProcessingTogether.com/sp/pad/export/ro.9Iaf6privOouM
    """
    
    SMOOTH, FPS = 3, 10
    
    def setup():
        size(0300, 0300)
        smooth(SMOOTH)
        frameRate(FPS)
    
        cursor(CROSS)
        imageMode(CORNER)
    
        global alien
        alien = respawnAlien()
    
    
    def draw():
        clear()
    
        if keyPressed and (key == 's' or key == ' '):
            set(width >> 2, height >> 2, alien)
    
    
    def mousePressed(): respawnAlien(alien)
    
    
    def createAlienLayer(w=100, h=100):
        pg = createGraphics(w, h)
        pg.beginDraw()
    
        pg.smooth(SMOOTH)
        pg.colorMode(HSB, 360, 0200, 1)
        pg.blendMode(REPLACE)
        pg.rectMode(CENTER)
        pg.noStroke()
    
        pg.endDraw()
        return pg
    
    
    def respawnAlien(pg=None):
        pg = pg or createAlienLayer()
    
        pg.beginDraw()
        pg.clear()
        pg.fill(random(360), random(0100, 0200), 1)
    
        #body:
        pg.rect(50, 30, 50, 10)
        pg.rect(20, 40, 10, 10)
        pg.rect(80, 40, 10, 10)
        pg.rect(10, 55, 10, 20)
        pg.rect(90, 55, 10, 20)
        pg.rect(20, 70, 10, 10)
        pg.rect(80, 70, 10, 10)
        pg.rect(50, 80, 50, 10)
        pg.rect(20, 90, 10, 10)
        pg.rect(80, 90, 10, 10)
    
        #crown:
        pg.rect(50, 23, 30, 5)
        pg.rect(33, 18, 5, 5)
        pg.rect(67, 18, 5, 5)
        pg.rect(29, 13, 5, 5)
        pg.rect(71, 13, 5, 5)
        pg.rect(50, 18, 11, 5)
        pg.rect(50, 13, 5, 5)
    
        #face:
        pg.rect(35, 50, 5, 5)
        pg.rect(65, 50, 5, 5)
        pg.rect(50, 65, 20, 5)
    
        pg.endDraw()
        return pg
    
Sign In or Register to comment.