You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现自动鼠标点击?含Java语音识别项目语音触发场景

Hey there! Let's tackle your two tech questions one by one—first, setting up automatic mouse clicks without manual input, then tying that functionality to voice commands in your Java project.

1. Implementing Automatic Mouse Clicks (No Manual Input)

The most straightforward cross-platform way (especially for your Java project) is using the built-in java.awt.Robot class. It lets you simulate mouse movements and clicks directly through Java code.

Example: Basic Auto-Clicker

import java.awt.Robot;
import java.awt.event.InputEvent;

public class AutoClicker {
    public static void main(String[] args) throws Exception {
        Robot robot = new Robot();
        
        // Optional: Move mouse to a specific screen coordinate (x=500, y=500)
        robot.mouseMove(500, 500);
        
        // Simulate a left mouse click (press + release)
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        
        // For repeated clicks (e.g., click every 2 seconds, 5 times total)
        for (int i = 0; i < 5; i++) {
            Thread.sleep(2000);
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        }
    }
}

Notes:

  • The Robot class requires AWT permissions—some IDEs or Java versions may need you to add VM arguments like --add-opens java.desktop/java.awt=ALL-UNNAMED (for Java 9+).
  • Screen coordinates start at (0,0) in the top-left corner of your display.

Non-Java Alternatives (For Quick Scripts)

If you don’t need to integrate this into Java, you can use:

  • AutoHotkey (Windows): Simple scripts to automate clicks (e.g., Click 500,500).
  • PyAutoGUI (Python): Cross-platform library with pyautogui.click() to trigger clicks.
2. Voice-Triggered Mouse Clicks in Your Java Project

To make this work, you’ll need two core components: a speech recognition engine to listen for the "点击" command, and the Robot class to trigger the click when the command is detected. Let’s break this down step by step.

Step 1: Integrate a Speech Recognition Library

For offline, local recognition (no internet required), use CMU Sphinx (a popular open-source speech recognition library). For better accuracy (but requiring internet), you could use cloud services like Google Cloud Speech-to-Text, but we’ll focus on CMU Sphinx here.

Add Maven Dependencies

If you’re using Maven, add these to your pom.xml:

<dependency>
    <groupId>edu.cmu.sphinx</groupId>
    <artifactId>sphinx4-core</artifactId>
    <version>5.0</version>
</dependency>
<dependency>
    <groupId>edu.cmu.sphinx</groupId>
    <artifactId>sphinx4-data</artifactId>
    <version>5.0</version>
</dependency>

Step 2: Configure Keyword Spotting for "点击"

First, create a grammar file named clickGrammar.jsgf in your src/main/resources folder to tell Sphinx to only listen for the "点击" command:

grammar clickGrammar;
public <command> = 点击;

Full Voice-Controlled Clicker Code

import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.LiveSpeechRecognizer;
import edu.cmu.sphinx.api.SpeechResult;
import java.awt.Robot;
import java.awt.event.InputEvent;

public class VoiceClicker {
    public static void main(String[] args) throws Exception {
        // Configure speech recognition
        Configuration config = new Configuration();
        // Replace with path to a Chinese acoustic model (download from CMU Sphinx's repo)
        config.setAcousticModelPath("resource:/edu/cmu/sphinx/models/zh-cn/zh-cn");
        config.setDictionaryPath("resource:/edu/cmu/sphinx/models/zh-cn/zh-cn.dict");
        config.setGrammarPath("src/main/resources/");
        config.setGrammarName("clickGrammar");
        config.setUseGrammar(true);

        // Start live recognition
        LiveSpeechRecognizer recognizer = new LiveSpeechRecognizer(config);
        recognizer.startRecognition(true);

        Robot robot = new Robot();
        System.out.println("Listening for '点击' command...");

        // Continuously listen for commands
        while (true) {
            SpeechResult result = recognizer.getResult();
            if (result != null) {
                String heardCommand = result.getHypothesis();
                System.out.println("Heard: " + heardCommand);
                
                if ("点击".equalsIgnoreCase(heardCommand)) {
                    // Trigger mouse click at current cursor position
                    robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                    robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                    System.out.println("Mouse clicked!");
                }
            }
        }
    }
}

Notes:

  • You’ll need to download a pre-trained Chinese acoustic model for CMU Sphinx (find it in their official repository) and update the acousticModelPath to match your file location.
  • Ensure your Java application has microphone access—some operating systems will prompt for this permission.
  • For better accuracy, consider tuning the grammar file or switching to a cloud-based speech API (like Google Cloud) which handles Chinese dialects and background noise better.

内容的提问来源于stack exchange,提问作者ritu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:41:07