Monday, September 29, 2014

Touch-screen Keyboard Script

Touch Screen Keyboard Script


This is a script that I wrote for a friend of mine. The script enables or disables the touch-screen keyboard for Windows 7, 8, and 8.1. To download the script, please click on the link below:

BJCairneys_Touchscreen_Settings


The Problem: A friend of mine was just starting out in college this year, and she purchased a touch screen laptop that can separate into a tablet. By default, the system has the built in touch screen keyboard enabled. If a user is using the system as a laptop, every time the screen is touched in an area that requires input, the touch screen keyboard will pop up. This can occupy much of the screen space and become annoying if the user wishes to use the actual keyboard of the laptop. At first, I considered disabling the touch screen keyboard all together. This would become an issue however, when the user wished to separate the keyboard from the screen, and use the system as a tablet. There would be no keyboard at all.

The Solution: I wrote a script and created a desktop icon to go with it. The function of the script is to provide a menu for the user with 3 simple choices. Enable or disable the touch screen keyboard, and exit the script. The icon sits on the desktop, and the user can double click on it to start the script.

The Method: For our second major assignment for WIN213 (PowerShell Scripting) We were required to do something similar, but the script was to enable and disable DHCP on a network adapter, and to assign static values in the case that DHCP was disabled. 

I used arrays to store the title, options and box characters for the menu. I used sub expressions and simple math to draw the menu box. For example, I would draw the top box character twice as many times as there were characters in the array position that had the longest string stored in it.

To produce the white space between the box edges and the strings, I called half as many white spaces as there were in the longest array position. This gave me a clean pair of boxes around both the title of the menu, and the options.

I used a main function to clear the screen and set the background and foreground colours and to call the draw-menu function which took care of the menu box. 

I used a switch statement and $host.ui.rawui.readkey("no echo, include key down") to get the effect of simply pressing a key to proceed with a choice. This will not work with PowerShell ISE, so the script must be run from the console.

The last line calls the main function.

The script is below:


<########################################################################################

#                                                                                       
# File Name: TouchScreenKeyBoard.ps1                  Author: Brian Cairney                
# Created: August 21, 2014                            Last Modified: N/A                 
# Purpose: To enable and disable the windows 8 touchscreen keyboard                     
# Notes: Powershell must be run as an administrator to use this script.                 
#              Also, an icon for the desktop is optional for convenience                      
# *Important*: This script is not compatible with Powershell ISE as it uses             
#                     the $Host.UI.RawUI.ReadKey() Method for menu selection.                  
#                                                                                       
#########################################################################################
#>


#########################################################################################

# Main Function to set colours, and draw the menu for user to select a choice           
#########################################################################################

Function Main{



#Save old background and foreground colours

$OldBackgroundColour = $Host.ui.RawUI.BackgroundColor
$OldForegroundColour = $Host.UI.RawUI.ForegroundColor



#Set new foreground and background colours

$Host.UI.RawUI.BackgroundColor = "Black"
$Host.UI.RawUI.ForegroundColor = "Green"
Clear-Host


#Call the Draw-Menu function for user selection

Draw-Menu


} #End of function: Main



###############################################################################################

# Draw-Menu Function that will draw a menu and initiate a switch statement for user selection 
###############################################################################################


Function Draw-Menu{



#Create Arrays and string variable to use for menu title, menu options, and graphics

$MenuCharacters = @("╔","═","╗","║","╚","╝","╠","╣"," ")
$MenuTitle = @("Windows Touch Keyboard Settings"," Created By Brian Cairney")
$MenuOptions = @("[1] Turn the touch keyboard on","[2] Turn the touch keyboard off","[3] Exit to desktop")


#Draw the top of the menu 

Write-Output "$($Menucharacters[0])$($MenuCharacters[1] * ($Menutitle[0].length * 2))$($MenuCharacters[2])"
Write-Output "$($MenuCharacters[3])$($MenuCharacters[8] * ($Menutitle[0].length * 2))$($MenuCharacters[3])"
Write-Output"$($MenuCharacters[3])$($MenuCharacters[8] * ($MenuTitle[0].length / 2))$($MenuTitle[0])$($MenuCharacters[8] * ($MenuTitle[0].Length                         / 2.1))$($MenuCharacters[3])"
Write-Output "$($MenuCharacters[3])$($MenuCharacters[8] * ($Menutitle[0].length * 2))$($MenuCharacters[3])"
Write-Output "$($MenuCharacters[3])$($MenuCharacters[8] * ($MenuTitle[0].length / 1.6))$($MenuTitle[1])$($MenuCharacters[8] *                                                          ($MenuTitle[0].Length / 1.7))$($MenuCharacters[3])"
Write-Output "$($MenuCharacters[3])$($MenuCharacters[8] * ($Menutitle[0].length * 2))$($MenuCharacters[3])"
Write-Output "$($MenuCharacters[6])$($MenuCharacters[1] * ($MenuTitle[0].Length * 2))$($MenuCharacters[7])" 


#Draw the bottom half of the menu and display options

Write-Output "$($MenuCharacters[3])$($MenuCharacters[8] * ($Menutitle[0].length * 2))$($MenuCharacters[3])"
Write-Output "$($MenuCharacters[3])$($MenuCharacters[8] * ($MenuTitle[0].Length / 2))$($MenuOptions[0])$($MenuCharacters[8] *                                                      ($MenuTitle[0].Length / 2))$($MenuCharacters[3])"
Write-Output "$($MenuCharacters[3])$($MenuCharacters[8] * ($Menutitle[0].length * 2))$($MenuCharacters[3])"
Write-Output "$($MenuCharacters[3])$($MenuCharacters[8] * ($MenuTitle[0].Length / 2))$($MenuOptions[1])$($MenuCharacters[8] *                                                      ($MenuTitle[0].Length / 2.1))$($MenuCharacters[3])"
Write-Output "$($MenuCharacters[3])$($MenuCharacters[8] * ($Menutitle[0].length * 2))$($MenuCharacters[3])"
Write-Output "$($MenuCharacters[3])$($MenuCharacters[8] * ($MenuTitle[0].Length / 2))$($MenuOptions[2])$($MenuCharacters[8] *                                                     11)$($MenuCharacters[8] * ($MenuTitle[0].Length / 2))$($MenuCharacters[3])"
Write-Output "$($MenuCharacters[3])$($MenuCharacters[8] * ($Menutitle[0].length * 2))$($MenuCharacters[3])"
Write-Output "$($Menucharacters[4])$($MenuCharacters[1] * ($Menutitle[0].length * 2))$($MenuCharacters[5])"


#Switch statement for user selection

$UserInput = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Switch -Wildcard ($UserInput){

        "49,1,*" {Start-keyboard;Clear-Host;Write-Host "The Touch Keyboard has been enabled, Enjoy!";Start-Sleep -Seconds 3;Clear-Host;Draw-Menu}
        "50,2,*" {Stop-Keyboard;Clear-Host;Write-Host "The Touch Keyboard has been disabled, Enjoy!";Start-Sleep -Seconds 3;Clear-Host;Draw-Menu}
         "51,3,*" {Clear-Host;Write-Host "Exiting";$Host.UI.RawUI.BackgroundColor $OldBackgroundColour;$Host.UI.RawUI.ForegroundColor =
                       $OldForegroundColour;Start-Sleep -Seconds 3;Clear-Host;exit}
        Default  {Clear-Host;Draw-Menu}


    }#End of Switch statement



}#End of function: Draw-Menu



#Function to enable the touch keyboard

Function Start-Keyboard{


Set-Service -StartupType Automatic TabletInputService

$Keyboard = Get-Service TabletInputService 
$Keyboard.Start()


}#End of Function: Enable-Keyboard



#Function to disable the touch keyboard

Function Stop-Keyboard{


Set-Service -StartupType Disabled TabletInputService

$Keyboard = Get-Service TabletInputService 
$Keyboard.Stop()


}#End of function: Stop-Keyboard



. main





No comments:

Post a Comment