Skip to content

Commit

Permalink
Added pause feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickolasbradham committed Nov 23, 2022
1 parent f730ed0 commit 5264406
Showing 1 changed file with 64 additions and 22 deletions.
86 changes: 64 additions & 22 deletions src/nbradham/mouseUtil/MCUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,43 @@

import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener;
import com.github.kwhat.jnativehook.mouse.NativeMouseEvent;
import com.github.kwhat.jnativehook.mouse.NativeMouseMotionListener;

public final class MCUtil {
/**
* The entire utility.
*
* @author Nickolas Bradham
*
*/
public final class MCUtil implements NativeKeyListener, NativeMouseMotionListener {

public static final void main(String[] args) throws AWTException, NativeHookException {
Robot r = new Robot();
private final JLabel coordsLabel = new JLabel("P to Pause Coordinates: Waiting for Movement..."),
hueLabel = new JLabel("Hue: Waiting...");
JFrame frame = new JFrame("Mouse and Color Util");
private final Robot r;
private long lastUpdate = 0;
private boolean unpaused = true;

GlobalScreen.registerNativeHook();
/**
* Constructs a new MCUtil
*
* @throws AWTException Thrown by {@link Robot#Robot()}.
*/
private MCUtil() throws AWTException {
r = new Robot();
}

/**
* Builds and shows the GUI. Registers listeners.
*
* @throws NativeHookException Thrown by
* {@link GlobalScreen#registerNativeHook()}.
*/
private void start() throws NativeHookException {
SwingUtilities.invokeLater(() -> {

JFrame frame = new JFrame("Mouse and Color Util");
JLabel coordsLabel = new JLabel("Coordinates: Waiting for Movement..."),
hueLabel = new JLabel("Hue: Waiting...");

GlobalScreen.addNativeMouseMotionListener(new NativeMouseMotionListener() {
@Override
public void nativeMouseMoved(NativeMouseEvent nativeEvent) {

Point p = MouseInfo.getPointerInfo().getLocation();
Color c = r.getPixelColor(p.x, p.y);

coordsLabel.setText(String.format("Coordinates: (%d, %d)", p.x, p.y));
hueLabel.setText(
String.format("Hue: %f", Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null)[0]));
}
});

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.add(coordsLabel);
Expand All @@ -63,5 +71,39 @@ public void windowClosing(WindowEvent e) {

frame.setVisible(true);
});

GlobalScreen.registerNativeHook();
GlobalScreen.addNativeKeyListener(this);
GlobalScreen.addNativeMouseMotionListener(this);
}

@Override
public void nativeKeyPressed(NativeKeyEvent nativeEvent) {
if (nativeEvent.getKeyCode() == NativeKeyEvent.VC_P)
unpaused = !unpaused;
}

@Override
public void nativeMouseMoved(NativeMouseEvent nativeEvent) {
long time = System.currentTimeMillis();
if (unpaused && time - lastUpdate > 16 && frame.isVisible()) {
Point p = MouseInfo.getPointerInfo().getLocation();
Color c = r.getPixelColor(p.x, p.y);

coordsLabel.setText(String.format("P to Pause Coordinates: (%d, %d)", p.x, p.y));
hueLabel.setText(String.format("Hue: %f", Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null)[0]));
lastUpdate = time;
}
}

/**
* Constructs and starts a new MCUtil instance.
*
* @param args Ignored.
* @throws AWTException Thrown by {@link #MCUtil()}.
* @throws NativeHookException Thrown by {@link #start()}.
*/
public static final void main(String[] args) throws AWTException, NativeHookException {
new MCUtil().start();
}
}

0 comments on commit 5264406

Please sign in to comment.