所以我正在学习SerialConsole
用 nachos (Java) 创建我自己的。我学会了使用Semaphore.P()
和Semaphore.V()
等待用户输入。一切都很顺利,直到我尝试制作像getch()
C's 中的函数conio.h
。
问题是,每当我调用 Semaphore.P() 时,即使调用了 Semaphore.V(),它也会Enter
在恢复程序之前始终等待按键被按下。我希望程序在我按下一个键时恢复。
下面是我尝试过的一些代码。
控制台.java
public class Console {
private SerialConsole console;
private Semaphore sem = new Semaphore(0);
private Runnable send, recv;
private char tempChar;
public Console() {
console = Machine.console();
send = new Runnable() {
@Override
public void run() {
sem.V();
}
};
recv = new Runnable() {
@Override
public void run() {
tempChar = (char) console.readByte();
sem.V();
}
};
console.setInterruptHandlers(recv, send);
}
public String nextLine() {
String result = "";
do {
sem.P();
if (tempChar != '\n') result += tempChar;
} while(tempChar != '\n');
return result;
}
public char getch() {
sem.P();
return tempChar;
}
}
主.java
public class Main {
private Console console;
public Main() {
console = new Console();
char c = console.getch();
System.out.println(c);
}
}
有什么我错过的,或者有什么方法可以以编程方式Enter
按键或其他东西吗?
PS:java.awt.Robot
不能在 nachos 项目中使用。
任何帮助,将不胜感激。