Advertisement
Old-Lost

Swap Mouse Buttons

Mar 28th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <# 
  2.     .DESCRIPTION
  3.         Swaps between right-handed mouse buttons and left-handed mouse buttons
  4. #>
  5. param ([ValidateSet('Left', 'Right')]$Handed)
  6.  
  7. function New-BalloonTip {
  8.     <#
  9.     .SYNOPSIS
  10.     Displays a balloon tip in the lower right corner of the screen.
  11.     .DESCRIPTION
  12.     Displays a balloon tip in the lower right corner of the screen. Icon, title, and text can be customized.
  13.  
  14.     The icon displayed in the tray can be customized to be one of the standard icons, or, if the -AppIcon switch is used instead, can be the icon used by the application itself.
  15.     .EXAMPLE
  16.     New-BalloonTip -IconType info -Title 'Do not disturb' -Text "I'm busy"
  17.  
  18.     Description
  19.     -----------
  20.     Creates a balloon tip in the lower right corner.
  21.     .INPUTS
  22.     None. You cannot pipe objects to this script.
  23.     .NOTES
  24.     #Requires -Version 2.0
  25.     .OUTPUTS
  26.     None.
  27.     #>
  28.    
  29.     [CmdletBinding(DefaultParameterSetName = 'p1')]
  30.     Param (
  31.         [Parameter(Mandatory, Position = 0)][string]$Title,
  32.         [Parameter(Mandatory, Position = 1)][string]$Text,
  33.         [Parameter(ParameterSetName = 'p1')][ValidateSet('Application', 'Asterisk', 'Error', 'Exclamation', 'Hand', 'Information', 'Question', 'Shield', 'Warning', 'WinLogo')][string]$TrayIcon = 'Information',
  34.         [Parameter(ParameterSetName = 'p2')][switch]$AppIcon,
  35.         [Parameter()][ValidateSet('None', 'Info', 'Warning', 'Error')][string]$IconType = 'Info'
  36.     )
  37.    
  38.     [void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
  39.    
  40.     $Balloon = New-Object System.Windows.Forms.NotifyIcon
  41.     switch ($PSCmdlet.ParameterSetName) {
  42.         'p1'  {
  43.             $Balloon.Icon = switch ($TrayIcon) {
  44.                 'Application' { [System.Drawing.SystemIcons]::Application }
  45.                 'Asterisk'    { [System.Drawing.SystemIcons]::Asterisk }
  46.                 'Error'       { [System.Drawing.SystemIcons]::Error }
  47.                 'Exclamation' { [System.Drawing.SystemIcons]::Exclamation }
  48.                 'Hand'        { [System.Drawing.SystemIcons]::Hand }
  49.                 'Information' { [System.Drawing.SystemIcons]::Information }
  50.                 'Question'    { [System.Drawing.SystemIcons]::Question }
  51.                 'Shield'      { [System.Drawing.SystemIcons]::Shield }
  52.                 'Warning'     { [System.Drawing.SystemIcons]::Warning }
  53.                 'WinLogo'     { [System.Drawing.SystemIcons]::WinLogo }
  54.             }
  55.         }
  56.         'p2'{
  57.             $path = Get-Process -ID $PID | Select-Object -ExpandProperty Path
  58.             $Balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
  59.         }
  60.     }
  61.     $Balloon.BalloonTipIcon = $IconType
  62.     $Balloon.BalloonTipText = $Text
  63.     $Balloon.BalloonTipTitle = $Title
  64.     $Balloon.Visible = $true
  65.     $Balloon.ShowBalloonTip(10000)
  66. } # New-BalloonTip
  67.  
  68. [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
  69.  
  70. $SwapButtons = Add-Type -MemberDefinition @'
  71. [DllImport("user32.dll")]
  72. public static extern bool SwapMouseButton(bool swap);
  73. '@ -Name "NativeMethods" -Namespace "PInvoke" -PassThru
  74.  
  75. # Write-Host "Mouse buttons currently swapped?" ([System.Windows.Forms.SystemInformation]::MouseButtonsSwapped)
  76. if ($PSBoundParameters.ContainsKey('Handed')) {
  77.     if (($Handed -eq 'Left' -and (-not [System.Windows.Forms.SystemInformation]::MouseButtonsSwapped)) -or
  78.         ($Handed -eq 'Right' -and [System.Windows.Forms.SystemInformation]::MouseButtonsSwapped)) {
  79.         [bool]$returnValue = $SwapButtons::SwapMouseButton(!([System.Windows.Forms.SystemInformation]::MouseButtonsSwapped))
  80.     }
  81. } else {
  82.     [bool]$returnValue = $SwapButtons::SwapMouseButton(!([System.Windows.Forms.SystemInformation]::MouseButtonsSwapped))
  83. }
  84.  
  85. if ([Environment]::UserInteractive) {
  86.     if ([System.Windows.Forms.SystemInformation]::MouseButtonsSwapped) {
  87.         New-BalloonTip -Title 'Left Handed buttons' -Text 'Mouse buttons are now configured for left hand' -TrayIcon Information -IconType Info
  88.     } else {
  89.         New-BalloonTip -Title 'Right Handed buttons' -Text 'Mouse buttons are now configured for right hand' -TrayIcon Information -IconType Info
  90.     }
  91. } else {
  92.     Write-Output ([System.Windows.Forms.SystemInformation]::MouseButtonsSwapped)
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement