Python Forum
Automatically read copied text from keyboard shortcuts - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Automatically read copied text from keyboard shortcuts (/thread-2271.html)

Pages: 1 2


Automatically read copied text from keyboard shortcuts - Ambidexter2017 - Mar-03-2017

Hi All, 

Need some help in writing a python script. Currently am using windows, below is the problem description 

If i select a text and copy it using CTRL+C and then use some keyboard short cut say ctrl+alt+1 then the selected text has to be automatically pasted to some specific column in an excel sheet, similarly if i use ctrl+alt+2 then the text has to be pasted to some other column in an excel sheet. 

How can i achieve this?

How can i write a py script to read the copied text and automatically paste it to specific column in excel sheet upon using a keyboard shortcut?

Thanks and Regards
Ambi


RE: Automatically read copied text from keyboard shortcuts - wavic - Mar-03-2017

Hello!
See Xerox and Pyperclip modules. There is also clipboard but it uses Pyperclip


RE: Automatically read copied text from keyboard shortcuts - Ambidexter2017 - Mar-03-2017

Hi Wavic,

Thanks for the reply... i will check those modules, But how do i get the context of the copied text using control+C from windows to python context ? Do you have any sample script snippets or ideas ?


RE: Automatically read copied text from keyboard shortcuts - wavic - Mar-03-2017

Well, I've opened Pyperclip github page and copied some text to the clipboard. In Python is simple:

>>> import pyperclip
>>> from_clipboard = pyperclip.paste()
>>> print(from_clipboard)
Pyperclip is a cross-platform Python module for copy and paste clipboard functions. It works with Python 2 and 3.
The same is for the other modules. See their webpages for more info. In Windows first install pywin32 module
Hm! Xerox requires pywin32. For Pyperclip it's not needed


RE: Automatically read copied text from keyboard shortcuts - Ambidexter2017 - Mar-06-2017

Okie, Thanks a lot Wavic, i will work on this !


RE: Automatically read copied text from keyboard shortcuts - Ambidexter2017 - Mar-09-2017

Hi wavic,

I installed pyperclip and am able to read the clipboard contents to my script and print it. Also i have integrated the other part to write it to file. But some how am not able to integrate these. I mean to say

Now after i get the clipboard contents, the python script should be running in background to read keyboard events such as CTRL+ALT+1. Now when it detects this key combination is pressed it has to further proceed to paste those contents to a file. How do i do that? Am unable to install Tkinter module. Also i tried to work with msvrt module but somehow unable to achieve that. Can you give your suggestions?

Thanks and Regards,
Narendra


RE: Automatically read copied text from keyboard shortcuts - wavic - Mar-09-2017

Hm! You got me... I do not use Windows at all.

See this On SO. You have to use nested if statement to check for ctrl then alt then 1 key before to proceed with the clipboard and writing its content to a file.

Or you can use a different approach. Write the simple script to get the clipboard contend and add/write it to a file. Just that. And when you need it run it with a key combination by your choice. It will do its job then will exit until the next time you need it.


RE: Automatically read copied text from keyboard shortcuts - Ambidexter2017 - Mar-10-2017

I tried using pyhook module, am able to handle the key press for single keys, but key combination am trying to work it out.


RE: Automatically read copied text from keyboard shortcuts - wavic - Mar-10-2017

Show the code. And the errors if there is any


RE: Automatically read copied text from keyboard shortcuts - Ambidexter2017 - Mar-10-2017

Below is the code snippet, 
But its not fruitful :( !!, even if i press ctrl+c its going to else block


[python]
def OnKeyboardEvent(event):

   if event.Key.lower() in ['lwin', 'tab', 'lmenu', 'lcontrol']:
       print "Control or win or tab or alt is pressed"
       if event.Ascii == 99:
           print "control+c is pressed"
           exit()
   else:
       print 'MessageName:',event.MessageName
       print 'Ascii:', repr(event.Ascii), repr(chr(event.Ascii))
       print 'Key:', repr(event.Key)
       print 'KeyID:', repr(event.KeyID)
       print 'ScanCode:', repr(event.ScanCode)
       print '---'
       print "The pressed key does not match Control+c"

hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()

pythoncom.PumpMessages ()
</python>