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键提交:
- 给输入框添加
onkeydown="enterPressed(event)"属性,确保按键事件能被捕获; - 在
enterPressed函数中兼容keyCode和which,避免不同环境下的键码获取问题; - 调整
pipePass函数的执行顺序,先写入内容再关闭窗口,避免可能的输出异常。
- 给输入框添加
内容的提问来源于stack exchange,提问作者Robin
相关产品推荐
相关产品推荐

