diff --git a/moonlight-common/src/com/limelight/nvstream/NvConnection.java b/moonlight-common/src/com/limelight/nvstream/NvConnection.java index f9ea09ac..419aea3c 100644 --- a/moonlight-common/src/com/limelight/nvstream/NvConnection.java +++ b/moonlight-common/src/com/limelight/nvstream/NvConnection.java @@ -318,4 +318,21 @@ public class NvConnection { } }); } + + public void sendKeyboardInput(final short keyMap, final byte keyDirection) { + if (inputStream == null) + return; + + threadPool.execute(new Runnable() { + @Override + public void run() { + try { + inputStream.sendKeyboardInput(keyMap, keyDirection); + } catch (IOException e) { + listener.displayMessage(e.getMessage()); + NvConnection.this.stop(); + } + } + }); + } } diff --git a/moonlight-common/src/com/limelight/nvstream/input/KeyboardPacket.java b/moonlight-common/src/com/limelight/nvstream/input/KeyboardPacket.java new file mode 100644 index 00000000..24b1ba28 --- /dev/null +++ b/moonlight-common/src/com/limelight/nvstream/input/KeyboardPacket.java @@ -0,0 +1,50 @@ +package com.limelight.nvstream.input; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +public class KeyboardPacket extends InputPacket { + private static final int PACKET_TYPE = 0x0A; + private static final int PACKET_LENGTH = 14; + + public static final byte KEY_DOWN = 0x03; + public static final byte KEY_UP = 0x04; + + private short keyCode; + private byte keyDirection; + + public KeyboardPacket(short keyCode, byte keyDirection) { + super(PACKET_TYPE); + this.keyCode = keyCode; + this.keyDirection = keyDirection; + } + + public byte[] toWireHeader() + { + ByteBuffer bb = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN); + + bb.putInt(packetType); + + return bb.array(); + } + + @Override + public byte[] toWire() { + ByteBuffer bb = ByteBuffer.allocate(PACKET_LENGTH).order(ByteOrder.LITTLE_ENDIAN); + + bb.put(toWireHeader()); + bb.put(keyDirection); + bb.putShort((short)0); + bb.putShort((short)0); + bb.putShort(keyCode); + bb.put((byte)0); + bb.put((byte)0); + bb.put((byte)0); + byte[] packet = bb.array(); + for (int i = 0; i < packet.length; i++) { + System.out.printf("%02x ", packet[i]); + } + System.out.println(); + return bb.array(); + } +} diff --git a/moonlight-common/src/com/limelight/nvstream/input/KeycodeTranslator.java b/moonlight-common/src/com/limelight/nvstream/input/KeycodeTranslator.java new file mode 100644 index 00000000..e2ee4650 --- /dev/null +++ b/moonlight-common/src/com/limelight/nvstream/input/KeycodeTranslator.java @@ -0,0 +1,20 @@ +package com.limelight.nvstream.input; + +import com.limelight.nvstream.NvConnection; + +public abstract class KeycodeTranslator { + public abstract short translate(int keycode); + protected NvConnection conn; + + public KeycodeTranslator(NvConnection conn) { + this.conn = conn; + } + + public void sendKeyDown(short keyMap) { + conn.sendKeyboardInput(keyMap, KeyboardPacket.KEY_DOWN); + } + + public void sendKeyUp(short keyMap) { + conn.sendKeyboardInput(keyMap, KeyboardPacket.KEY_UP); + } +} diff --git a/moonlight-common/src/com/limelight/nvstream/input/NvController.java b/moonlight-common/src/com/limelight/nvstream/input/NvController.java index b0b85984..8060e57d 100644 --- a/moonlight-common/src/com/limelight/nvstream/input/NvController.java +++ b/moonlight-common/src/com/limelight/nvstream/input/NvController.java @@ -62,4 +62,11 @@ public class NvController { out.write(new MouseMovePacket(deltaX, deltaY).toWire()); out.flush(); } + + public void sendKeyboardInput(short keyMap, byte keyDirection) throws IOException + { + System.out.println("sending keyboard packet"); + out.write(new KeyboardPacket(keyMap, keyDirection).toWire()); + out.flush(); + } }