gpt4 book ai didi

java - KeyListener isShiftDown() 正在读取shift 是否向下

转载 作者:行者123 更新时间:2023-12-01 04:33:13 25 4
gpt4 key购买 nike

所以我正在构建一个 sphinx-4 程序,它仅在您按住 Shift 按钮时才会监听。这样我就可以防止错误,并且只有当我按住 Shift 按钮时才让它听我的。当我释放 Shift 按钮时,我希望程序等待,直到我再次按住它。当按下 ctrl-c 时,程序将完全退出。我正在使用按键监听器来执行此操作。

我遇到的问题是,在我按下 Shift 按钮后,程序确实开始监听,但是当我松开它时,它不会停止监听。我不确定我的代码有什么问题。这是我创建的 MKeyListener 类中的内容:

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();
}

这是我正在运行的主要类(class):

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"));
}



}

我在这里做错了什么?

最佳答案

你的方法看起来非常昂贵。您正在尝试做一些看似不必要且可能有问题的事情 - 尽管我不是 Sphinx4 的专家

我编写了一个应用程序,它执行完全相同的操作(除了它监听空格而不是移位)。我的方法完全不同,而且我认为非常轻量级。

配置.xml:

<component name="keyPressedSpeechClassifier"
type="package.KeyPressedSpeechClassifier">
</component>

<component name="epFrontEnd" type="edu.cmu.sphinx.frontend.FrontEnd">
<propertylist name="pipeline">
<item>microphone</item>
<item>speechClassifier</item>
<item>keyPressedSpeechClassifier</item>
<item>speechMarker</item>
<item>nonSpeechDataFilter</item>
<item>premphasizer</item>
<item>windower</item>
<item>fft</item>
<item>melFilterBank</item>
<item>dct</item>
<item>liveCMN</item>
<item>featureExtraction</item>
</propertylist>
</component>

按键语音分类器:

public class KeyPressedSpeechClassifier extends BaseDataProcessor {

static KeyPressedSpeechClassifier _instance;
public static KeyPressedSpeechClassifier getInstance() {return _instance;}

LinkedList<Data> queue;

volatile boolean isRecognitionEnabled = false;

public KeyPressedSpeechClassifier() {
queue = new LinkedList<Data>();
_instance = this;
System.out.println("KeyPressedSpeechClassifier created");
}

@Override
public Data getData() throws DataProcessingException {
Data data = getPredecessor().getData();
if (data instanceof DoubleData) {
DoubleData dd = (DoubleData) data;
queue.push(new SpeechClassifiedData(dd, isRecognitionEnabled));
} else if (data instanceof SpeechClassifiedData) {
((SpeechClassifiedData) data).setSpeech(isRecognitionEnabled);
queue.push(data);
} else {
queue.push(data);
}
if (queue.isEmpty()) {
return null;
} else {
return queue.pop();
}
}

public void setRecognitionEnabled(boolean enabled) {
this.isRecognitionEnabled = enabled;
}
}

关于java - KeyListener isShiftDown() 正在读取shift 是否向下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17703397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com