# keyPressed and keyReleased with java(eclipse)

**URL:** https://discourse.processing.org/t/keypressed-and-keyreleased-with-java-eclipse/12994
**Category:** Coding Questions
**Created:** [July 26, 2019, 8:12am UTC](https://discourse.processing.org/t/keypressed-and-keyreleased-with-java-eclipse/12994 "2019-07-26T08:12:49Z")
**Posts on this page:** 1
**Showing post:** 5

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [July 27, 2019, 12:23pm UTC](https://discourse.processing.org/t/keypressed-and-keyreleased-with-java-eclipse/12994/5 "2019-07-27T12:23:27Z")

</div>

Oopsie! Just now I’ve noticed the end of your thread title better: “Java (Eclipse)”! 🥴

When using Eclipse and other similar IDEs, we need to use real Java syntax in files w/ “.java” extension. ☕

Processing’s IDE (PDE) uses “.pde” files. Although it recognizes “.java” files too. 😇

If you wanna, I can convert the `class` Player to actual Java syntax inside file “Player.java”. 🤓

But b/c I don’t have Eclipse, I won’t be able to test if it’s actually compilable outside PDE. 🙈

* * *

I’ve just converted `class` Player to proper Java syntax in its own “Player.java” file: ☕

## Player.java:

```auto
package sketches.classes;

import processing.core.PApplet;
import static processing.core.PApplet.constrain;
import static processing.core.PApplet.parseInt;
import static processing.core.PConstants.*;

public class Player {
  public static final int INK = 0xff008000, OUTLINE = 0;
  public static final float BOLD = 2f;

  protected final PApplet p;
  protected boolean isLeft, isRight, isUp, isDown;

  public int x, y;
  public final int d, v;

  public Player(PApplet pa, int xx, int yy, int dd, int vv) {
    p = pa;
    x = xx;
    y = yy;
    d = dd;
    v = vv;
  }

  public void display() {
    p.circle(x, y, d);
  }

  public void move() {
    final int r = d >> 1;
    x = constrain(x + v*(parseInt(isRight) - parseInt(isLeft)), r, p.width - r);
    y = constrain(y + v*(parseInt(isDown) - parseInt(isUp)), r, p.height - r);
  }

  public boolean setMove(final int k, final boolean b) {
    switch (k) {
    case 'W':
    case UP:
      return isUp = b;

    case 'S':
    case DOWN:
      return isDown = b;

    case 'A':
    case LEFT:
      return isLeft = b;

    case 'D':
    case RIGHT:
      return isRight = b;

    default:
      return b;
    }
  }
}

```

What’s still left is the main “Player\_Move.pde” file: 😐

## Player\_Move.pde:

```auto
/**
 * Player Move (v1.3.1)
 * by GoToLoop (2014/Dec/07)
 *
 * Discourse.Processing.org/t/
 * keypressed-and-keyreleased-with-java-eclipse/12994/5
 *
 * Forum.Processing.org/two/discussion/8511/movement-code-messed-up#Item_6
 * Studio.ProcessingTogether.com/sp/pad/export/ro.91tcpPtI9LrXp
 */

//package sketches.main;

import sketches.classes.Player;

static final int DIAM = 48, SPD = 4, FPS = 60;
static final color BG = 0350;

Player p;

void setup() {
  size(800, 600);

  smooth(3);
  frameRate(FPS);
  ellipseMode(CENTER);

  fill(Player.INK);
  stroke(Player.OUTLINE);
  strokeWeight(Player.BOLD);

  //p = new Player(width >> 1, height >> 1, DIAM, SPD);
  p = new Player(this, width >> 1, height >> 1, DIAM, SPD);
}

void draw() {
  background(BG);
  p.move();
  p.display();
}

void keyPressed() {
  p.setMove(keyCode, true);
}

void keyReleased() {
  p.setMove(keyCode, false);
}

```

A very quick way to convert it to Java syntax is by using PDE’s “Export Application” option (hit CTRL+SHIFT+E). 💡

The “Player\_Move.pde” will be output as “Player\_Move.java”. 😉

I believe you can move it to Eclipse and hopefully very little or even no changes will be needed. 🌑

---

_[View the full topic](https://discourse.processing.org/t/keypressed-and-keyreleased-with-java-eclipse/12994)._
