我有一个小程序,它与远程 UNIX 机器交互。一切正常。我正在关注 ganymede-ssh 代码。下面是代码摘录。
session = CLIUtil.createTerminal();
if (session != null) {
in = session.getStdout();
out = session.getStdin();
thread = new RemoteThread();
thread.start();
public void keyPressed(KeyEvent key) {
int keyCode = key.getKeyCode();
try {
else if (keyCode == KeyEvent.VK_L && ctrlMask)
{
cmd = CLIConstants.CLEAR_COMMAND;
out.write(new String(cmd + "\n").getBytes());
}
else if ((keyCode >= KeyEvent.VK_A && keyCode <= KeyEvent.VK_Z && !ctrlMask) ||
(keyCode >= KeyEvent.VK_0 && keyCode <= KeyEvent.VK_9 && !ctrlMask) ||
keyCode == KeyEvent.VK_QUOTE || keyCode == KeyEvent.VK_MINUS ||
keyCode == KeyEvent.VK_PERIOD || keyCode == KeyEvent.VK_SLASH ||
keyCode == KeyEvent.VK_BACK_SLASH || keyCode == KeyEvent.VK_SPACE)
{
int cursorPosition = terminalArea.getCaretPosition();
System.out.println("Cursor Position = " + cursorPosition);
terminalArea.append("" + key.getKeyChar());
cmd = cmd + key.getKeyChar();
terminalArea.setCaretPosition(cursorPosition + 1);
}
else if (keyCode == KeyEvent.VK_ENTER)
{
try {
out.write(new String(cmd + "\n").getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
class RemoteThread extends Thread {
int x = 100;
int y = 3000;
char[][] lines = new char[y][];
int posy = 0;
int posx = 0;
@Override
public void run() {
try {
while (true) {
byte[] buff = new byte[8192];
int len = in.read(buff);
if (len != -1) {
addTextToTerminal(buff, len);
if (cmd.startsWith(CLIConstants.CLEAR_COMMAND))
out.write("\n".getBytes());
cmd = "";
} else {
CLIUtil.terminateConnection();
}
public void addTextToTerminal(byte[] buff, int len) {
if (cmd.startsWith(CLIConstants.CLEAR_COMMAND)) {
try {
lines = null;
posx = posy = 0;
while (in.available() <= 10)
;
while (in.available() > 0) {
in.skip(in.available());
}
} catch (IOException e) {
e.printStackTrace();
}
return;
}
if (lines == null)
lines = new char[y][];
for (int i = 0; i < len; i++) {
char c = (char) (buff[i]);
// Backspace key
if (c == '\b') {
if (posx < 0)
continue;
posx--;
continue;
}
// Carriage Return
if (c == '\r') {
posx = 0;
continue;
}
// New Line
if (c == '\n') {
posy++;
if (posy >= y) {
for (int k = 1; k < y; k++)
lines[k - 1] = lines[k];
posy--;
lines[y - 1] = new char[x];
for (int k = 0; k < x; k++)
lines[y - 1][k] = ' ';
}
continue;
}
// Special Characters
if (c < 32) {
continue;
}
if (posx >= x) {
posx = 0;
posy++;
if (posy >= y) {
posy--;
for (int k = 1; k < y; k++)
lines[k - 1] = lines[k];
lines[y - 1] = new char[x];
for (int k = 0; k < x; k++)
lines[y - 1][k] = ' ';
}
}
if (lines[posy] == null) {
lines[posy] = new char[x];
for (int k = 0; k < x; k++)
lines[posy][k] = ' ';
}
lines[posy][posx] = c;
posx++;
}
StringBuffer sb = new StringBuffer(x * y);
for (int i = 0; i < lines.length; i++) {
if (i != 0)
sb.append('\n');
if (lines[i] != null) {
sb.append(lines[i]);
}
}
String text = sb.toString().trim();
terminalArea.setText(text + " ");
terminalArea.setCaretPosition(((x-1) * 100) + y);
terminalArea.repaint();
}
}
在这里我能够捕获 tab 键事件,但是要写入远程 unix 机器吗?
if(keyCode == KeyEvent.VK_TAB){
String temp = new String();
System.out.println("tab key event");
terminalArea.setFocusTraversalKeysEnabled(false);
//split_string(cmd);
out.write(); ?????
}
答案1
您可以使用以下方式发送关键事件java.awt.Robot
:
(请注意,这不是发送制表符*。)
像这样的东西会发送一个Tab事件 - 以显示以下内容的最小使用java.awt.Robot
:
public class SendTabKey {
public static void main(String[] args) { new SendTabKey(); }
public SendTabKey() { try {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_TAB);
} catch (Exception ex) { ex.printStackTrace(); }
}
}
(忽略除robot
线条之外的所有内容。)
*)
我并没有真正理解你想要做什么,所以这主要是对标题中明确问题的明确答案。
如果您想从小程序发送事件,您将需要使用权限java.awt.Robot
。
由于此类可以在 shell 中键入并运行命令,因此它显然不安全 - 这意味着您需要完全权限,例如运行外部命令。