Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么可以';我不能在PowerShell中多次阅读按键吗?_Powershell - Fatal编程技术网

为什么可以';我不能在PowerShell中多次阅读按键吗?

为什么可以';我不能在PowerShell中多次阅读按键吗?,powershell,Powershell,第一次运行这个时,我能够阅读角色,但下一次运行这个时,就不那么多了 是的,我正在PowerShell窗口中运行它 不,我不负责 来自PowerShell ISE 我查看了这个函数,其中有一个相关函数名为$host.UI.RawUI.,但运行它似乎并没有改变我只能让它工作一次的事实……我还需要做些什么吗?如果在同一范围内再次运行代码,$KeyPress仍然具有上一次运行的值,如果上一次运行基于有效的按键退出了而循环,则后续运行甚至不会进入循环 因此,切换到do{…}while(…)循环: $K

第一次运行这个时,我能够阅读角色,但下一次运行这个时,就不那么多了

  • 是的,我正在PowerShell窗口中运行它
  • 不,我不负责 来自PowerShell ISE

我查看了这个函数,其中有一个相关函数名为$host.UI.RawUI.,但运行它似乎并没有改变我只能让它工作一次的事实……我还需要做些什么吗?

如果在同一范围内再次运行代码,
$KeyPress
仍然具有上一次运行的值,如果上一次运行基于有效的按键退出了
循环,则后续运行甚至不会进入循环

因此,切换到
do{…}while(…)
循环:

$KeyOption = 'Y','N'
while ($KeyOption -notcontains $KeyPress.Character) {
 $KeyPress = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
$KeyOption = 'Y','N'
do {
  $KeyPress = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
} while ($KeyOption -notcontains $KeyPress.Character)