David Jones
David Jones

Reputation: 582

Problem in Powershell not consuming ReadKey

When using the following LOC in a custom Powershell function that essentially was a "Wait until any key is pressed" called from a script:

[System.Console]::ReadKey($true)

… later on, particularly when the function returned or completed its context I got:

KeyChar   Key Modifiers
-------   --- ---------
... Enter         0

Q: How to avoid this??

Upvotes: 1

Views: 543

Answers (1)

David Jones
David Jones

Reputation: 582

I found the solution was that you need to consume the ReadKey call. i.e.:

  $keypress = [System.Console]::ReadKey($true)

I found the same with some other PowerShell calls, that did an action but returned a value, such as a count, that the returned value was eventually displayed. Yes I got unused warnings but I ignored them.

Hope this helps someone. It annoyed me for several days off an on until I realised this.

Upvotes: 1

Related Questions