Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
CBenoit committed Mar 30, 2016
2 parents 566fe80 + f69e62b commit 6a7e39d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 28 deletions.
24 changes: 16 additions & 8 deletions src/main/java/fr/utbm/rngames/keyboard/KeyboardWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,21 @@
import java.util.logging.Logger;

public class KeyboardWriter extends KeyboardListener {
private static final Logger LOG;

private static final String CSV_SEPARATOR;
private static final String EVENT_NAME;
private static final String PRESSED_EVENT;
private static final String RELEASED_EVENT;

static {
CSV_SEPARATOR = Locale.getString("keyboard.csv.separator"); //$NON-NLS-1$
LOG = Logger.getLogger(KeyboardWriter.class.getName());
EVENT_NAME = Locale.getString("keyboard.event.name"); //$NON-NLS-1$
PRESSED_EVENT = Locale.getString("keyboard.event.pressed"); //$NON-NLS-1$
RELEASED_EVENT = Locale.getString("keyboard.event.released"); //$NON-NLS-1$
}

private final Logger logger = Logger.getLogger(KeyboardWriter.class.getName());

private final List<Integer> keysPressed = new ArrayList<>();
private final long startTime;
Expand Down Expand Up @@ -67,19 +75,19 @@ protected void close() {
try {
this.writer.close();
} catch (IOException exception) {
this.logger.severe(exception.getMessage());
LOG.severe(exception.getMessage());
}
}

@Override
public void nativeKeyPressed(NativeKeyEvent evt) {
if (!this.keysPressed.contains(evt.getKeyCode())) {
this.keysPressed.add(evt.getKeyCode());
if (!this.keysPressed.contains(new Integer(evt.getKeyCode()))) {
this.keysPressed.add(new Integer(evt.getKeyCode()));
try {
this.writer.write(generateFileEntry("Key Down", evt)); //$NON-NLS-1$
this.writer.write(generateFileEntry(EVENT_NAME + " " + PRESSED_EVENT, evt)); //$NON-NLS-1$
this.writer.newLine();
} catch (IOException exception) {
this.logger.severe(exception.getMessage());
LOG.severe(exception.getMessage());
}
}
}
Expand All @@ -88,10 +96,10 @@ public void nativeKeyPressed(NativeKeyEvent evt) {
public void nativeKeyReleased(NativeKeyEvent evt) {
this.keysPressed.remove(new Integer(evt.getKeyCode()));
try {
this.writer.write(generateFileEntry("Key Up", evt)); //$NON-NLS-1$
this.writer.write(generateFileEntry(EVENT_NAME + " " + RELEASED_EVENT, evt)); //$NON-NLS-1$
this.writer.newLine();
} catch (IOException exception) {
this.logger.severe(exception.getMessage());
LOG.severe(exception.getMessage());
}
}

Expand Down
43 changes: 25 additions & 18 deletions src/main/java/fr/utbm/rngames/mouse/MouseWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,25 @@
import java.util.logging.Logger;

public class MouseWriter extends MouseListener {
private static final Logger LOG;

private static final String CSV_SEPARATOR;
private static final String EVENT_NAME;
private static final String PRESSED_EVENT;
private static final String RELEASED_EVENT;
private static final String SCROLLED_EVENT;
private static final String MOVED_EVENT;

static {
CSV_SEPARATOR = Locale.getString("mouse.csv.separator"); //$NON-NLS-1$
LOG = Logger.getLogger(MouseWriter.class.getName());
EVENT_NAME = Locale.getString("mouse.event.name"); //$NON-NLS-1$
PRESSED_EVENT = Locale.getString("mouse.event.pressed"); //$NON-NLS-1$
RELEASED_EVENT = Locale.getString("mouse.event.released"); //$NON-NLS-1$
SCROLLED_EVENT = Locale.getString("mouse.event.scrolled"); //$NON-NLS-1$
MOVED_EVENT = Locale.getString("mouse.event.moved"); //$NON-NLS-1$
}

private final Logger logger = Logger.getLogger(MouseWriter.class.getName());

private final long startTime;
private final URL fileLocation;
private final BufferedWriter writer;
Expand All @@ -56,7 +67,7 @@ public MouseWriter(URL fileLocation) throws IOException {
this.writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),
StandardCharsets.UTF_8));

this.writer.write(Locale.getString("mouse.file.header"));
this.writer.write(Locale.getString("mouse.file.header")); //$NON-NLS-1$
this.writer.newLine();
}

Expand All @@ -65,7 +76,7 @@ public void close() {
try {
this.writer.close();
} catch (IOException exception) {
this.logger.severe(exception.getMessage());
LOG.severe(exception.getMessage());
}
}

Expand All @@ -77,22 +88,20 @@ public void nativeMouseClicked(NativeMouseEvent evt) {
@Override
public void nativeMousePressed(NativeMouseEvent evt) {
try {
this.writer.write(generateMouseButtonEntry(Locale.getString("mouse.event.pressed"), evt));
this.writer.write(generateMouseButtonEntry(PRESSED_EVENT, evt));
this.writer.newLine();
} catch (IOException exception) {
this.logger.severe(exception.getMessage());
// System.exit(-1);
LOG.severe(exception.getMessage());
}
}

@Override
public void nativeMouseReleased(NativeMouseEvent evt) {
try {
this.writer.write(generateMouseButtonEntry(Locale.getString("mouse.event.released"), evt));
this.writer.write(generateMouseButtonEntry(RELEASED_EVENT, evt));
this.writer.newLine();
} catch (IOException exception) {
this.logger.severe(exception.getMessage());
// System.exit(-1);
LOG.severe(exception.getMessage());
}
}

Expand All @@ -102,8 +111,7 @@ public void nativeMouseMoved(NativeMouseEvent evt) {
this.writer.write(generateMouseMoveEntry(evt));
this.writer.newLine();
} catch (IOException exception) {
this.logger.severe(exception.getMessage());
// System.exit(-1);
LOG.severe(exception.getMessage());
}
}

Expand All @@ -119,8 +127,7 @@ public void nativeMouseWheelMoved(NativeMouseWheelEvent evt) {
this.writer.write(generateMouseButtonEntry(evt));
this.writer.newLine();
} catch (IOException exception) {
this.logger.severe(exception.getMessage());
// System.exit(-1);
LOG.severe(exception.getMessage());
}
}

Expand All @@ -129,28 +136,28 @@ public final URL getFileLocation() {
}

private String generateMouseButtonEntry(String eventType, NativeMouseEvent evt) {
return "mouse " + evt.getButton() + " " + eventType + CSV_SEPARATOR //$NON-NLS-1$ //$NON-NLS-2$
return EVENT_NAME + " " + evt.getButton() + " " + eventType + CSV_SEPARATOR //$NON-NLS-1$ //$NON-NLS-2$
+ evt.getX() + CSV_SEPARATOR
+ evt.getY() + CSV_SEPARATOR
+ (evt.getWhen() - this.startTime);
}

private String generateMouseButtonEntry(NativeMouseWheelEvent evt) {
if (evt.getWheelRotation() > 0) {
return "mouse scroll down" + CSV_SEPARATOR //$NON-NLS-1$
return EVENT_NAME + " " + SCROLLED_EVENT + " " + PRESSED_EVENT + CSV_SEPARATOR //$NON-NLS-1$
+ evt.getX() + CSV_SEPARATOR
+ evt.getY() + CSV_SEPARATOR
+ (evt.getWhen() - this.startTime);
}

return "mouse scroll up" + CSV_SEPARATOR //$NON-NLS-1$
return EVENT_NAME + " " + SCROLLED_EVENT + " " + RELEASED_EVENT + CSV_SEPARATOR //$NON-NLS-1$
+ evt.getX() + CSV_SEPARATOR
+ evt.getY() + CSV_SEPARATOR
+ (evt.getWhen() - this.startTime);
}

private String generateMouseMoveEntry(NativeMouseEvent evt) {
return "mouse move" + CSV_SEPARATOR //$NON-NLS-1$
return EVENT_NAME + " " + MOVED_EVENT + CSV_SEPARATOR //$NON-NLS-1$
+ evt.getX() + CSV_SEPARATOR
+ evt.getY() + CSV_SEPARATOR
+ (evt.getWhen() - this.startTime);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
keyboard.file.header = Event;Key;Time
keyboard.csv.separator = ;
keyboard.file.name = rngamestmp.keyboard.csv
keyboard.event.name = Key
keyboard.event.pressed = Down
keyboard.event.released = Up
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
mouse.file.header = Event;X;Y;Time
mouse.csv.separator = ;
mouse.file.name = nrgamestmp.mouse.csv
mouse.event.pressed=down
mouse.event.released=up
mouse.event.name = mouse
mouse.event.pressed = down
mouse.event.released = up
mouse.event.scrolled = scroll
mouse.event.moved = move

0 comments on commit 6a7e39d

Please sign in to comment.