Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

clipboard and unicode text

by Animor (Initiate)
on Nov 15, 2017 at 22:03 UTC ( [id://1203528]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I'm using Active Perl 5.24 on Windows 10. I'm trying to set Unicode text to clipboard, using Win32::Clipboard, however when I paste the clipboard I get "Gibberish" for the Unicode characters. Is there a simple way of doing it? I saw in an old thread that using Win32::API with SetClipboardData and CF_UNICODETEXT should work, but I'm not sure how to use it. Looks like SetClipboardData is expecting to get some kind of a handle. Any help is appreciated.

Replies are listed 'Best First'.
Re: clipboard and unicode text (+ question in postscript)
by vr (Curate) on Nov 16, 2017 at 17:03 UTC

    Win32::Clipboard won't place Unicode text on Clipboard by design:

    CF_TEXT ... is the only format you can use to set clipboard data

    This is what works for me (written based on that):

    use strict; use warnings; use Win32::API 'WriteMemory'; use Encode 'encode'; Win32::API-> Import( 'kernel32', 'HGLOBAL GlobalAlloc( UINT uFlags, S +IZE_T dwBytes )' ) or die; Win32::API-> Import( 'kernel32', 'UINT_PTR GlobalLock( HGLOBAL hMem )' + ) or die; Win32::API-> Import( 'kernel32', 'BOOL GlobalUnlock( HGLOBAL hMem +)' ) or die; Win32::API-> Import( 'kernel32', 'HGLOBAL GlobalFree( HGLOBAL hMem )' + ) or die; Win32::API-> Import( 'user32', 'BOOL OpenClipboard( HWND hWndNew +Owner )' ) or die; Win32::API-> Import( 'user32', 'HANDLE SetClipboardData( UINT uFor +mat, HANDLE hMem )' ) or die; Win32::API-> Import( 'user32', 'BOOL CloseClipboard( )' ) + or die; my $str = "\x{05d0}\x{05d1}\x{05d2}"; # Alef Bet Gimel my $buf = encode( 'UTF16LE', $str ) . "\0\0"; my $h = GlobalAlloc( 2, length $buf ) or die; # GMEM_MOVEABLE my $p = GlobalLock( $h ) or die; WriteMemory( $p, $buf, length $buf ); GlobalUnlock( $h ); OpenClipboard( 0 ) or die; SetClipboardData( 13, $h ) or die; # CF_UNICODETEXT CloseClipboard() or die; GlobalFree( $h );

    P.S. If your ANSI CP is 1255 (mine isn't, so I tested with another esoteric CP), and you don't mind appended newline, you could simply do this:

    use Encode 'encode'; my $str = encode 'CP1255', "\x{05d0}\x{05d1}\x{05d2}"; system "echo $str| clip";

    P.P.S. BTW, can anyone enlighten me, how Perl encodes argument to system, on Windows? Can this encoding be changed?

    use strict; use warnings; use Encode 'encode'; my $str = encode 'CP1255', "\x{05d0}\x{05d1}\x{05d2}"; system "echo $str";

    Output from Perl script still corresponds to my original CP1251.

    
    C:\>mode con cp select=1255
    
    Status for device CON:
    ----------------------
        Lines:          9000
        Columns:        80
        Keyboard rate:  31
        Keyboard delay: 1
        Code page:      1255
    
    
    C:\>echo אבג
    אבג
    
    C:\>perl 171116.pl
    абв
    
    
      Thank you very much! The first method seems to work fine.
Re: clipboard and unicode text
by Anonymous Monk on Nov 16, 2017 at 01:31 UTC

    Hi,

    Do you have sample code?

      Yes, this is an example of what I'm trying to do:
      use Win32::Clipboard; use utf8; tie my $clipboard, 'Win32::Clipboard'; my $text = "אושרי"; # please note that I + wrote here a unicode string (Hebrew chars), but this forum changed i +t automatically to these numbers (ASCII I guess) $clipboard = $text;
      Then, outside of the program, I try to paste (using ctrl-v) the unicode string. I get "Gibberish". I also tried the following, which didn't work:
      utf8::decode($text); utf8::encode($text);
        Where are you pasting?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1203528]
Approved by 1nickt
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-20 03:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found