그래서 shift 버튼을 누르고있을 때만 들리는 sphinx-4 프로그램을 만들고 있습니다. 이것은 내가 실수를 막을 수 있고, 내가 시프트 버튼을 누르고있을 때만 내 말을들을 수있게하기 위해서입니다. Shift 버튼을 놓으면 프로그램을 다시 열 때까지 기다려야합니다. ctrl-c를 누르면 프로그램이 완전히 종료됩니다. 나는 키 릴스터를 사용하여 이것을하고있다.KeyListener isShiftDown()은 읽는 동안 shift가 내려지지 않습니다
내가 겪고있는 문제는 내가 시프트 버튼을 누른 후에 프로그램이 듣기 시작한다는 것이다. 그러나 내가 그것을 놓을 때 듣기를 멈추지는 않을 것이다. 내 코드가 무엇이 잘못되었는지 잘 모르겠습니다.
다음public class MKeyListener implements KeyListener {
static ConfigurationManager cm;
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("Please hold down the shift button to have Sphinx listen.");
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == KeyEvent.VK_SHIFT)
{
Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
recognizer.allocate();
// start the microphone or exit if the programm if this is not possible
Microphone microphone = (Microphone) cm.lookup("microphone");
if (!microphone.startRecording()) {
System.out.println("Cannot start microphone.");
recognizer.deallocate();
System.exit(1);
}
System.out.println("Say: (Good morning | Hello) (Bhiksha | Evandro | Paul | Philip | Rita | Will)");
// loop the recognition until the programm exits.
Boolean var = e.isShiftDown();
while (var == true) {
System.out.println("Start speaking. Press Ctrl-C to quit.\n");
Result result = recognizer.recognize();
if (result != null) {
String resultText = result.getBestFinalResultNoFiller();
System.out.println("You said: " + resultText + '\n');
} else {
System.out.println("I can't hear what you said.\n");
}
var = e.isShiftDown();
System.out.println(var);
}
recognizer.deallocate();
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == KeyEvent.VK_SHIFT)
{
Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
recognizer.deallocate();
}
내 주요 클래스는 내가 실행 해요입니다 : 내가 만든 여기 내 MKeyListener 클래스에 내가 가진 무엇인가
public class HelloWorld extends MKeyListener implements KeyListener{
public static void main(String[] args) throws Exception {
JTextField textField = new JTextField();
textField.addKeyListener(new MKeyListener());
JFrame jframe = new JFrame();
jframe.add(textField);
jframe.setSize(400, 350);
jframe.setVisible(true);
if (args.length > 0) {
cm = new ConfigurationManager(args[0]);
} else {
cm = new ConfigurationManager(HelloWorld.class.getResource("helloworld.config.xml"));
}
}
은 내가 잘못 여기서 뭐하는 거지?
당신을 감사합니다! 나는 실제로 완전히 다른 접근법을 사용하기로 결정했는데, 이것이 내가 더 잘 이해하는 데 도움이됩니다. – angyxpoo