使用 PowerShell,我想读取按键发生时的值,而无需用户按 Enter 键。例如,如果用户按“1”,我希望 PowerShell 脚本立即对选择做出反应,而无需“输入”。
我的研究发现了 ReadKey,
$input = $Host.UI.RawUI.ReadKey('IncludeKeyDown');
但 ReadKey 在字符串中返回的信息比我需要的多得多:
72,h,0,真
虽然我可以从该字符串解析按键,但我更喜欢更直接的选项。有吗?
Perhaps you could clarify a bit - but ReadKey returns a KeyInfo http://msdn.microsoft.com/en-us/library/system.management.automation.host.keyinfo_members(v=vs.85).aspx object not a string. KeyInfo
contains VirtualKeyCode, Character, ControlKeyState and KeyDown members - all fields in your output string in that order. In fact, it looks like PowerShell has just called the .ToString() method in your output example. You will probably want to look at the Character
property to find your desired character. Consider the following example where I press 1:
$key = $Host.UI.RawUI.ReadKey()
if ($key.Character -eq '1') {
"Pressed 1"
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)