Font size: +

A Simple On Screen Keyboard In PowerShell

simon_5_PSUGH1_4_20181009-074058_1

Already in April I programmed my PSConftris in PowerShell. This is available in the PowerShell gallery and can be used withAlready in April I programmed my PSConftris in PowerShell. This is available in the PowerShell gallery and can be used with

Install-Modules PSConftris -Scope CurrentUser

And started with:

New-PSConftris

PSConftris

PSConftris is particularly interesting because it includes many functions for real-time processing in PowerShell and set the screen position. I plan to write my own blog article about the architecture at some time.

Now I have built a vending machine for our PowerShell Saturday of the #PSUGH in Hannover. Basis is a Raspberry Pi3 with the PowerShell Core. Among other things an On Screen Keyboard is necessary for the score input with the joystick. This function may also be useful for other projects. Therefore I would like to share it with the community in this blog.

PSConftrisArcade

Here is a screenshot for use

PowerShell OnScreenKeyboard

And here is the script for it

  <#
      .SYNOPSIS
      Set cursor posion in the PowerShell console

      .DESCRIPTION
      Set cursor posion in the PowerShell console

      .PARAMETER x
      parameter -x for the x position

      .PARAMETER y
      parameter -y for the < position

      .EXAMPLE
      set-ConsolePosition -x Value -y Value
  #>


  [CmdletBinding()]
  Param(
    $maxStringSize = 20,
    $minChars=3
  )

  function set-ConsolePosition {
    <#
        .SYNOPSIS
        Set cursor posion in the PowerShell console

        .DESCRIPTION
        Set cursor posion in the PowerShell console

        .PARAMETER x
        parameter -x for the x position

        .PARAMETER y
        parameter -y for the < position

        .EXAMPLE
        set-ConsolePosition -x Value -y Value
    #>


    Param(
      [Parameter(Mandatory)]
      [int]$x,
      [Parameter(Mandatory)]
      [int]$y
    )
    $position=$host.ui.rawui.cursorposition 
    $position.x=$x 
    $position.y=$y 
    $host.ui.rawui.cursorposition = $position 

  }



  function Get-PowerShellOnScreenKeyboard{
    <#
        .SYNOPSIS
        Create a onScreen ASCII Keyboard

        .DESCRIPTION
        Create a onScreen ASCII Keyboard

        .PARAMETER xsxale
        -xsxale Chars x (11)

        .PARAMETER yscale
        -yscale chars y (3)

        .PARAMETER maxStringSize
        -maxStringSize max size of the output String

        .PARAMETER minChars
        -minChars min size of the output  String

        .EXAMPLE
        Get-PowerShellOnScreenKeyboard 
        

        .LINK
        URLs to related sites
        www.software-virtualisierung.de


        .OUTPUTS
        a String
    #>




    param(
      $xsxale = 11,
      $yscale = 3,
      $maxStringSize = 20,
      $minChars=3
    )
  
    clear

    $keytable = @('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','Ö','Ä','Ü','_','.')

    #Paint Keyboards
    $i =0;
    for($y=0; $y -lt($keytable.Count / 10);$y++){
      for($x=0; $x -lt 11;$x++){
        Write-Host $($keytable[$i] + " ") -NoNewline
        if($i -lt $keytable.count){
          $i++
        } 
      }
      Write-Host
      Write-Host
    }

    set-ConsolePosition -x 0 -y 7
    Write-Host "Entry :"
    Write-Host 
    Write-Host "Top left button = delete (backspace key)"
    Write-Host "Bottom left button = insert (space key)"
    Write-Host "Top middle button = Enter (enter key)"
    Write-Host "Min input size = $minChars and max size = $maxStringSize"

    $x=0
    $y=0
    [string] $InputString = ""

    set-ConsolePosition -x ($x*2) -y ($y*2+1)
    Write-Host '^'
    set-ConsolePosition -x 0 -y 15

    $AllDone = $false
    while(!($AllDone)){

      if( ([Console]::KeyAvailable -and ([ConsoleKeyInfo] $key = [Console]::ReadKey())) ) {
      
        #Clear Buffer
        while ([Console]::KeyAvailable){
          $key = [Console]::ReadKey() 
        }
        $oldx=$x
        $oldy=$y
    
        #Write-Host $key.key

        switch ($key.key)
        {
          'LeftArrow' { 
            if($x -gt 0) {$x--}
          
            
          } 
          'RightArrow'{ 
            if($x -lt ($xsxale-1)) {
              if(($y)*$xsxale + $x +1 -lt $keytable.Count){
                $x++
              }
            }

          } 
          'UpArrow'    { 
            if($y -gt 0) {$y--}
          }
          'DownArrow' { 
            if($y -lt ($yscale-1)) {
              if(($y+1)*$xsxale + $x -lt $keytable.Count){
          
                $y++
              }
            }
                   
          }
          'Enter' {
            if(($InputString.Length -ge $minChars) -and ($InputString.Length -le $maxStringSize)){
              $AllDone = $true
            } else {
              #Write-Host "Min input size = $minChars and max size = $maxStringSize"
            }

          }
          'Spacebar' {
            if($InputString.Length -lt $maxStringSize){
            
              $InputString += [char] $keytable[$y*$xsxale + $x]
            }

          }
      
          'Backspace' {
            if($InputString.Length -ge 1) {
              $InputString =  $InputString.Substring(0,$InputString.Length-1)
            }
          }
      }
        
      #if(($oldx -ne $x) -or ($oldy -ne $y)){
      set-ConsolePosition -x ($oldx*2) -y ($oldy*2+1)
      Write-Host ' '
      set-ConsolePosition -x ($x*2) -y ($y*2+1)
      Write-Host '^'
      
      set-ConsolePosition -x 0 -y 7
      Write-Host $("Entry :" + $InputString+"                                  ")
      
       
      #}
      set-ConsolePosition -x 0 -y 15
      Start-Sleep -Milliseconds 30
    
    }
  }

  return $InputString
    
}   


Get-PowerShellOnScreenKeyboard -maxStringSize $maxStringSize -minChars $minChars

 

 

 

 

All App-V Events And Messages
When Local Applications Are Started By App-V
 

Comments

No comments made yet. Be the first to submit a comment
Already Registered? Login Here
Friday, March 29, 2024

Captcha Image

@nickinformation Tweets

My german Blog: 

http://www.software-virtualisierung.de

in 

We use cookies on our website. Some of them are essential for the operation of the site, while others help us to improve this site and the user experience (tracking cookies). You can decide for yourself whether you want to allow cookies or not. Please note that if you reject them, you may not be able to use all the functionalities of the site.