有人能弄清楚为什么这在 Windows 10 中不起作用吗?它在 Windows 11 中运行良好

问题描述 投票:0回答:1

我刚刚在这里找到了一些代码。我喜欢显示(UI)的想法,并一直使用它为我的组织的日常运营制作一些编译的修复程序。

完整代码如下:

@if (@CodeSection == @Batch) @then

@echo off

setlocal EnableDelayedExpansion
if "%~1" equ "newCmd" goto newCmd

rem Multi-line menu with options selection via DOSKEY
rem Antonio Perez Ayala
rem Set title for AppActivate
title Select option
rem Define the options
set numOpts=0

for %%a in ("Calculator=calc" "Notepad=notepad" "Paint=mspaint" "Explorer=explorer") do (
   for /F "tokens=1,2 delims==" %%b in (%%a) do (
      set /A numOpts+=1
      set "option[!numOpts!]=%%b"
      set "app[%%b]=%%c"
   )
)

set /A numOpts+=1
set "option[!numOpts!]=Quit"
rem Options selection loop
:nextOpt

cmd /C "%~F0" newCmd
if errorlevel 1 goto nextOpt

goto :EOF

:newCmd

rem Clear previous doskey history
doskey /REINSTALL
rem Fill doskey history with menu options
cscript //nologo /E:JScript "%~F0" %numOpts%

for /L %%i in (1,1,%numOpts%) do set /P "="

cls

echo MULTI-LINE MENU WITH OPTIONS SELECTION

echo/
rem Send a F7 key to open the selection menu
cscript //nologo /E:JScript "%~F0"
rem Read the option
set /P "option=Select the desired option: "

echo/

set "continue=1"
if defined app[%option%] (
   start !app[%option%]!
) else (
   set "continue=0"
)

exit %continue%

@end

var wshShell = WScript.CreateObject("WScript.Shell"),
    envVar = wshShell.Environment("Process");

// Select the cmd.exe window
wshShell.AppActivate("Select option");

WScript.Sleep(300);

if ( WScript.Arguments.Length ) {
   // Send menu options to fill the menu
   var numOpts = parseInt(WScript.Arguments(0));
   for ( var i=1; i <= numOpts; i++ ) {
      wshShell.SendKeys(envVar("option["+i+"]")+"{ENTER}");
   }
} else {
   // Send a F7 to open the menu
   wshShell.SendKeys("{F7}");
}
batch-file sendkeys jscript
1个回答
0
投票

我在 Windows 10 上尝试过,有时有效,有时无效,但是:

如果您手动按 F7,菜单总是会出现。所以我将按键次数增加了一倍/三倍以提高成功率:

...
else {
   // Send a F7 to open the menu
   wshShell.SendKeys("{F7}");
   wshShell.SendKeys("{F7}");
   wshShell.SendKeys("{F7}");
}

可能不是理想的解决方案,但它确实有效。

© www.soinside.com 2019 - 2024. All rights reserved.