keyPressed and keyReleased with java(eclipse)

i ref to this first code and

  • -a- made it for me readable and runable in processing IDE
  • -b- made a working registered class from it but failed about the key register
    so need to still call them from main
  • -c- find from G4P a other way to register the key event

-A-

Player player;

void settings() {
  size(800, 800);
  player = new Player(400, 400, 3, 50);
}

void draw() {
  background(255);
  player.draw();
}

void keyPressed() {
  player.keyPressed();
}

void keyReleased() {
  player.keyReleased();
}

//__________________________________________________________CLASS PLAYER
class Player {
  int x, y, spd, size;
  boolean[] keys=new boolean[4];

  Player(int x, int y, int spd, int size) {
    this.x=x;
    this.y=y;
    this.spd=spd;
    this.size=size;
    println("use key [q] UP [s] LEFT [d] RIGHT [z] DOWN");
  }

  void draw() {
    if (keys[0]) y+=spd;       //q   UP
    if (keys[1]) y-=spd;       //z   DOWN
    if (keys[2]) x-=spd;       //s   LEFT
    if (keys[3]) x+=spd;       //d   RIGHT
    rect(x, y, size, size);
  }

  void keyPressed() {
    if (key=='z') keys[0]=true;
    if (key=='q') keys[1]=true;
    if (key=='s') keys[2]=true;
    if (key=='d') keys[3]=true;
  }

  void keyReleased() {
    if (key=='z') keys[0]=false;
    if (key=='q') keys[1]=false;
    if (key=='s') keys[2]=false;
    if (key=='d') keys[3]=false;
  }
}

-B-

Player player;

void settings() {
  size(800, 800);
  player = new Player(this, 400, 400, 3, 50);
}

void draw() {
  background(255);
}

void keyPressed() {
  player.keyPressed();
}

void keyReleased() {
  player.keyReleased();
}

//__________________________________________________________CLASS PLAYER
public class Player {
  PApplet pa;
  int x, y, spd, size;
  boolean[] keys=new boolean[4];

  Player(PApplet pa, int x, int y, int spd, int size) {
    this.pa = pa;
    this.x=x;
    this.y=y;
    this.spd=spd;
    this.size=size;

    pa.registerMethod("draw", this);
    println("use key [q] UP [s] LEFT [d] RIGHT [z] DOWN");
  }

  void draw() {
    if (keys[0]) y+=spd;       //q   UP
    if (keys[1]) y-=spd;       //z   DOWN
    if (keys[2]) x-=spd;       //s   LEFT
    if (keys[3]) x+=spd;       //d   RIGHT
    pa.rect(x, y, size, size);
  }
  // registering and NOT calling did not work.
  void keyPressed() {
    if (key=='z') keys[0]=true;
    if (key=='q') keys[1]=true;
    if (key=='s') keys[2]=true;
    if (key=='d') keys[3]=true;
  }

  void keyReleased() {
    if (key=='z') keys[0]=false;
    if (key=='q') keys[1]=false;
    if (key=='s') keys[2]=false;
    if (key=='d') keys[3]=false;
  }
}


-C-

// try to also register the key thing 
// by copy from:
// libraries\G4P\src\g4p_controls\GTextField.java
// Copyright (c) 2012 Peter Lager

import processing.event.KeyEvent;

Player player;

void settings() {
  size(800, 800);
  player = new Player(this, 400, 400, 3, 50);
}

void draw() {
  background(255);
}

//__________________________________________________________CLASS PLAYER
public class Player {
  PApplet pa;
  int x, y, spd, size;
  boolean[] keys=new boolean[4];

  Player(PApplet pa, int x, int y, int spd, int size) {
    this.pa = pa;
    this.x=x;
    this.y=y;
    this.spd=spd;
    this.size=size;

    pa.registerMethod("draw", this);
    pa.registerMethod("keyEvent", this);
    println("use key [q] UP [s] LEFT [d] RIGHT [z] DOWN");
  }

  void draw() {
    if (keys[0]) y+=spd;       //q   UP
    if (keys[1]) y-=spd;       //z   DOWN
    if (keys[2]) x-=spd;       //s   LEFT
    if (keys[3]) x+=spd;       //d   RIGHT
    pa.rect(x, y, size, size);
  }


  public void keyEvent(KeyEvent e) {   //@quark
    int keyCode = e.getKeyCode();
    char keyChar = e.getKey();
    boolean shiftDown = e.isShiftDown();
    boolean ctrlDown = e.isControlDown();
    int keyID = e.getAction();
    if (keyID == KeyEvent.PRESS) keyPressedProcess(keyCode, keyChar, shiftDown, ctrlDown);
    if (keyID == KeyEvent.RELEASE) keyReleasedProcess(keyCode, keyChar, shiftDown, ctrlDown);
  }

  protected void keyPressedProcess(int keyCode, char keyChar, boolean shiftDown, boolean ctrlDown) {
    //println(keyChar);
    if (keyChar=='z') keys[0]=true;
    if (keyChar=='q') keys[1]=true;
    if (keyChar=='s') keys[2]=true;
    if (keyChar=='d') keys[3]=true;
  }
  protected void keyReleasedProcess(int keyCode, char keyChar, boolean shiftDown, boolean ctrlDown) {
    //println(keyChar);
    if (keyChar=='z') keys[0]=false;
    if (keyChar=='q') keys[1]=false;
    if (keyChar=='s') keys[2]=false;
    if (keyChar=='d') keys[3]=false;
  }
}

hope the 3 examples might help you with your problem ( also code styling ).

3 Likes