| 1 |
# |
| 2 |
# PowerShell script for controlling the console QuickEdit and InsertMode flags. |
| 3 |
# |
| 4 |
# Turn QuickEdit off to interact with mouse-driven console programs. |
| 5 |
# |
| 6 |
# Usage: |
| 7 |
# |
| 8 |
# powershell .\ConinMode.ps1 [Options] |
| 9 |
# |
| 10 |
# Options: |
| 11 |
# -QuickEdit [on/off] |
| 12 |
# -InsertMode [on/off] |
| 13 |
# -Mode [integer] |
| 14 |
# |
| 15 |
|
| 16 |
param ( |
| 17 |
[ValidateSet("on", "off")][string] $QuickEdit, |
| 18 |
[ValidateSet("on", "off")][string] $InsertMode, |
| 19 |
[int] $Mode |
| 20 |
) |
| 21 |
|
| 22 |
$signature = @' |
| 23 |
[DllImport("kernel32.dll", SetLastError = true)] |
| 24 |
public static extern IntPtr GetStdHandle(int nStdHandle); |
| 25 |
|
| 26 |
[DllImport("kernel32.dll", SetLastError = true)] |
| 27 |
public static extern uint GetConsoleMode( |
| 28 |
IntPtr hConsoleHandle, |
| 29 |
out uint lpMode); |
| 30 |
|
| 31 |
[DllImport("kernel32.dll", SetLastError = true)] |
| 32 |
public static extern uint SetConsoleMode( |
| 33 |
IntPtr hConsoleHandle, |
| 34 |
uint dwMode); |
| 35 |
|
| 36 |
public const int STD_INPUT_HANDLE = -10; |
| 37 |
public const int ENABLE_INSERT_MODE = 0x0020; |
| 38 |
public const int ENABLE_QUICK_EDIT_MODE = 0x0040; |
| 39 |
public const int ENABLE_EXTENDED_FLAGS = 0x0080; |
| 40 |
'@ |
| 41 |
|
| 42 |
$WinAPI = Add-Type -MemberDefinition $signature ` |
| 43 |
-Name WinAPI -Namespace ConinModeScript ` |
| 44 |
-PassThru |
| 45 |
|
| 46 |
function GetConIn { |
| 47 |
$ret = $WinAPI::GetStdHandle($WinAPI::STD_INPUT_HANDLE) |
| 48 |
if ($ret -eq -1) { |
| 49 |
throw "error: cannot get stdin" |
| 50 |
} |
| 51 |
return $ret |
| 52 |
} |
| 53 |
|
| 54 |
function GetConsoleMode { |
| 55 |
$conin = GetConIn |
| 56 |
$mode = 0 |
| 57 |
$ret = $WinAPI::GetConsoleMode($conin, [ref]$mode) |
| 58 |
if ($ret -eq 0) { |
| 59 |
throw "GetConsoleMode failed (is stdin a console?)" |
| 60 |
} |
| 61 |
return $mode |
| 62 |
} |
| 63 |
|
| 64 |
function SetConsoleMode($mode) { |
| 65 |
$conin = GetConIn |
| 66 |
$ret = $WinAPI::SetConsoleMode($conin, $mode) |
| 67 |
if ($ret -eq 0) { |
| 68 |
throw "SetConsoleMode failed (is stdin a console?)" |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
$oldMode = GetConsoleMode |
| 73 |
$newMode = $oldMode |
| 74 |
$doingSomething = $false |
| 75 |
|
| 76 |
if ($PSBoundParameters.ContainsKey("Mode")) { |
| 77 |
$newMode = $Mode |
| 78 |
$doingSomething = $true |
| 79 |
} |
| 80 |
|
| 81 |
if ($QuickEdit + $InsertMode -ne "") { |
| 82 |
if (!($newMode -band $WinAPI::ENABLE_EXTENDED_FLAGS)) { |
| 83 |
# We can't enable an extended flag without overwriting the existing |
| 84 |
# QuickEdit/InsertMode flags. AFAICT, there is no way to query their |
| 85 |
# existing values, so at least we can choose sensible defaults. |
| 86 |
$newMode = $newMode -bor $WinAPI::ENABLE_EXTENDED_FLAGS |
| 87 |
$newMode = $newMode -bor $WinAPI::ENABLE_QUICK_EDIT_MODE |
| 88 |
$newMode = $newMode -bor $WinAPI::ENABLE_INSERT_MODE |
| 89 |
$doingSomething = $true |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if ($QuickEdit -eq "on") { |
| 94 |
$newMode = $newMode -bor $WinAPI::ENABLE_QUICK_EDIT_MODE |
| 95 |
$doingSomething = $true |
| 96 |
} elseif ($QuickEdit -eq "off") { |
| 97 |
$newMode = $newMode -band (-bnot $WinAPI::ENABLE_QUICK_EDIT_MODE) |
| 98 |
$doingSomething = $true |
| 99 |
} |
| 100 |
|
| 101 |
if ($InsertMode -eq "on") { |
| 102 |
$newMode = $newMode -bor $WinAPI::ENABLE_INSERT_MODE |
| 103 |
$doingSomething = $true |
| 104 |
} elseif ($InsertMode -eq "off") { |
| 105 |
$newMode = $newMode -band (-bnot $WinAPI::ENABLE_INSERT_MODE) |
| 106 |
$doingSomething = $true |
| 107 |
} |
| 108 |
|
| 109 |
if ($doingSomething) { |
| 110 |
echo "old mode: $oldMode" |
| 111 |
SetConsoleMode $newMode |
| 112 |
$newMode = GetConsoleMode |
| 113 |
echo "new mode: $newMode" |
| 114 |
} else { |
| 115 |
echo "mode: $oldMode" |
| 116 |
} |