This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
The only way to scroll by keyboard (without a mouse) in the command console, hence in MSH Is by using the following combination of key's
Alt-Space, E, L
this is not very handy
after I saw a post about this in the NG, I made this function scroll-host (alias S), to simplify it a bit, after you run this script
S [enter]is enough to get into scrolling mode , Space will stop the scollingmode and jump back to the last line.
# Scroll-Host.msh
# creates Scroll-Host Function
# /\/\o\/\/ 2006
# http://mow001.blogspot.com
Function Scroll-host {
$Oldpos = $host.ui.rawui.CursorPosition
while ($true) {
$script:pos = $host.ui.rawui.CursorPosition
$key = $host.ui.rawui.ReadKey("NoEcho,IncludeKeyDown").VirtualKeyCode
# Quit on Space Enter or Escape
if ($key -eq 32 -or $key -eq 13 -or $key -eq 27) {
$host.ui.rawui.CursorPosition = $oldpos;break}
# Handle Cursor
switch ($key) {
38 {$script:pos.y -=1}
40 {$script:pos.y +=1}
33 {$script:pos.y -= ($host.ui.rawui.windowsize.height -1)}
34 {$script:pos.y += ($host.ui.rawui.windowsize.height -1)}
}
if ($script:pos.y -lt 0) {$script:pos.y = 0}
if ($script:pos.x -lt 0) {$script:pos.x = 0}
if ($script:pos.y -gt ($host.ui.rawui.buffersize.Height - 1)) {
$script:pos.y = ($host.ui.rawui.buffersize.Height - 1)}
$host.ui.rawui.CursorPosition = $script:pos
}
}
set-alias s scroll-host
the trick part was that I could not get rid of the Key press echoed to the screen, so I remove it after, by buffering the character that was there before.
*edit* ,
I got a helpfull comment from "Monad Jim", so I could remove the "ugly" character removing work-around : # Read the key and remove the output of this
$TempPos = $host.ui.rawui.CursorPosition
$re = new-object $rect $TempPos.x,$TempPos.y,1,$TempPos.y
$buffer = $host.ui.rawui.getbuffercontents($re)
$key = $host.ui.rawui.ReadKey().VirtualKeyCode
$host.ui.rawui.SetBufferContents($TempPos,$buffer)
enjoy,
gr /\/\o\/\/
Tags : Monad mshPS. Yes, I could have used a switch statement for the keycheck, also you could add enter to quit also as in the original, it's that lazy thing again ;-).
*Edit* as I removed the echo comment I did those changes also.