Skip to content

Commit

Permalink
Added left and right hotkeys.
Browse files Browse the repository at this point in the history
closes #20
#22
  • Loading branch information
NathanSweet committed Jun 11, 2019
1 parent 5bc5d16 commit bb8b464
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 27 deletions.
22 changes: 17 additions & 5 deletions src/com/esotericsoftware/clippy/Clippy.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,23 @@ public void uncaughtException (Thread thread, Throwable ex) {
final KeyStroke toggleHotkey = KeyStroke.getKeyStroke(config.toggleHotkey);
final KeyStroke popupHotkey = KeyStroke.getKeyStroke(config.popupHotkey);
final KeyStroke uploadHotkey = KeyStroke.getKeyStroke(config.uploadHotkey);

final KeyStroke screenshotHotkey = KeyStroke.getKeyStroke(config.screenshotHotkey);
final KeyStroke screenshotAppHotkey = KeyStroke.getKeyStroke(config.screenshotAppHotkey);
final KeyStroke screenshotRegionHotkey = KeyStroke.getKeyStroke(config.screenshotRegionHotkey);
final KeyStroke screenshotLastRegionHotkey = KeyStroke.getKeyStroke(config.screenshotLastRegionHotkey);

final KeyStroke tobiiClickHotkey = config.tobiiEnabled ? KeyStroke.getKeyStroke(config.tobiiClickHotkey) : null;
final KeyStroke tobiiClickHotkey2 = tobiiClickHotkey != null && tobiiClickHotkey.getKeyCode() == KeyEvent.VK_CAPS_LOCK
? KeyStroke.getKeyStroke("ctrl CAPS_LOCK")
: null; // Enables ctrl + click.
final KeyStroke tobiiMoveHotkey = config.tobiiEnabled ? KeyStroke.getKeyStroke(config.tobiiMoveHotkey) : null;
final KeyStroke tobiiRightClickHotkey = config.tobiiEnabled ? KeyStroke.getKeyStroke(config.tobiiRightClickHotkey) : null;
final KeyStroke tobiiLeftClickHotkey = config.tobiiEnabled ? KeyStroke.getKeyStroke(config.tobiiLeftClickHotkey) : null;

List<KeyStroke> keys = Arrays.asList(toggleHotkey, popupHotkey, uploadHotkey, screenshotHotkey, screenshotAppHotkey,
screenshotRegionHotkey, screenshotLastRegionHotkey, tobiiClickHotkey, tobiiClickHotkey2, tobiiMoveHotkey);
screenshotRegionHotkey, screenshotLastRegionHotkey, tobiiClickHotkey, tobiiClickHotkey2, tobiiMoveHotkey,
tobiiRightClickHotkey, tobiiLeftClickHotkey);
keyboard = new Keyboard() {
protected void hotkey (KeyStroke keyStroke) {
if (keyStroke.equals(toggleHotkey)) {
Expand All @@ -200,10 +206,16 @@ else if (keyStroke.equals(screenshotRegionHotkey)) //
screenshot.region();
else if (keyStroke.equals(screenshotLastRegionHotkey)) //
screenshot.lastRegion();
else if (keyStroke.equals(tobiiClickHotkey) || keyStroke.equals(tobiiClickHotkey2)) //
tobii.hotkeyPressed(tobiiClickHotkey.getKeyCode(), true);
else if (keyStroke.equals(tobiiMoveHotkey)) //
tobii.hotkeyPressed(tobiiMoveHotkey.getKeyCode(), false);
else if (tobii.connected) {
if (keyStroke.equals(tobiiClickHotkey) || keyStroke.equals(tobiiClickHotkey2)) //
tobii.hotkeyPressed(tobiiClickHotkey.getKeyCode(), true);
else if (keyStroke.equals(tobiiMoveHotkey)) //
tobii.hotkeyPressed(tobiiMoveHotkey.getKeyCode(), false);
else if (keyStroke.equals(tobiiLeftClickHotkey)) //
tobii.hotkeyLeftClick();
else if (keyStroke.equals(tobiiRightClickHotkey)) //
tobii.hotkeyRightClick();
}
}
};
for (KeyStroke key : keys) {
Expand Down
22 changes: 12 additions & 10 deletions src/com/esotericsoftware/clippy/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
import static com.esotericsoftware.clippy.util.Util.*;
import static com.esotericsoftware.minlog.Log.*;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map.Entry;

import com.esotericsoftware.clippy.PhilipsHue.PhilipsHueTimeline;
import com.esotericsoftware.clippy.util.ColorTimeline;
import com.esotericsoftware.clippy.util.Sun;
Expand All @@ -33,15 +42,6 @@
import com.esotericsoftware.jsonbeans.JsonWriter;
import com.esotericsoftware.minlog.Log;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map.Entry;

/** @author Nathan Sweet */
public class Config {
static public final Clippy clippy = Clippy.instance;
Expand Down Expand Up @@ -108,8 +108,10 @@ public class Config {
public ArrayList<PhilipsHueLights> philipsHue;

public boolean tobiiEnabled;
public String tobiiMoveHotkey;
public String tobiiClickHotkey = "CAPS_LOCK";
public String tobiiMoveHotkey;
public String tobiiLeftClickHotkey;
public String tobiiRightClickHotkey;
public float tobiiHeadSensitivityX = 6;
public float tobiiHeadSensitivityY = 8;

Expand Down
54 changes: 42 additions & 12 deletions src/com/esotericsoftware/clippy/Tobii.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import static com.esotericsoftware.clippy.util.Util.*;
import static com.esotericsoftware.minlog.Log.*;

import com.esotericsoftware.clippy.tobii.EyeX;
import com.esotericsoftware.clippy.util.SharedLibraryLoader;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import com.esotericsoftware.clippy.tobii.EyeX;
import com.esotericsoftware.clippy.util.SharedLibraryLoader;

public class Tobii {
static private final int mouseAnimationMillis = 100; // Zero disables animating mouse movement.
static private final int screenLeft = 10, screenTop = 10; // Margins to keep mouse on screen, useful for bottom and right.
Expand All @@ -34,7 +34,7 @@ public class Tobii {
final Clippy clippy = Clippy.instance;
EyeX eyeX;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
boolean connected, hotkeyPressed, mouseControl, storeHeadAdjustment, ignoreNextHotkey;
boolean connected, hotkeyPressed, hotkeyLeftClick, hotkeyRightClick, mouseControl, storeHeadAdjustment, ignoreNextHotkey;

double gazeX, gazeY, gazeStartX, gazeStartY, gazeSnappedX, gazeSnappedY;
final Buffer gazeJumpSamples = new Buffer(gazeJumpSampleCount * 2);
Expand All @@ -44,7 +44,7 @@ public class Tobii {
int headSamplesSkipped;

final Point mouse = new Point();
int mouseLastX, mouseLastY, mouseStartX, mouseStartY, mouseEndX, mouseEndY;
int mouseLastX, mouseLastY, mouseStartX, mouseStartY, mouseEndX, mouseEndY, mouseButtonPressed;
long mouseMoveLastTime, mouseDownTime;
double mouseMoveTime;
boolean mouseDrag;
Expand Down Expand Up @@ -174,15 +174,19 @@ public synchronized void hotkeyPressed (final int vk, final boolean clickOnRelea

hotkeyPressed = true;
mouseControl = true;
hotkeyLeftClick = false;
hotkeyRightClick = false;
storeHeadAdjustment = true;
screen = Toolkit.getDefaultToolkit().getScreenSize();

mouseDrag = mouseDownTime > 0;
mouseDownTime = 0;
if (mouseDrag)
setMousePosition(hotkeyReleaseX, hotkeyReleaseY, false);
else
else {
mouseButtonPressed = 0;
setMouseToGaze();
}

threadPool.submit(new Runnable() {
public void run () {
Expand All @@ -201,8 +205,8 @@ synchronized boolean hotkeyPressedTick (int vk, boolean clickOnRelease) {
// Shift aborts without clicking.
if (clippy.keyboard.isKeyDown(KeyEvent.VK_SHIFT)) return false;

// Click when hotkey is released.
if (!clippy.keyboard.isKeyDown(vk)) {
// Click when hotkey is released or aborted.
if (!hotkeyPressed || !clippy.keyboard.isKeyDown(vk)) {
if (clickOnRelease && System.currentTimeMillis() - hotkeyReleaseTime < doubleClickTime) {
// If a double click, use the same position as the mouse down.
setMousePosition(hotkeyReleaseX, hotkeyReleaseY, false);
Expand All @@ -226,8 +230,11 @@ synchronized boolean hotkeyPressedTick (int vk, boolean clickOnRelease) {
}
setGridOffset(gazeStartX, gazeStartY, mouse.x - gazeStartX, mouse.y - gazeStartY);
}
if (clickOnRelease) {
robot.mousePress(InputEvent.BUTTON1_MASK);
if (clickOnRelease || hotkeyLeftClick || hotkeyRightClick) {
if (hotkeyRightClick)
robot.mousePress(mouseButtonPressed = InputEvent.BUTTON3_DOWN_MASK);
else
robot.mousePress(mouseButtonPressed = InputEvent.BUTTON1_DOWN_MASK);
mouseDownTime = System.currentTimeMillis();
}
}
Expand Down Expand Up @@ -259,7 +266,7 @@ void hotkeyReleased (int vk) {
// Stop mouse control.
boolean mouseDown;
synchronized (this) {
if (mouseDrag) robot.mouseRelease(InputEvent.BUTTON1_MASK);
if (mouseDrag) robot.mouseRelease(mouseButtonPressed);
mouseDown = mouseDownTime > 0;
mouseControl = false;
}
Expand Down Expand Up @@ -292,13 +299,36 @@ void hotkeyReleased (int vk) {
sleep(Math.max(0, doubleTapDragTime - (System.currentTimeMillis() - mouseDownTime)));
synchronized (this) {
if (mouseDownTime > 0) {
robot.mouseRelease(mouseButtonPressed);
mouseDownTime = 0;
robot.mouseRelease(InputEvent.BUTTON1_MASK);
mouseButtonPressed = 0;
}
}
}
}

synchronized void hotkeyLeftClick () {
if (!connected) return;
if (hotkeyPressed) {
hotkeyPressed = false; // Abort existing movement.
hotkeyLeftClick = true;
} else {
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
}
}

synchronized void hotkeyRightClick () {
if (!connected) return;
if (hotkeyPressed) {
hotkeyPressed = false; // Abort existing movement.
hotkeyRightClick = true;
} else {
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
}
}

void setMouseToGaze () {
double x = gazeX, y = gazeY;
gazeStartX = x;
Expand Down

0 comments on commit bb8b464

Please sign in to comment.