Skip to content

Ctrl-C doesn't work if PowerShell was started with Russian keyboard layout #1393

Closed

Description

Environment

PS version: 7.0.0
PSReadline version: 2.0.0
os: 10.0.18362.1 (WinBuild.160101.0800)
PS file version: 7.0.0.0
HostName: ConsoleHost
BufferWidth: 170
BufferHeight: 32766

Steps to reproduce

  1. Enable Russian keyboard layout on Windows (I use Alt-Shift to do that)
  2. Start PowerShell Core 7.0 (which includes PSReadLine 2.0)
  3. Press Ctrl-C in the shell

Expected behavior

Ctrl-C should terminate current command/input/whatever, it should work as Ctrl-C usually do.

Actual behavior

Russian letter "с" getting inserted into the command line, and no actual Ctrl-C behavior happens.

Ctrl-C is still broken even if I switch to the English keyboard layout (then English letter "c" gets inserted inctead of actual Ctrl-C behavior heppening).

Activity

  1. ghost added
    Needs-Triage 🔍It's a new issue that core contributor team needs to triage.
    on Mar 7, 2020
  2. iSazonov commented on Mar 7, 2020

    @iSazonov

    Friedrich von Never (@ForNeVeR) Do you see the issue without PSReadline? If no please move the issue to PSReadline repo.

  3. ForNeVeR commented on Mar 8, 2020

    @ForNeVeR
    ContributorAuthor

    Ilya (@iSazonov), no, I cannot reproduce the issue without PSReadLine.

    Isn't this repository already a PSReadLine repo? I can see that some repository movement has been involved, but this is definitely a repo I have been reporting PSReadLine issues earlier.

    Please direct me to a right repo if this isn't right. I've tried searching for other PSReadLine repo but wasn't successful, sorry.

  4. iSazonov commented on Mar 8, 2020

    @iSazonov

    Isn't this repository already a PSReadLine repo?

    Oh, sorry.

  5. daxian-dbw commented on Mar 18, 2020

    @daxian-dbw
    Member

    Ctrl-C is still broken even if I switch to the English keyboard layout (then English letter "c" gets inserted inctead of actual Ctrl-C behavior heppening).

    Friedrich von Never (@ForNeVeR) Thanks for the reporting. Just want some clarification on this behavior, was it broken for you when using the English keyboard from the very beginning?

  6. added
    Issue-BugIt either shouldn't be doing this or needs an investigation.
    and removed
    Needs-Triage 🔍It's a new issue that core contributor team needs to triage.
    on Mar 18, 2020
  7. ForNeVeR commented on Mar 19, 2020

    @ForNeVeR
    ContributorAuthor

    Ctrl-C works fine if I start PowerShell session when English keyboard layout is active. It is only broken if I start the session with Russian layout.

  8. daxian-dbw commented on Apr 2, 2020

    @daxian-dbw
    Member

    A duplicate of this issue is reported in #1442 with a different repro -- ctrl+r results in the Russian letter к.

  9. ForNeVeR commented on Apr 2, 2020

    @ForNeVeR
    ContributorAuthor

    Yes, actually all of the Ctrl-hotkeys are broken.

  10. ForNeVeR commented on Aug 23, 2020

    @ForNeVeR
    ContributorAuthor

    Exact steps to reproduce:

    1. Start PSReadline (e.g. by starting PSReadline-net461 from sources).
    2. Enable Russian keyboard layout, and then press Ctrl-C or Ctrl-R one time (it won't work).
    3. After that, you may change the current keyboard layout as many times as you want, Ctrl-hotkeys won't work for you in this session. The session is absolutely broken.

    Also, I've been able to reproduce the issue both in PowerShell Core and in PowerShell 5.1.

    I was able to determine that the culprit is likely Microsoft.PowerShell.PSKeyInfo.KeyInfoAsString: this method returns a string "Ctrl+к" even when I press Ctrl-r with English keyboard layout enabled (and the input argument properly indicates Ctrl+r as being pressed). Most likely, there're two different issues:

    1. Something in PSKeyInfo is static and wrongly so, so it remembers my (Russian) keyboard layout when I enter this method for the first time, and then reuses this layout even if I change it afterwards.
    2. It just doesn't work as it should: it relies on the current keyboard layout for some reason, when the expected behavior would be to ignore it and always use the default one (at least, every single program I know behaves that way with the Russian+English keyboard layouts; there may be some exceptions from this rule, but I'm not aware of them yet).

    I'll try to investigate further.

  11. ForNeVeR commented on Aug 24, 2020

    @ForNeVeR
    ContributorAuthor

    This call of ToUnicode causes problems:

    int charCount = ToUnicode(virtualKey, scanCode, state, chars, chars.Length, flags);

    On my system, it looks like it remembers the first keyboard layout with which it is called, and then it just uses it for all subsequent calls. So, while it obviously should hold "r" for input values of ConsoleKey.R, 19, (an array of 256 zeros), (an array of 2 zeros), 2, 4, actually it returns a Russian letter "к".

    In the documentation, it isn't specified which layout it will use, but I could try to guess it uses current thread layout or something like that, and, since this code is called on a background thread (which may not process keyboard layout change messages properly), it may actually auto-initialize the keyboard layout for the first usage, and then leave it as-is. And this behavior could be dependent on OS version, installed language pack, or whatever else factor comes into play here.

    I believe we could try to load an English keyboard layout and pass it to ToUnicodeEx (which takes a layout as input instead of relying on some vague global value), since our hotkeys seem to be specified in English.

    Jason Shirk (@lzybkr), am I right? Would that be a good fix for the issue? I'm not quite sure why do you call all this ToUnicode business instead of e.g. creating a table like ConsoleKey.R => "r", but it surely was a good reason, I presume?

  12. lzybkr commented on Aug 24, 2020

    @lzybkr
    Contributor

    Console applications are "sticky" with regards to the keyboard layout. I reported this as a bug to the appropriate internal team but there were concerns of appcompat issues.

    I don't know if specifying the English keyboard layout works correctly or not. Hopefully it would, but I vaguely recall experimenting specifying an explicit keyboard layout and it not working.

    That said, I don't know under what conditions you would specify the English keyboard layout. How would PSReadLine know, for any given keyboard layout, that you really wanted a shortcut given that some normal keys use modifiers to type. This is why PSReadLine uses ToUnicode in the first place.

  13. 44 remaining items

  14. added 6 commits that reference this issue on Oct 19, 2023
    2ee9c7c
    40a3868
    d54cf58
    5e12c53
    574507c
    64a076b
  15. ForNeVeR commented on Nov 3, 2023

    @ForNeVeR
    ContributorAuthor

    Folks, love y'all so much. Finally, we'll have a significant improvement on this in the next release ❤️

    The shortcuts are still expected to be defunct if you have a "wrong" keyboard layout enabled when you hit Ctrl+C, but it won't stick with you until the end of PowerShell session: just change the layout to English, and it will start working again.

    It was verified in several terminal apps (including the Windows Terminal, of course), but there are cases when it is still broken (e.g. in the old console, or when you start Windows Terminal as a default terminal app, as opposed to explicitly clicking the Terminal icon).

    I'd suggest to report new cases separately, and you may link them in this thread. I'll try to take a look at new particular scenarios and improve them if possible.

    But again: we expect that in majority of cases, the issue will be gone next release. I am so happy 🎉

  16. TimofeyBiryukov commented on Feb 7, 2024

    @TimofeyBiryukov

    I've just ran in to this, and this still seems to be a problem.

    If you start PowerShell (7.4.1), with a Russian layout enabled. CTRL+C wouldn't be working. Switching back to Eng layout doesn't fix the issue. More, if you hold CTRL and press any button, the letters will be in a layout you started with, regardless of if you switched layouts or not. I.e. switch to Eng, press F button see F on screen, hold CTRL press F, see A(Ru) on screen.

  17. ForNeVeR commented on Feb 7, 2024

    @ForNeVeR
    ContributorAuthor

    Not sure if the fix was integrated into 7.4.1. So far, it looks like the latest release of PSReadLine is 2.3.4, built before merging of my PR.

    So, it looks like we just need to wait for a bit more for the next release.

    In the meantime, I think you can use my workaround fork (installation instructions are notably messy but are included in the release page).

  18. Kadeluxe commented on Apr 18, 2024

    @Kadeluxe

    I'm on 7.4.2 and it's not working without manually replacing.

  19. ForNeVeR commented on Apr 18, 2024

    @ForNeVeR
    ContributorAuthor

    Sadly, the last release, PSReadLine v2.3.5, still doesn't have my fix included. Check the list of commits by this link: v2.3.5...master

  20. daxian-dbw commented on Apr 18, 2024

    @daxian-dbw
    Member

    The fix is included in v2.4.0-beta0. Please install that version and try again.

  21. Kadeluxe commented on Apr 18, 2024

    @Kadeluxe

    Install-Module -Name PSReadLine -AllowPrerelease -Force worked, thanks for the tip.

    Once again Friedrich von Never (@ForNeVeR) thank you for your time, I really do appreciate your work here.

  22. bn45hkurr0y4 commented on Jan 29, 2026

    @bn45hkurr0y4

    This problem still exists in PSReadline 2.4.5.
    bash/zsh/cmd do not have this problem.

    PS C:\Users\Computer> Get-Module -Name PSReadLine
    
    ModuleType Version    PreRelease Name                                ExportedCommands
    ---------- -------    ---------- ----                                ----------------
    Script     2.4.5                 PSReadLine                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, …
    
    PS C:\Users\Computer> pwsh -v
    PowerShell 7.5.4
    PS C:\Users\Computer> winget list Microsoft.WindowsTerminal
    Name             Id                        Version      Source
    ---------------------------------------------------------------
    Windows Terminal Microsoft.WindowsTerminal 1.23.20211.0 winget
    
    PS C:\Users\Computer> winver
    Windows 11 25H2 26200.7623
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-PRA PR is opened targeting the issueIssue-BugIt either shouldn't be doing this or needs an investigation.Issue-KeyboardLayoutIssues related to dead keys with some keyboard layout.Issue-Usability

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions