Pywin32 noob Needs help

I am trying to get the selected text from an application using pywin32 by sending a ctrl-c to get it into the clip board. From my research this is what I came up with:

import win32gui as win32
import win32api
import win32con
import win32process
from win32gui import GetWindowText,GetForegroundWindow

windows = []
win32.EnumWindows(lambda hwnd, resultList: resultList.append(hwnd),
windows)
for window in windows:
    a=win32.GetWindowText(window)
    if (a.__contains__('PhoneTray')):
        win32.ShowWindow(window,9)
        win32.SetForegroundWindow(window)
        break
wnd=win32.GetForegroundWindow()
tid,pid=win32process.GetWindowThreadProcessId(wnd)
win32api.GenerateConsoleCtrlEvent(win32con.CTRL_C_EVENT,tid)
exit(0)

When I run this code I get the following traceback:

Traceback (most recent call last):
  File "./test.py", line 18, in <module>
    win32api.GenerateConsoleCtrlEvent(win32con.CTRL_C_EVENT,tid)
KeyboardInterrupt

Can someone tell me what I am doing wrong? TIA.

I am trying to get the selected text from an application using pywin32
by sending a ctrl-c to get it into the clip board. From my research
this is what I came up with:
[…]
When I run this code I get the following traceback:

Traceback (most recent call last):
 File "./test.py", line 18, in <module>
   win32api.GenerateConsoleCtrlEvent(win32con.CTRL_C_EVENT,tid)
KeyboardInterrupt

Can someone tell me what I am doing wrong? TIA.

I think your terminal environment is processing ^C as an interrupt and
not as “copy”. For example, on UNIX systems ^C in a terminal is almost
universally a keyboard interrupt used to stop a programme.

I would guess that you are in a terminal environment, where ^C is often
interrupt, rather than a “GUI”, where it is often bound to “copy”.

Maybe try ensuring the application focus is on a GUI widget rather than
the terminal where you invoked the programme?

Ah, of it might be this line from your code:

win32api.GenerateConsoleCtrlEvent(win32con.CTRL_C_EVENT,tid)

which is in the traceback, which specifily generates a Console ^C
action. Maybe there is another call to generate a “copy to clipboard”
action?

Cheers,
Cameron Simpson cs@cskk.id.au

Thanks for the reply. After some searching there does seem to be a clipboard interface in pywin32. Unfortunately there is scant information on it. I found some documentation on using it but there is nothing on where to import it from. Any ideas?