Skip to content
This repository has been archived by the owner on Sep 8, 2020. It is now read-only.

Commit

Permalink
Documentation and refactoring. New idle detection class
Browse files Browse the repository at this point in the history
  • Loading branch information
Col-E committed Mar 7, 2018
1 parent 6d329c2 commit 42ba283
Show file tree
Hide file tree
Showing 13 changed files with 461 additions and 285 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
*.MF
*.sh
/bin/
/target/
51 changes: 51 additions & 0 deletions src/me/coley/simplejna/Idle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package me.coley.simplejna;

import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.User32;

/**
* Utility for retrieving the idle time on Windows.
*
* @author ochafik
*/
public class Idle {

/**
* Get the time elapsed since the last input event <i>((mouse or keyboard))</i>
* in milliseconds.
*
* @return Time in milliseconds
*/
public static int dxInputEventTime() {
User32.LASTINPUTINFO lastInputInfo = new User32.LASTINPUTINFO();
User32.INSTANCE.GetLastInputInfo(lastInputInfo);
return Kernel32.INSTANCE.GetTickCount() - lastInputInfo.dwTime;
}

public static enum State {
UNKNOWN, ONLINE, IDLE, AWAY
};

//@formatter:off
/*
public static void main(String[] args) {
if (!System.getProperty("os.name").contains("Windows")) {
System.err.println("ERROR: Only implemented on Windows");
System.exit(1);
}
State state = State.UNKNOWN;
DateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
for (;;) {
int idleSec = dxInputEventTime() / 1000;
State newState = idleSec < 30 ? State.ONLINE : idleSec > 5 * 60 ? State.AWAY : State.IDLE;
if (newState != state) {
state = newState;
System.out.println(dateFormat.format(new Date()) + " # " + state);
}
try {
Thread.sleep(100);
} catch (Exception ex) {}
}
}
*/
}
122 changes: 68 additions & 54 deletions src/me/coley/simplejna/Keyboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,61 +11,75 @@
* Keyboard related methods and values.
*/
public class Keyboard {
public static final int KEYEVENTF_KEYDOWN = 0;
public static final int KEYEVENTF_KEYUP = 2;
public static final int KEYEVENTF_KEYDOWN = 0;
public static final int KEYEVENTF_KEYUP = 2;

/**
* Sends a key-down input followed by a key-up input for the given character
* value c.
*
* @param c
*/
public static void pressKey(int c) {
INPUT input = new INPUT();
input.type = new DWORD(INPUT.INPUT_KEYBOARD);
input.input.setType("ki");
input.input.ki.wScan = new WORD(0);
input.input.ki.time = new DWORD(0);
input.input.ki.dwExtraInfo = new ULONG_PTR(0);
input.input.ki.wVk = new WORD(c);
input.input.ki.dwFlags = new DWORD(KEYEVENTF_KEYDOWN);
User32.INSTANCE.SendInput(new DWORD(1), (INPUT[]) input.toArray(1), input.size());
input.input.ki.wVk = new WORD(c);
input.input.ki.dwFlags = new DWORD(KEYEVENTF_KEYUP);
User32.INSTANCE.SendInput(new DWORD(1), (INPUT[]) input.toArray(1), input.size());
}
/**
* Check if a key is pressed.
*
* @param vkCode
* Key-code. For example: <i>KeyEvent.VK_SHIFT </i>
*
* @return {@code true} if key is down. False otherwise.
*/
public static boolean isKeyDown(int vkCode) {
short state = User32.INSTANCE.GetAsyncKeyState(vkCode);
// check most-significant bit for non-zero.
return (0x1 & (state >> (Short.SIZE - 1))) != 0;
}

/**
* Sends a key-down input for the given character value c.
*
* @param c
*/
public static void sendKeyDown(int c) {
INPUT input = new INPUT();
input.type = new DWORD(INPUT.INPUT_KEYBOARD);
input.input.setType("ki");
input.input.ki.wScan = new WORD(0);
input.input.ki.time = new DWORD(0);
input.input.ki.dwExtraInfo = new ULONG_PTR(0);
input.input.ki.wVk = new WORD(c);
input.input.ki.dwFlags = new DWORD(KEYEVENTF_KEYDOWN);
User32.INSTANCE.SendInput(new DWORD(1), (INPUT[]) input.toArray(1), input.size());
}
/**
* Sends a key-down input followed by a key-up input for the given character
* value c.
*
* @param c
*/
public static void pressKey(int c) {
INPUT input = new INPUT();
input.type = new DWORD(INPUT.INPUT_KEYBOARD);
input.input.setType("ki");
input.input.ki.wScan = new WORD(0);
input.input.ki.time = new DWORD(0);
input.input.ki.dwExtraInfo = new ULONG_PTR(0);
input.input.ki.wVk = new WORD(c);
input.input.ki.dwFlags = new DWORD(KEYEVENTF_KEYDOWN);
User32.INSTANCE.SendInput(new DWORD(1), (INPUT[]) input.toArray(1), input.size());
input.input.ki.wVk = new WORD(c);
input.input.ki.dwFlags = new DWORD(KEYEVENTF_KEYUP);
User32.INSTANCE.SendInput(new DWORD(1), (INPUT[]) input.toArray(1), input.size());
}

/**
* Sends a key-up input for the given character value c.
*
* @param c
*/
public static void sendKeyUp(int c) {
INPUT input = new INPUT();
input.type = new DWORD(INPUT.INPUT_KEYBOARD);
input.input.setType("ki");
input.input.ki.wScan = new WORD(0);
input.input.ki.time = new DWORD(0);
input.input.ki.dwExtraInfo = new ULONG_PTR(0);
input.input.ki.wVk = new WORD(c);
input.input.ki.dwFlags = new DWORD(KEYEVENTF_KEYUP);
User32.INSTANCE.SendInput(new DWORD(1), (INPUT[]) input.toArray(1), input.size());
}
/**
* Sends a key-down input for the given character value c.
*
* @param c
*/
public static void sendKeyDown(int c) {
INPUT input = new INPUT();
input.type = new DWORD(INPUT.INPUT_KEYBOARD);
input.input.setType("ki");
input.input.ki.wScan = new WORD(0);
input.input.ki.time = new DWORD(0);
input.input.ki.dwExtraInfo = new ULONG_PTR(0);
input.input.ki.wVk = new WORD(c);
input.input.ki.dwFlags = new DWORD(KEYEVENTF_KEYDOWN);
User32.INSTANCE.SendInput(new DWORD(1), (INPUT[]) input.toArray(1), input.size());
}

/**
* Sends a key-up input for the given character value c.
*
* @param c
*/
public static void sendKeyUp(int c) {
INPUT input = new INPUT();
input.type = new DWORD(INPUT.INPUT_KEYBOARD);
input.input.setType("ki");
input.input.ki.wScan = new WORD(0);
input.input.ki.time = new DWORD(0);
input.input.ki.dwExtraInfo = new ULONG_PTR(0);
input.input.ki.wVk = new WORD(c);
input.input.ki.dwFlags = new DWORD(KEYEVENTF_KEYUP);
User32.INSTANCE.SendInput(new DWORD(1), (INPUT[]) input.toArray(1), input.size());
}
}
154 changes: 77 additions & 77 deletions src/me/coley/simplejna/Mouse.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,87 +10,87 @@
* Mouse related methods and values.
*/
public class Mouse {
public static final int MOUSEEVENTF_MOVE = 1;
public static final int MOUSEEVENTF_LEFTDOWN = 2;
public static final int MOUSEEVENTF_LEFTUP = 4;
public static final int MOUSEEVENTF_RIGHTDOWN = 8;
public static final int MOUSEEVENTF_RIGHTUP = 16;
public static final int MOUSEEVENTF_MIDDLEDOWN = 32;
public static final int MOUSEEVENTF_MIDDLEUP = 64;
public static final int MOUSEEVENTF_WHEEL = 2048;
public static final int MOUSEEVENTF_MOVE = 1;
public static final int MOUSEEVENTF_LEFTDOWN = 2;
public static final int MOUSEEVENTF_LEFTUP = 4;
public static final int MOUSEEVENTF_RIGHTDOWN = 8;
public static final int MOUSEEVENTF_RIGHTUP = 16;
public static final int MOUSEEVENTF_MIDDLEDOWN = 32;
public static final int MOUSEEVENTF_MIDDLEUP = 64;
public static final int MOUSEEVENTF_WHEEL = 2048;

/**
* Moves the mouse relative to it's current position.
*
* @param x
* Horizontal movement
* @param y
* Vertical movement
*/
public static void mouseMove(int x, int y) {
mouseAction(x, y, MOUSEEVENTF_MOVE);
}
/**
* Moves the mouse relative to it's current position.
*
* @param x
* Horizontal movement
* @param y
* Vertical movement
*/
public static void mouseMove(int x, int y) {
mouseAction(x, y, MOUSEEVENTF_MOVE);
}

/**
* Sends a left-click input at the given x,y coordinates. If -1 is given for
* each of the inputs it will send the input to the current position of the
* mouse.
*
* @param x
* @param y
*/
public static void mouseLeftClick(int x, int y) {
mouseAction(x, y, MOUSEEVENTF_LEFTDOWN);
mouseAction(x, y, MOUSEEVENTF_LEFTUP);
}
/**
* Sends a left-click input at the given x,y coordinates. If -1 is given for
* each of the inputs it will send the input to the current position of the
* mouse.
*
* @param x
* @param y
*/
public static void mouseLeftClick(int x, int y) {
mouseAction(x, y, MOUSEEVENTF_LEFTDOWN);
mouseAction(x, y, MOUSEEVENTF_LEFTUP);
}

/**
* Sends a right-click input at the given x,y coordinates. If -1 is given
* for each of the inputs it will send the input to the current position of
* the mouse.
*
* @param x
* @param y
*/
public static void mouseRightClick(int x, int y) {
mouseAction(x, y, MOUSEEVENTF_RIGHTDOWN);
mouseAction(x, y, MOUSEEVENTF_RIGHTUP);
}
/**
* Sends a right-click input at the given x,y coordinates. If -1 is given for
* each of the inputs it will send the input to the current position of the
* mouse.
*
* @param x
* @param y
*/
public static void mouseRightClick(int x, int y) {
mouseAction(x, y, MOUSEEVENTF_RIGHTDOWN);
mouseAction(x, y, MOUSEEVENTF_RIGHTUP);
}

/**
* Sends a middle-click input at the given x,y coordinates. If -1 is given
* for each of the inputs it will send the input to the current position of
* the mouse.
*
* @param x
* @param y
*/
public static void mouseMiddleClick(int x, int y) {
mouseAction(x, y, MOUSEEVENTF_MIDDLEDOWN);
mouseAction(x, y, MOUSEEVENTF_MIDDLEUP);
}
/**
* Sends a middle-click input at the given x,y coordinates. If -1 is given for
* each of the inputs it will send the input to the current position of the
* mouse.
*
* @param x
* @param y
*/
public static void mouseMiddleClick(int x, int y) {
mouseAction(x, y, MOUSEEVENTF_MIDDLEDOWN);
mouseAction(x, y, MOUSEEVENTF_MIDDLEUP);
}

/**
* Sends an action (flags) at the given x,y coordinates.
*
* @param x
* @param y
* @param flags
*/
public static void mouseAction(int x, int y, int flags) {
INPUT input = new INPUT();
/**
* Sends an action (flags) at the given x,y coordinates.
*
* @param x
* @param y
* @param flags
*/
public static void mouseAction(int x, int y, int flags) {
INPUT input = new INPUT();

input.type = new DWORD(INPUT.INPUT_MOUSE);
input.input.setType("mi");
if (x != -1) {
input.input.mi.dx = new LONG(x);
}
if (y != -1) {
input.input.mi.dy = new LONG(y);
}
input.input.mi.time = new DWORD(0);
input.input.mi.dwExtraInfo = new ULONG_PTR(0);
input.input.mi.dwFlags = new DWORD(flags);
User32.INSTANCE.SendInput(new DWORD(1), new INPUT[] { input }, input.size());
}
input.type = new DWORD(INPUT.INPUT_MOUSE);
input.input.setType("mi");
if (x != -1) {
input.input.mi.dx = new LONG(x);
}
if (y != -1) {
input.input.mi.dy = new LONG(y);
}
input.input.mi.time = new DWORD(0);
input.input.mi.dwExtraInfo = new ULONG_PTR(0);
input.input.mi.dwFlags = new DWORD(flags);
User32.INSTANCE.SendInput(new DWORD(1), new INPUT[] { input }, input.size());
}
}
Loading

0 comments on commit 42ba283

Please sign in to comment.