Hola, tengo está clase de java donde utilizo el teclado como comando, y me gustaria para los mismos comando, en vez con el teclado usando botones. Si alguien me puede echar una mano:
import java.awt.KeyEventDispatcher;import java.awt.event.KeyEvent;
public class ControlTeclado implements KeyEventDispatcher, Runnable { private VueloDrone af = null; private Thread controller; private boolean stop; private boolean hascontrol = false; float speed = 0.4f; float theta = 0; float phi = 0; float gaz = 0; float yaw = 0; public ControlTeclado(VueloDrone af) { this.af = af; } public void start() { controller = new Thread(this); controller.start(); stop = false; } public void stop() { stop = true; } public boolean dispatchKeyEvent(KeyEvent e) { if (e.getID() == KeyEvent.KEY_PRESSED) { switch(e.getKeyCode()) { case KeyEvent.VK_P: af.takePhoto(); break; case KeyEvent.VK_R: af.toggleRecording(); break; case KeyEvent.VK_T: af.toggleRecordingOnUSB(); break; case KeyEvent.VK_PAGE_UP: if(speed + 0.1f < 1.05f) { speed += 0.1f; } break; case KeyEvent.VK_PAGE_DOWN: if(speed - 0.1f > 0.05f) { speed -= 0.1f; } break; case KeyEvent.VK_F5: af.takeOff(); break; case KeyEvent.VK_F6: af.land(); break; case KeyEvent.VK_F12: af.emergency(); break; case KeyEvent.VK_F: af.flip(); VueloDrone.append("Flip!"); break; case KeyEvent.VK_W: theta = -speed; break; case KeyEvent.VK_S: theta = speed; break; case KeyEvent.VK_A: phi = -speed; break; case KeyEvent.VK_D: phi = speed; break; case KeyEvent.VK_UP: gaz = speed; break; case KeyEvent.VK_DOWN: gaz = -speed; break; case KeyEvent.VK_LEFT: yaw = -0.8f; break; case KeyEvent.VK_RIGHT: yaw = 0.8f; break; } //} if(e.getKeyCode() == KeyEvent.VK_CONTROL) { setControl(true); } if(e.isAltDown()) { if(!af.isScriptExecuting()) { for(int i = 0; i < af.shortcuts.size(); i++) { if(Character.toUpperCase(e.getKeyChar()) == ((char) af.shortcuts.get(i)[2])) { af.runScriptFromShortcut(i); } } } } if(e.getKeyCode() == KeyEvent.VK_F1) { af.requestUserControl(); } if(e.getKeyCode() == KeyEvent.VK_ESCAPE) { af.leaveFullscreenMode(); af.video.toggleHUD(); } if(e.getKeyCode() == KeyEvent.VK_ENTER) { if(e.isAltDown()){ af.enterFullscreenMode(); af.video.toggleHUD(); }} if(e.getKeyCode() == KeyEvent.VK_F3) { af.video.toggleDebugInfo(); af.toggleDebugInfo(); } } else if (e.getID() == KeyEvent.KEY_RELEASED) { switch(e.getKeyCode()) { case KeyEvent.VK_W: theta = 0; break; case KeyEvent.VK_S: theta = 0; break; case KeyEvent.VK_A: phi = 0; break; case KeyEvent.VK_D: phi = 0; break; case KeyEvent.VK_UP: gaz = 0; break; case KeyEvent.VK_DOWN: gaz = 0; break; case KeyEvent.VK_LEFT: yaw = 0; break; case KeyEvent.VK_RIGHT: yaw = 0; break; // } } if(e.getKeyCode() == KeyEvent.VK_CONTROL) { setControl(false); af.ctrler_move(0, 0, 0, 0); } } else if (e.getID() == KeyEvent.KEY_TYPED) { } return false; } public void setControl(boolean hascontrol) { this.hascontrol = hascontrol; if(af.ctrlpoll != null) { af.ctrlpoll.setControl(!hascontrol); } } public boolean hasControl() { return hascontrol; } public void run() { while(!stop) { if(hasControl()) { af.ctrler_move(phi, theta, gaz, yaw); } try { Thread.sleep(10); } catch(InterruptedException e) { } } }}
Hola, tengo está clase de java donde utilizo el teclado como comando, y me gustaria para los mismos comando, en vez con el teclado usando botones. Si alguien me puede echar una mano:
import java.awt.KeyEventDispatcher;
import java.awt.event.KeyEvent;
public class ControlTeclado implements KeyEventDispatcher, Runnable {
private VueloDrone af = null;
private Thread controller;
private boolean stop;
private boolean hascontrol = false;
float speed = 0.4f;
float theta = 0;
float phi = 0;
float gaz = 0;
float yaw = 0;
public ControlTeclado(VueloDrone af) {
this.af = af;
}
public void start() {
controller = new Thread(this);
controller.start();
stop = false;
}
public void stop() {
stop = true;
}
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_PRESSED) {
switch(e.getKeyCode()) {
case KeyEvent.VK_P:
af.takePhoto();
break;
case KeyEvent.VK_R:
af.toggleRecording();
break;
case KeyEvent.VK_T:
af.toggleRecordingOnUSB();
break;
case KeyEvent.VK_PAGE_UP:
if(speed + 0.1f < 1.05f) {
speed += 0.1f;
}
break;
case KeyEvent.VK_PAGE_DOWN:
if(speed - 0.1f > 0.05f) {
speed -= 0.1f;
}
break;
case KeyEvent.VK_F5:
af.takeOff();
break;
case KeyEvent.VK_F6:
af.land();
break;
case KeyEvent.VK_F12:
af.emergency();
break;
case KeyEvent.VK_F:
af.flip();
VueloDrone.append("Flip!");
break;
case KeyEvent.VK_W:
theta = -speed;
break;
case KeyEvent.VK_S:
theta = speed;
break;
case KeyEvent.VK_A:
phi = -speed;
break;
case KeyEvent.VK_D:
phi = speed;
break;
case KeyEvent.VK_UP:
gaz = speed;
break;
case KeyEvent.VK_DOWN:
gaz = -speed;
break;
case KeyEvent.VK_LEFT:
yaw = -0.8f;
break;
case KeyEvent.VK_RIGHT:
yaw = 0.8f;
break;
}
//}
if(e.getKeyCode() == KeyEvent.VK_CONTROL) {
setControl(true);
}
if(e.isAltDown()) {
if(!af.isScriptExecuting()) {
for(int i = 0; i < af.shortcuts.size(); i++) {
if(Character.toUpperCase(e.getKeyChar()) == ((char) af.shortcuts.get(i)[2])) {
af.runScriptFromShortcut(i);
}
}
}
}
if(e.getKeyCode() == KeyEvent.VK_F1) {
af.requestUserControl();
}
if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
af.leaveFullscreenMode();
af.video.toggleHUD();
}
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
if(e.isAltDown()){
af.enterFullscreenMode();
af.video.toggleHUD();
}}
if(e.getKeyCode() == KeyEvent.VK_F3) {
af.video.toggleDebugInfo();
af.toggleDebugInfo();
}
} else if (e.getID() == KeyEvent.KEY_RELEASED) {
switch(e.getKeyCode()) {
case KeyEvent.VK_W:
theta = 0;
break;
case KeyEvent.VK_S:
theta = 0;
break;
case KeyEvent.VK_A:
phi = 0;
break;
case KeyEvent.VK_D:
phi = 0;
break;
case KeyEvent.VK_UP:
gaz = 0;
break;
case KeyEvent.VK_DOWN:
gaz = 0;
break;
case KeyEvent.VK_LEFT:
yaw = 0;
break;
case KeyEvent.VK_RIGHT:
yaw = 0;
break;
// }
}
if(e.getKeyCode() == KeyEvent.VK_CONTROL) {
setControl(false);
af.ctrler_move(0, 0, 0, 0);
}
} else if (e.getID() == KeyEvent.KEY_TYPED) {
}
return false;
}
public void setControl(boolean hascontrol) {
this.hascontrol = hascontrol;
if(af.ctrlpoll != null) {
af.ctrlpoll.setControl(!hascontrol);
}
}
public boolean hasControl() {
return hascontrol;
}
public void run() {
while(!stop) {
if(hasControl()) {
af.ctrler_move(phi, theta, gaz, yaw);
}
try {
Thread.sleep(10);
} catch(InterruptedException e) {
}
}
}
}