我這里有一個腳本:
Clear-Host
$Word = [String]::Empty
$Mode = "INSERT MODE"
While ($True) {
$E = [Console]::ReadKey($True)
If ($E.Key -eq "Escape") {
Break
}
If ($E.Key -eq "Enter") {
Write-Host `n
$Word = "`n"
}Else {
$Word = $E.KeyChar
Write-Host $E.KeyChar -NoNewLine
}
}
Clear-Host
Return $Word
它應該讀取用戶的按鍵,然后將它們列印到螢屏上,除了退格鍵和輸入鍵之外,它可以正常作業。Enter 列印兩個換行符,退格鍵將指標向后移動并像這樣覆寫字符:
Hello worle_ (Spelling Error)
Hello worle? (Pressed backspace)
Hello world_ (Just overwrote the letter as if it wasn't there)
如何輸入以列印換行符并正確使用退格鍵?我使用的是 powershell 5.1 并使用 Windows 10。
uj5u.com熱心網友回復:
我建議利用 .NET 程式集“System.Windows.Forms”。
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("Hello")
[System.Windows.Forms.SendKeys]::SendWait("{TAB}")
[System.Windows.Forms.SendKeys]::SendWait("World")
[System.Windows.Forms.SendKeys]::SendWait("{Enter}")
您可以在此處找到其他特殊字符。
uj5u.com熱心網友回復:
請嘗試以下操作:
$word = ''
$mode = 'INSERT MODE'
$done = $false
While (-not $done) {
$e = [Console]::ReadKey($true)
switch ($e.Key) {
'Escape' { $done = $true; break }
'Enter' { $word = "`n"; Write-Host; break }
'Backspace' {
if ($word.Length) { $word = $word.Substring(0, $word.Length-1) }
Write-Host -NoNewLine "`b `b"
break
}
default { $word = $e.KeyChar; Write-Host -NoNewLine $e.KeyChar }
}
}
Write-Host
# Output the captured input.
"[$word]"
要僅發出一個換行符,只需使用
Write-Hostwithout arguments。要模擬互動式退格行為,發出一個退格,然后是一個空格,然后是另一個退格。
- 默認情況下,向控制臺發送退格字符是非破壞性的:也就是說,游標向后移動一個位置,但不會擦除前一個游標位置的字符。
- 上述技術通過將其替換為空格,使其看起來好像字符已被擦除。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/342861.html
上一篇:VisualStudio2019中的問題組裝,簡單的“HelloWorld”legacy_stdio_definitions.lib
