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

mshta脚本问题:Chrome中Enter键检测失效及输入框自动聚焦需求

修复批处理调用的MSHTA输入窗口问题

针对你遇到的两个问题,以下是修改后的完整脚本,可实现自动聚焦输入框和按Enter键提交的需求:

<!-- :
::
@echo off
setlocal EnableDelayedExpansion

if "%~1" equ "/?" (
    echo Creates an input value window and output
    echo   the result to console or assign it to variable
    echo   if variable name is passed
    (echo()
    echo Usage:
    (echo()
    echo %~nx0 [storeInputTo]
)
for /f "tokens=* delims=" %%p in ('mshta.exe "%~f0"') do (
    set "input=%%p"
)

if "%~1" equ "" (
    echo "%input%"
    endlocal
) else (
    endlocal & (
        set "%~1=%input%"
    )
)
exit /b
-->

<html>
<head><title>Enter Album Name</title></head><body>
    <script language='javascript' >
        window.resizeTo(650,250);
        window.moveTo((screen.width/2)-325, (screen.height/2)-125);
        
        // 页面加载完成后自动聚焦输入框
        window.onload = function() {
            document.getElementById('input').focus();
        };
        
        function enterPressed(e){
                // 兼容不同环境的键码获取方式
                var keyCode = e.keyCode || e.which;
                if (keyCode == 13) {
                    pipePass();
                }
        }
        function pipePass() {
            var pass=document.getElementById('input').value;
            var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
            fso.Write(pass);
            close();
        }
    </script>
    <!-- 给输入框绑定按键事件 -->
    <input type="text" id="input" value="" size="50" onkeydown="enterPressed(event)">
    <hr>
    <button onclick='pipePass()'>Submit</button>
</body>
</html>

修改说明:

  • 自动聚焦输入框:添加window.onload事件,页面加载完成后调用输入框的focus()方法,打开窗口时光标直接定位在输入框内。
  • Enter键提交:
    1. 给输入框添加onkeydown="enterPressed(event)"属性,确保按键事件能被捕获;
    2. 在enterPressed函数中兼容keyCode和which,避免不同环境下的键码获取问题;
    3. 调整pipePass函数的执行顺序,先写入内容再关闭窗口,避免可能的输出异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 20:42:34