0

Background

I have a program that calls various functions based on hotkeys and the clipboard. I am using pyperclip and aoikhotkey. This combination is working very well. However, I would like to improve it even more if possible. Currently, my workflow is as follows:

  1. Highlight target text using my mouse
  2. Press Ctrl+c to put it in the clipboard
  3. Press my hotkey combination to call the function which uses the clipboard content.

I would like to eliminate step 2 and have the function called by the hotkey "scrape" the content on my screen (possibly using mouse or cursor event monitoring).

Question

Does anyone have any ideas about how I can do this? I suspect that I might be able to use Tkinter somehow to accomplish this, but I don't have any experience working with Tkinter, so if anyone has any suggestions or hints, I would be grateful.

Reference

Here's a post asking a similar question, but using the Autohotkey scripting language:

Get Selected Text Without Using the Clipboard

Update

The title of this question was originally "Getting selected text without using the clipboard". However, I changed it because the comments section to this question helped clarify my actual needs and goals.

robasia
  • 113
  • 10
  • the simplest solution would be to poll the clipboard at a suitable rate and act whenever the clipboard content changes, this could be achieved with tkinters `Tk.after` method however if you aren't actually using the library for anything else this would be overkill, simply using a loop with a sleep is probably sufficient, however you'd need to decide an adequate poll rate – James Kent Oct 01 '18 at 15:10
  • @JamesKent Thanks for your suggestion. Actually, I would like to avoid using the clipboard all together and just use a mouse or cursor event to grab the text that I highlight on the screen automatically. This way I can use my hotkeys without first copying anything to the clipboard. – robasia Oct 01 '18 at 15:27
  • Either you use the system clipboard, or you effectively end up writing your own, and reinventing the wheel needs a compelling argument, so: why do you even care about whether it uses the clipboard or not? Is there a performance hit or the like you're running into? – Mike 'Pomax' Kamermans Oct 01 '18 at 15:53
  • @Mike'Pomax'Kamermans Thanks for your comment. Basically, I am trying to streamline my workflow so that I can get selected text without using the clipboard. This isn't necessary, but it would save me some key strokes, so I just thought I would ask if anyone had any suggestions. You can see that reference link I left in my question for a similar question using Autohotkey. Anyway, I guess there is no easy way. – robasia Oct 01 '18 at 15:56
  • Hotkey `scrape`? – Nouman Oct 01 '18 at 16:17
  • I don't know if this level of widget access is allowed by Tkinter. You might try wxPython. Alternately, you could write your own text field widget. – Daniel R. Livingston Oct 01 '18 at 16:24
  • @BlackThunder Thanks for the package recommendation. `Scrape` looks interesting, but I am afraid what I want to do is much more complicated than that. I need to some get the last active window, search for selected text, and then scrape it. – robasia Oct 01 '18 at 16:24
  • @DanielR.Livingston Thanks for the advice. I will check out wxPython, but perhaps this might end up being more trouble than it's worth just to save a few keystrokes. – robasia Oct 01 '18 at 16:26
  • Maybe start from 'first principles'...see what the approach to success would be in the actual OS widget API, and then see how portable that is to Python. https://developer.apple.com/documentation/appkit/nstextfield – Daniel R. Livingston Oct 01 '18 at 16:28
  • 1
    the problem you may run into is that for windows that control their own rendering it may not be possible to scrape at all. why don't you try adding a hook to catch global control+c events and then get the data from the clipboard a short delay afterwards? no extra keystrokes and not reinventing the wheel? – James Kent Oct 02 '18 at 07:47
  • @JamesKent It seems that I misunderstood my initial goal. I have been thinking about your comments and you are right. Your suggestions are very good. Thank you for taking the time to help me. – robasia Oct 03 '18 at 02:53
  • 1
    @DanielR.Livingston You are right. To solve this problem, I need to start from low-level 'first principles'. I have been thinking about it more, and I have realized that I misunderstood my initial goals. @ JamesKent has made some useful suggestions and I think I will explore the option of using a hook as he suggested. Thanks for your help and for pointing me in the right direction with regard to my initial question. – robasia Oct 03 '18 at 03:00
  • A different plan of attack: simple event driven programming. If the user clicks and holds the mouse button and mouse x/y are within the window / text field boundary, then when the user lifts the left mouse `hasSelectedText = True`. If True, then auto-run a Ctrl+C keypress combination. This should be pretty easy in wxPython. I’m assuming you’re doing text selection within a GUI you’ve created, and not an arbitrary window? – Daniel R. Livingston Oct 03 '18 at 03:08
  • @DanielR.Livingston That's an interesting suggestion, and that is probably how the scripting language AutoHotKey works with regard to clipboard events. I will look into wxPython as this type of functionality would be very useful in my workflow. Thank you for your time and knowledge, Daniel. – robasia Oct 03 '18 at 03:13
  • Select Text without using the Clipboard is i think not possible, you will need the Clipboard to Copy the Text (Ctrl+c) - you can do that with your Keyboard Device by pressing the keys or you can do that by command Send a Hotkey stroke:pyautogui.hotkey('Ctrl','c') - with python Packages pyautogui and pywinauto - you can send any text or hotkeys. - and if you want to use with your Mouse device you can use AutoPythonLauncher Software with this you can create Clickable Images on the Screen - watch this video [Click Here](https://www.youtube.com/watch?v=zrxXOvLwWAM) – stevecody Oct 03 '18 at 07:44
  • 1
    @stevecody Thanks a lot. This comments section to my OP has convinced me that my initial goals were unreasonable and probably not reliable. So, basically, I am now working on a solution that utilizes the clipboard. However, your solution seems like the easies yet. So, bascially, I can just 1) select the text with my mouse, 2) run my hotkey, which 3) calls `pyautogui.hotkey('Ctrl','c')`, and finally, I can 4) grab the text using `pyperclip.paste()`. I will give it a try later after I read the docs some more. Thanks again. I think this might be just what I have been looking for. – robasia Oct 03 '18 at 09:44
  • @stevecody Your solution works very well and it is the easiest to implement. Thank you for telling me about these two packages. If you want, copy and paste your comment into a answer and I will accept it. – robasia Oct 03 '18 at 14:44
  • @robasia - ok, i can do that And It is Fine to Here from you, that you did like my comment, and that help to solved your Question. – stevecody Oct 04 '18 at 11:53

1 Answers1

1

Select Text without using the Clipboard is i think not possible, you will need the Clipboard to Copy the Text (Ctrl+c) - you can do that with your Keyboard Device by pressing the keys or you can do that by command Send a Hotkey stroke:

pyautogui.hotkey('Ctrl','c')

With python Packages pyautogui and pywinauto - you can send any text or hotkeys without to having to do a pressing on a Keyboard Device. - and if you want to use with your Mouse device you can use AutoPythonLauncher Software with this you can create Clickable Images on the Screen - watch this video Click Here

stevecody
  • 658
  • 1
  • 6
  • 18