lejos.remote.ev3.RemoteKeys Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lejos-ev3-api Show documentation
Show all versions of lejos-ev3-api Show documentation
leJOS (pronounced like the Spanish word "lejos" for "far") is a tiny Java Virtual Machine. In 2013 it was ported to the LEGO EV3 brick.
The newest version!
package lejos.remote.ev3;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;
import lejos.hardware.Keys;
import lejos.hardware.port.PortException;
public class RemoteKeys implements Keys {
private RMIKeys keys;
private Map listeners;
private static final int PRESS_EVENT_SHIFT = 0;
private static final int RELEASE_EVENT_SHIFT = 8;
public RemoteKeys(RMIKeys keys) {
this.keys=keys;
}
@Override
public void discardEvents() {
try {
keys.discardEvents();
} catch (RemoteException e) {
throw new PortException(e);
}
}
@Override
public int waitForAnyEvent() {
try {
return keys.waitForAnyEvent();
} catch (RemoteException e) {
throw new PortException(e);
}
}
@Override
public int waitForAnyEvent(int timeout) {
try {
return keys.waitForAnyEvent(timeout);
} catch (RemoteException e) {
throw new PortException(e);
}
}
@Override
public int waitForAnyPress(int timeout) {
try {
return keys.waitForAnyPress(timeout);
} catch (RemoteException e) {
throw new PortException(e);
}
}
@Override
public int waitForAnyPress() {
try {
return keys.waitForAnyPress();
} catch (RemoteException e) {
throw new PortException(e);
}
}
@Override
public int getButtons() {
try {
return keys.getButtons();
} catch (RemoteException e) {
throw new PortException(e);
}
}
@Override
public int readButtons() {
try {
return keys.readButtons();
} catch (RemoteException e) {
throw new PortException(e);
}
}
@Override
public void setKeyClickVolume(int vol) {
try {
keys.setKeyClickVolume(vol);
} catch (RemoteException e) {
throw new PortException(e);
}
}
@Override
public int getKeyClickVolume() {
try {
return keys.getKeyClickVolume();
} catch (RemoteException e) {
throw new PortException(e);
}
}
@Override
public void setKeyClickLength(int len) {
try {
keys.setKeyClickLength(len);
} catch (RemoteException e) {
throw new PortException(e);
}
}
@Override
public int getKeyClickLength() {
try {
return keys.getKeyClickLength();
} catch (RemoteException e) {
throw new PortException(e);
}
}
@Override
public void setKeyClickTone(int key, int freq) {
try {
keys.setKeyClickTone(key, freq);
} catch (RemoteException e) {
throw new PortException(e);
}
}
@Override
public int getKeyClickTone(int key) {
try {
return keys.getKeyClickTone(key);
} catch (RemoteException e) {
throw new PortException(e);
}
}
void addListener(int iCode,RemoteKey key) {
if (listeners == null) {
listeners = new HashMap();
new KeysListenThread().start();
}
listeners.put(iCode, key);
}
class KeysListenThread extends Thread {
public KeysListenThread() {
setDaemon(true);
}
@Override
public void run() {
while (true) {
int state = RemoteKeys.this.waitForAnyEvent();
int mask = 1;
for (int i=0;i