- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我正在构建一个 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/
我正在用 Java 开发一个非常非常简单的互联网浏览器。我已经把大部分内容都写下来了,但我在获取“按 Enter 键发送 URL”部分时遇到了困难。具体来说,当我使用 keyTyped() 方法时,我
我是一名编码新手,大约 10 年前有一点 C++ 经验,现在正在学习 java(大约 4-5 个月)。我正在进行一个小型协作项目,并且我有一些事情需要解决。 代码如下: import java.awt
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; public
我正在尝试向我的输入字段添加一个按键监听器,但无法使其工作。我的输入类型是 text 和 id= "code"。这是我尝试过的: document.querySelector('code').addE
我正在尝试用 Java 创建一个自动点击器(我只知道这门语言,而且我刚学过 Threads)。我想让小程序在它自己的窗口中打开(而不是在网页上),并且我希望能够在不选择窗口的情况下使用空格键启动和停止
我是 BlackBerry 应用程序开发的新手。我希望能够在黑莓(在我的情况下为 8900)打开并且在所有屏幕上时能够监听按键事件,这可能吗? 如果是这样,有人将我引向正确的方向会很棒。我已经在看接口
import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics;
我对 KeyListener 没有太多经验,但我在我的应用程序中使用了一个,它工作得很好,只是我需要等待输入才能继续我的程序。为此,我做了一个 while 循环,循环直到 String temp 不为
我正在编写一个小游戏。到目前为止,我有一个玩家,他可以在 2D 打印的瓷砖世界中跟随玩家移动。这里有一个小preview . 现在我的玩家移动代码如下所示: KeyListener 类 public
我对 Java 还很陌生,到目前为止我完全是自学的。 现在我正在尝试将 KeyListener 添加到我的 JFrame 类中,我不知道我做错了什么,需要一些帮助。欢迎任何其他提示。 我的 JFram
尽管我将框架注册到了 keyListener,但 keyListener 并未触发操作。在其他代码中,实现 KeyListener 接口(interface)、注册 JFrame 并重写适当的方法就是
我一直在研究这个蛇项目,但并不真正理解为什么键监听器实际上没有更改变量 char 键。我还有一些其他的按键监听器示例,它们都工作正常,但由于某种原因我的无法工作。一些帮助将不胜感激。非常感谢您的帮助。
我在使用 KeyListener 时遇到问题。我将 KeyListener 添加到我创建的 JPanel,但 KeyListener 不起作用。 我的主类代码是 package client; imp
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我正在尝试用java进行一个简单的 react 测试。当屏幕变绿时,我按空格键,女巫应该将 boolean 值“单击为 false 并停止测量时间的循环。实际上,关键监听器什么也不做。我是否将 kea
我有一个扩展 JFrame 的 GUI 类,以及一个扩展 KeyAdapter 的单独内部类 KeyPresses。在 GUI 的构造函数中,我将 KeyAdapter 的实例传递给 addKeyLi
我卡住了,当我点击箭头键时,我的图片无法加载。仅显示第一张图片。然后就什么都没有发生了。我希望能够通过单击箭头键来更改文件夹中的图片。这是代码。 import java.awt.Dimension;
我编写了一个简单的 KeyListener 来识别 Arrow_Left/Right 事件。由于某种原因,我的左键触发了 1 个 VK_LEFT 按下和 1 个 VK_RIGHT 事件(右箭头正常工作
这个问题已经有答案了: java keylistener not called (2 个回答) 已关闭 9 年前。 我遇到了 KeyListener 问题。我创建类(class)gra在构造函数中玩俄
我希望我能以正确的方式描述我的问题。我现在正在编写一个简单的双杆射击游戏,因此我实现了一个实现 KeyListener 的 KeyInputHandler 类。但是当玩家同时按下 2 个按钮时我遇到了
我是一名优秀的程序员,十分优秀!