2
2
// Licensed under the MIT License.
3
3
4
4
using System ;
5
- using System . Management . Automation ;
6
5
using System . Management . Automation . Host ;
7
6
using Microsoft . Extensions . Logging ;
8
- using Microsoft . PowerShell . EditorServices . Services . PowerShell . Console ;
9
7
10
8
namespace Microsoft . PowerShell . EditorServices . Services . PowerShell . Host
11
9
{
@@ -15,7 +13,6 @@ internal class EditorServicesConsolePSHostRawUserInterface : PSHostRawUserInterf
15
13
16
14
private readonly PSHostRawUserInterface _internalRawUI ;
17
15
private readonly ILogger _logger ;
18
- private KeyInfo ? _lastKeyDown ;
19
16
20
17
#endregion
21
18
@@ -129,60 +126,7 @@ public override string WindowTitle
129
126
/// </summary>
130
127
/// <param name="options">Options for reading the current keypress.</param>
131
128
/// <returns>A KeyInfo struct with details about the current keypress.</returns>
132
- public override KeyInfo ReadKey ( ReadKeyOptions options )
133
- {
134
- bool includeUp = ( options & ReadKeyOptions . IncludeKeyUp ) != 0 ;
135
-
136
- // Key Up was requested and we have a cached key down we can return.
137
- if ( includeUp && _lastKeyDown != null )
138
- {
139
- KeyInfo info = _lastKeyDown . Value ;
140
- _lastKeyDown = null ;
141
- return new KeyInfo (
142
- info . VirtualKeyCode ,
143
- info . Character ,
144
- info . ControlKeyState ,
145
- keyDown : false ) ;
146
- }
147
-
148
- bool intercept = ( options & ReadKeyOptions . NoEcho ) != 0 ;
149
- bool includeDown = ( options & ReadKeyOptions . IncludeKeyDown ) != 0 ;
150
- if ( ! ( includeDown || includeUp ) )
151
- {
152
- throw new PSArgumentException (
153
- "Cannot read key options. To read options, set one or both of the following: IncludeKeyDown, IncludeKeyUp." ,
154
- nameof ( options ) ) ;
155
- }
156
-
157
- // Allow ControlC as input so we can emulate pipeline stop requests. We can't actually
158
- // determine if a stop is requested without using non-public API's.
159
- bool oldValue = System . Console . TreatControlCAsInput ;
160
- try
161
- {
162
- System . Console . TreatControlCAsInput = true ;
163
- ConsoleKeyInfo key = ConsoleProxy . SafeReadKey ( intercept , default ) ;
164
-
165
- if ( IsCtrlC ( key ) )
166
- {
167
- // Caller wants CtrlC as input so return it.
168
- if ( ( options & ReadKeyOptions . AllowCtrlC ) != 0 )
169
- {
170
- return ProcessKey ( key , includeDown ) ;
171
- }
172
-
173
- // Caller doesn't want CtrlC so throw a PipelineStoppedException to emulate
174
- // a real stop. This will not show an exception to a script based caller and it
175
- // will avoid having to return something like default(KeyInfo).
176
- throw new PipelineStoppedException ( ) ;
177
- }
178
-
179
- return ProcessKey ( key , includeDown ) ;
180
- }
181
- finally
182
- {
183
- System . Console . TreatControlCAsInput = oldValue ;
184
- }
185
- }
129
+ public override KeyInfo ReadKey ( ReadKeyOptions options ) => _internalRawUI . ReadKey ( options ) ;
186
130
187
131
/// <summary>
188
132
/// Flushes the current input buffer.
@@ -279,62 +223,5 @@ public override void SetBufferContents(
279
223
public override int LengthInBufferCells ( string source , int offset ) => _internalRawUI . LengthInBufferCells ( source , offset ) ;
280
224
281
225
#endregion
282
-
283
- /// <summary>
284
- /// Determines if a key press represents the input Ctrl + C.
285
- /// </summary>
286
- /// <param name="keyInfo">The key to test.</param>
287
- /// <returns>
288
- /// <see langword="true" /> if the key represents the input Ctrl + C,
289
- /// otherwise <see langword="false" />.
290
- /// </returns>
291
- private static bool IsCtrlC ( ConsoleKeyInfo keyInfo )
292
- {
293
- // In the VSCode terminal Ctrl C is processed as virtual key code "3", which
294
- // is not a named value in the ConsoleKey enum.
295
- if ( ( int ) keyInfo . Key == 3 )
296
- {
297
- return true ;
298
- }
299
-
300
- return keyInfo . Key == ConsoleKey . C && ( keyInfo . Modifiers & ConsoleModifiers . Control ) != 0 ;
301
- }
302
-
303
- /// <summary>
304
- /// Converts <see cref="ConsoleKeyInfo" /> objects to <see cref="KeyInfo" /> objects and caches
305
- /// key down events for the next key up request.
306
- /// </summary>
307
- /// <param name="key">The key to convert.</param>
308
- /// <param name="isDown">
309
- /// A value indicating whether the result should be a key down event.
310
- /// </param>
311
- /// <returns>The converted value.</returns>
312
- private KeyInfo ProcessKey ( ConsoleKeyInfo key , bool isDown )
313
- {
314
- // Translate ConsoleModifiers to ControlKeyStates
315
- ControlKeyStates states = default ;
316
- if ( ( key . Modifiers & ConsoleModifiers . Alt ) != 0 )
317
- {
318
- states |= ControlKeyStates . LeftAltPressed ;
319
- }
320
-
321
- if ( ( key . Modifiers & ConsoleModifiers . Control ) != 0 )
322
- {
323
- states |= ControlKeyStates . LeftCtrlPressed ;
324
- }
325
-
326
- if ( ( key . Modifiers & ConsoleModifiers . Shift ) != 0 )
327
- {
328
- states |= ControlKeyStates . ShiftPressed ;
329
- }
330
-
331
- KeyInfo result = new ( ( int ) key . Key , key . KeyChar , states , isDown ) ;
332
- if ( isDown )
333
- {
334
- _lastKeyDown = result ;
335
- }
336
-
337
- return result ;
338
- }
339
226
}
340
227
}
0 commit comments