Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

Pasting string with carriage return and tans in it

If you have a question or need help, this is the place to be.
Post Reply
IceGothic
Posts: 33
Joined: Tue Oct 20, 2009 2:47 pm

Pasting string with carriage return and tans in it

Post by IceGothic »

Hi guys,

Basically, I want EG to paste a predefined block of text when I press a certain shortcut. This text might include carriage return, new line and tabs.

Example of textblock :

"
This is Line 1,
[.....]This is a tabbed line 2,
This is the last line.
"

* in this block text, there is 2 "carriage return"+"new line" characters and one tab character.

I tried using the "copy to clipboard" action followed with a "Emulate keystrokes(CTRL-V)" but I can't make the special chars work. I also tryed the "emulate keystrokes" only passing all chars to it; it's working but is really slow.

Any of you is a python expert or have any idea?
Thanx
User avatar
jinxdone
Plugin Developer
Posts: 443
Joined: Tue Jan 02, 2007 4:08 pm

Re: Pasting string with carriage return and tans in it

Post by jinxdone »

Try the following python script.

Code: Select all

eg.WinApi.Clipboard.SetClipboardText("""This is
    a piece
of text""")
eg.SendKeys(None, "{Shift+Insert}", True)
-jinxdone
IceGothic
Posts: 33
Joined: Tue Oct 20, 2009 2:47 pm

Re: Pasting string with carriage return and tans in it

Post by IceGothic »

Awesomeeeeee!! Thanx :o)

you woudn't happen to know how to get before and reset after the old clipboard content (text, files,image,data etc)?
User avatar
jinxdone
Plugin Developer
Posts: 443
Joined: Tue Jan 02, 2007 4:08 pm

Re: Pasting string with carriage return and tans in it

Post by jinxdone »

Try this one.. the difference here is that it only works with the CF_UNICODETEXT data on the clipboard and doesn't empty your clipboard in between of other types of data. (like binary) So, if you paste into programs that do not want the CF_UNICODETEXT from the clipboard you will get whatever stuff you happen to have on the clipboard instead of the string set here.

Code: Select all

text = u"""This is
    a piece
of text"""

from eg.WinApi.Dynamic import (
    cast,
    sizeof,
    create_unicode_buffer,
    c_char_p,
    c_wchar_p,
    OpenClipboard,
    EmptyClipboard,
    CloseClipboard,
    GetClipboardData,
    SetClipboardData,
    GlobalLock,
    GlobalUnlock,
    GlobalAlloc,
    CF_TEXT,
    CF_UNICODETEXT,
    GHND,
)
import ctypes
memcpy = ctypes.cdll.msvcrt.memcpy

if eg.WinApi.Clipboard.SafeOpenClipboard():
    oldtext = u''
    # Try fetching the old clipboard content
    hClipMem = GetClipboardData(CF_UNICODETEXT)
    if hClipMem:
        oldtext = cast(GlobalLock(hClipMem), c_wchar_p).value
        GlobalUnlock(hClipMem)

    # Copy the new string to the clipboard
    charBuffer = create_unicode_buffer(text)
    charBufferSize = sizeof(charBuffer)
    hGlobalMem = GlobalAlloc(GHND, charBufferSize)
    lpGlobalMem = GlobalLock(hGlobalMem)
    memcpy(lpGlobalMem, charBuffer, charBufferSize)
    GlobalUnlock(hGlobalMem)
    try:
        SetClipboardData(CF_UNICODETEXT, hGlobalMem)
    finally:
        CloseClipboard()

    # Paste it
    eg.SendKeys(None, "{Shift+Insert}", True)

    # Put the old content back
    charBuffer = create_unicode_buffer(oldtext)
    charBufferSize = sizeof(charBuffer)
    hGlobalMem = GlobalAlloc(GHND, charBufferSize)
    lpGlobalMem = GlobalLock(hGlobalMem)
    memcpy(lpGlobalMem, charBuffer, charBufferSize)
    GlobalUnlock(hGlobalMem)
    if eg.WinApi.Clipboard.SafeOpenClipboard():
        try:
            SetClipboardData(CF_UNICODETEXT, hGlobalMem)
        finally:
            CloseClipboard()
It would be better to improve the eg.WinApi.Clipboard to facilitate this sort of working with the clipboard, but that would be a bigger job and require some planning. I hope this will do for now. (even though its an ugly hack)

-jinxdone
Post Reply