/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" Series about Monad on Computerworld "," Watching FSRM Quota and filescreen events from Monad "," More ACL and MSH on MSH for Fun "," Jelle "," Monad and "Jeugd sentiment" "," Monad Beta 3 Docs changes "," Get SpecialFolder Locations in Monad "," Filling Emty Properties in AD from MSH "," First Monad B3 impression and please help the scri... "," Monad Beta 3 is released ! "," ")

Friday, January 20, 2006

 


MSH Scroll-host function



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 :

PS. 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.


Comments:
Anonymous Anonymous
you can reduce the keystroke input logic to the following:

$key = $host.ui.rawui.ReadKey("NoEcho,IncludeKeyDown").VirtualKeyCode

There's an overload to readkey that allows you to not echo the key stroke! Woo hoo!
 
Blogger /\/\o\/\/
Woo Hoo, Indeed ;-)

I realy did not like the buffer trick but missed that overload.

thanks for the Comment, realy helpfull !

gr /\/\o\/\/
 
Post a Comment



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?