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