IB2017 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks

on Windows, with the latest Tk version, using a Russian keyboard layout, I am not able to use the ubiquitous shortcuts for copy and paste (Ctrl+C and Ctrl+V) with any widget. Is there a reason while Tk does not know this shortcuts if the layout is set to Russian (no problem if set on other languages)? Is there some workaround this issue? Thank you for any suggestions that will point me in the right direction.

  • Comment on Windows Tk issue Copy and Paste Russian keyboard

Replies are listed 'Best First'.
Re: Windows Tk issue Copy and Paste Russian keyboard
by soonix (Chancellor) on May 19, 2019 at 18:32 UTC
    Possible workaround (untested) using the "Insert" and "Delete" keys:
    • Control+Insert = Copy
    • Shift+Insert = Paste
    • Shift+Delete = Cut

      Hi Soonix. Your shortcuts work, thank you. Do you have more information about why? I'd like to understand the behind the scenes.

        I know these keyboard shortcuts for a long time (actually longer than Control-C and Control-V). The Wikipedia article on Cut, copy, and paste mentions CUA which probably was one of my sources originally.

        Beware: The first mentioned WP article also warns about Windows Explorer

        Shift+Delete cannot be used to cut files; instead it deletes them without using the Recycle bin.

Re: Windows Tk issue Copy and Paste Russian keyboard
by vr (Curate) on May 22, 2019 at 18:22 UTC

    As I understand, Tk "emulates" X events when under Windows, and doesn't use MS-prescribed way to handle keyboard accelerators.

    Check this line and function it resides in. Not sure I understand 100%, how come if I press "x" holding Ctrl, the function is called with same virtual keycode 88 for X key but (1) with "state" 8 for NumLock only and next (2) with "state" now 12 for Ctrl and NumLock. Return values are 120 (ascii x) and 24 (Ctrl-x).

    Then it gets curiouser, whatever "keysym" this function returns appears to determine which, if any, accelerator to process, only. Keys pressed during "normal" input produce "normal" and expected characters displayed, regardless of changes I make, see further, to function. And more curioser, return "24" (in case of Ctrl-x) seems to be ignored and unused. But "keysym" returned for as-if-without-Ctrl call is important.

    If you are still with me, the linked line misses the fact that under Latin and non-Latin kb-layouts, for the same virtual keycode (e.g. 88 for X key), the ToAscii populates 1st byte of "buf" with either 120 or 247, the latter is cyrillic "che" in CP1251 (these characters share the same key). So, with Russian kb-layout active, Ctrl-x doesn't cut in Tk.

    Long story short, replace that line with:

    static HKL USKB; if (!(int)USKB) USKB = LoadKeyboardLayout("00000409", 0); result = ToAsciiEx(keycode, scancode, keys, (LPWORD) buf, 0, USKB);

    and then Ctrl-x, -c, -v work as expected under Russian layout, too. No idea if it's "right" way to fix the problem, though. Nor if it's acceptable for you to advise to compile Tk or distribute modified versions.

    BTW, it's not true that "Ctrl-C" and friends work in Tk under US or any Latin keyboard. I mean, they don't if you press Ctrl-c with CapsLock active. Which doesn't contradict the description above and could be fixed if it bothers anybody (evidently not).

Re: Windows Tk issue Copy and Paste Russian keyboard
by Anonymous Monk on May 19, 2019 at 23:42 UTC

    Code?

    keyboard layout is an operating system concept, Tk knows nothing about it, it just gets keycodes

      Hello Anonymous. No code this time, as Copy and Paste works in Tk out-of-the-box on Windows (and on macOS too), so I do not normally need to handle it specifically. I admit I do not know how it works behind the scenes (but I would like to get info about it). I just noted that it "simply works" on any layout I tried, except Russian (there may be others though). And my Russian users have told me that the standard shortkeys should be the usual ones (ctrl+C/V).

        How do you type Ctrl+C and Ctrl+V on a Russian keyboard? Are they Ctrl+Ц and Ctrl+В?

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]