Pad uit register lezen toegevoegd.

This commit is contained in:
2011-02-20 17:44:40 +00:00
parent 854134a3c9
commit dff79afa29
6 changed files with 91 additions and 41 deletions

View File

@@ -59,7 +59,7 @@ public class Main extends TaskListener {
//add(new WMPApplication());
add(new GomPlayerApplication());
//add(new WinampApplication());
//add(new iTunesApplication());
add(new iTunesApplication());
for (Application application : applicationCycle) {
application.start();
}

View File

@@ -1,6 +1,6 @@
package pm.application.windows;
public enum VirtualKey {
public enum Key {
/*
* VK_0 - VK_9 are the same as ASCII '0' - '9' (0x30 - 0x39)
* 0x40 : unassigned
@@ -170,7 +170,7 @@ public enum VirtualKey {
protected int code;
private VirtualKey(int code) {
private Key(int code) {
this.code = code;
}

View File

@@ -0,0 +1,18 @@
package pm.application.windows;
public enum Type {
UP (0x0101), // WM_KEYUP
DOWN (0x0100), // WM_KEYDOWN
SYSUP (0x0105), // WM_SYSKEYUP
SYSDOWN (0x0104); // WM_SYSKEYDOWN
protected int code;
private Type(int code) {
this.code = code;
}
public int getCode() {
return code;
}
}

View File

@@ -18,9 +18,7 @@ abstract public class WindowsApplication extends Application {
protected final static int START_SLEEP = 500;
protected final static int WM_APPCOMMAND = 0x0319;
protected final static int WM_KEYDOWN = 0x0100;
protected String path;
protected String program;
protected String name;
protected String target;
@@ -29,6 +27,7 @@ abstract public class WindowsApplication extends Application {
protected int handle;
protected IntCall sendMessage;
protected IntCall postMessage;
protected IntCall mapVirtualKey;
static {
try {
@@ -38,11 +37,10 @@ abstract public class WindowsApplication extends Application {
}
}
public WindowsApplication(String path, String program, String name) {
this.path = path;
public WindowsApplication(String program, String name, String target) {
this.program = program;
this.name = name;
target = path + program;
this.target = target;
handle = -1;
}
@@ -71,6 +69,7 @@ abstract public class WindowsApplication extends Application {
}
sendMessage = new IntCall("user32", "SendMessageA");
postMessage = new IntCall("user32", "PostMessageA");
mapVirtualKey = new IntCall("user32", "MapVirtualKeyA");
}
public void exit() throws ApplicationExitException {
@@ -88,19 +87,20 @@ abstract public class WindowsApplication extends Application {
}
}
protected void key(int key) throws SendKeyException {
int result = postMessage.executeCall(new Object[] {
handle, WM_KEYDOWN, key});
protected void key(Type key, int code) throws SendKeyException {
int scanCode = mapVirtualKey.executeCall(new Object[] {code, 0});
int result = postMessage.executeCall(new Object[] {
handle, key.getCode(), code, (scanCode << 16)});
if (result < 1 || postMessage.getLastError() != null) {
throw new SendKeyException();
}
}
protected void key(char key) throws SendKeyException {
key((int) Character.toUpperCase(key));
protected void key(Type key, char character) throws SendKeyException {
key(key, (int) Character.toUpperCase(character));
}
protected void key(VirtualKey virtualKey) throws SendKeyException {
key(virtualKey.getCode());
protected void key(Type key, Key virtualKey) throws SendKeyException {
key(key, virtualKey.getCode());
}
}

View File

@@ -1,31 +1,23 @@
package pm.application.windows.gomplayer;
import java.io.IOException;
import pm.Action;
import pm.application.windows.Command;
import pm.application.windows.VirtualKey;
import pm.application.windows.Type;
import pm.application.windows.Key;
import pm.application.windows.WindowsApplication;
import pm.exception.application.ApplicationInitialiseException;
import pm.exception.application.windows.SendCommandException;
import pm.exception.application.windows.SendKeyException;
import pm.util.Native;
public class GomPlayerApplication extends WindowsApplication {
protected final static String PATH = "C:\\Program Files (x86)\\GRETECH\\GomPlayer\\";
protected final static String REGISTRY = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Clients\\Media\\GomPlayer\\shell\\open\\command";
protected final static String PROGRAM = "GOM.exe";
protected final static String NAME = "GOM Player";
protected boolean playing;
public GomPlayerApplication() {
super(PATH, PROGRAM, NAME);
playing = false;
}
public void initialise() {
try {
super.initialise();
} catch (ApplicationInitialiseException e) {
e.printStackTrace();
}
super(PROGRAM, NAME, Native.getValue(REGISTRY));
}
public void action(Action action) {
@@ -34,7 +26,7 @@ public class GomPlayerApplication extends WindowsApplication {
try {
switch (action) {
case PLAY:
key(VirtualKey.SPACE);
key(Type.DOWN, Key.SPACE);
break;
case NEXT:
command(Command.MEDIA_NEXTTRACK);
@@ -49,13 +41,13 @@ public class GomPlayerApplication extends WindowsApplication {
command(Command.MEDIA_REWIND);
break;
case MUTE:
key('m');
key(Type.DOWN, 'm');
break;
case VOLUME_UP:
key(VirtualKey.UP);
key(Type.DOWN, Key.UP);
break;
case VOLUME_DOWN:
key(VirtualKey.DOWN);
key(Type.DOWN, Key.DOWN);
break;
case SHUFFLE:
//

View File

@@ -2,7 +2,9 @@ package pm.util;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Native {
@@ -10,16 +12,54 @@ public class Native {
File file = new File("native/list.exe");
Process process = Runtime.getRuntime().exec(file.getPath());
Scanner scanner = new Scanner(process.getInputStream());
ArrayList<Integer> handleList = new ArrayList<Integer>();
ArrayList<String> titleList = new ArrayList<String>();
while (scanner.hasNextLine()) {
try {
int handle = new Integer(scanner.nextLine());
String title = scanner.nextLine();
if (title.contains(name)) {
System.out.println("Window (" + handle + "): \"" + title + "\"");
return handle;
}
int handle = new Integer(scanner.nextLine());
String title = scanner.nextLine();
if (title.contains(name)) {
handleList.add(handle);
titleList.add(title);
}
} catch (InputMismatchException e) {}
}
return -1;
int count = handleList.size();
if (count == 1) {
return handleList.get(0);
}
for (int i = 0; i < count; ++i) {
if (titleList.get(i).endsWith(name)) {
return handleList.get(i);
}
}
return count > 0 ? handleList.get(0) : -1;
}
public static String getValue(String key, String name) throws IOException {
Process process = Runtime.getRuntime().exec("reg query " + key);
Scanner processScanner = new Scanner(process.getInputStream());
try {
processScanner.nextLine();
processScanner.nextLine();
while (processScanner.hasNextLine()) {
String line = processScanner.nextLine();
int index = line.indexOf(name);
if (index > -1) {
int begin = index + name.length() + 1;
Scanner lineScanner = new Scanner(line.substring(begin));
lineScanner.next();
return lineScanner.nextLine().trim();
}
}
} catch (NoSuchElementException e) {}
return null;
}
public static String getValue(String key) {
try {
return getValue(key, "(Default");
} catch (IOException e) {}
return null;
}
}