Python Forum
Automatically read copied text from keyboard shortcuts
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Automatically read copied text from keyboard shortcuts
#1
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
Reply
#2
Hello!
See Xerox and Pyperclip modules. There is also clipboard but it uses Pyperclip
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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 ?
Reply
#4
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Okie, Thanks a lot Wavic, i will work on this !
Reply
#6
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
Reply
#7
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
I tried using pyhook module, am able to handle the key press for single keys, but key combination am trying to work it out.
Reply
#9
Show the code. And the errors if there is any
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
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>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Read text file, modify it then write back Pavel_47 5 1,587 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  Attempting to read keyboard outside of console Jimmy998 5 1,349 Nov-25-2022, 08:17 PM
Last Post: Larz60+
  Need help with infinite loop & making hotkeys/shortcuts Graxum 1 1,158 Aug-22-2022, 02:57 AM
Last Post: deanhystad
  read a text file, find all integers, append to list oldtrafford 12 3,517 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  Creating Shortcuts with python Oshadha 2 1,837 Jun-23-2022, 08:07 AM
Last Post: Oshadha
  Not able to read the text using pexpect/expect Bipinjohnson 7 4,030 Jan-10-2022, 11:21 AM
Last Post: Bipinjohnson
  [SOLVED] Read text file from some point till EOF? Winfried 1 1,950 Oct-10-2021, 10:29 PM
Last Post: Winfried
  Why changing data in a copied list changes the original list? plumberpy 3 2,224 Aug-14-2021, 02:26 AM
Last Post: plumberpy
  Open and read multiple text files and match words kozaizsvemira 3 6,733 Jul-07-2021, 11:27 AM
Last Post: Larz60+
  read terminal text from running program AArdvark_UK 2 1,882 Aug-27-2020, 12:43 PM
Last Post: AArdvark_UK

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020