1

I'm looking for a way to generate a ctrl+A (select all), and then ctrl+x in a python script. I know how to generate this in a specific app (pywinauto and other modules do that). But I'm looking for a way to send these keys in any apps (in any field of the active windows). I want to launch the python script containing these keys anywhere (the script will be launch using a key shortcut. Details below (1))

EDIT: I'm NOT trying to copy/past in the command windows (cf. the 2 last sentences). My script send the keys in the command windows, but that's the problem I'm trying to solve...


Using python pywinauto (or Ctypes or other modules)

I tried several propositions listed here with the same result.

I thought pywinauto could do it. Following pywinauto latest documentation I tried that:

  1. open an (any) app containing a text field (that's the active windows)
  2. place the cursor where you want to make the select all + cut/past
  3. run the script bellow using an shortcut (so you won't leave the active windows)

    from pywinauto.keyboard import SendKeys

    SendKeys('^a^x')

Result The code only print ^A^X in the python console. It doesn't do what it's suppose to do in the field of the active window (where I placed my cursor): it doesn't select all + cut the text.


Using autohotkey:

The only way I found to simulate a real crtl+A ctrl+C is by using autohotkey (it works but it's not a python solution):

  1. save the code bellow in my AHK script: select_copy.ahk

    Send, ^a

    Send, ^x

  2. create another AHK script called shortcut.ahk where you will specify a shortcut to launch select_copy.ahk (shortcut.ahk sould run constantly in windows background (2))

    !^+G:: Run select_copy.ahk , C:\Users\Me\Desktop

(meaning: when I hit ctrl+alt+shift+G run the script select_copy.ahk)

result: It works. when I call the ahk, it select/cut things in the active windows.


A combination of both did not work
I tried to launch the select_copy.ahk from within a python script (using subprocess.call) but I ended up with the same result than pywinauto (or Ctypes): it only prints ^A^X in the consol, but doesnt select&cut. So I'm wondering if python could really do what autohotkey does.

(1) What the script will do: I will launch the script (using a shortcut key) on one or another html editor, it will cut all the text, parse its source code, make some change put back the datas in the clipbboard, and past it. I'm only missing the first part (select all + cut) and the last part (past).

(2) It's not the big deal since shortcut.ahk contains also all my other ahk shortcuts and scripts.

Community
  • 1
  • 1
J. Does
  • 785
  • 3
  • 10
  • 23
  • Which application are you trying to do that? Console (cmd.exe) can't accept Ctrl+A even if you do that manually on your keyboard! You also need to set focus on the edit control before pressing Ctrl+A. So more complicated code might be required if you switched to explorer window to click on the shortcut. – Vasily Ryabov Jan 27 '17 at 17:05
  • @VasilyRyabov thanks for your reply! The ctrlX should work on multiple apps that contain html editor (mostly tinyMCE editor), in browser (firefox, chrome), and others apps. (AutoHotkey -.ahk- does that). No I'm not interested to cut past anything in the console. Python just launch it in the console, but it's not wanted. The cut past should apply in the active windows. I launch the script using AHK shortcut (ctrl+shift+p), without quitting the editor. – J. Does Jan 27 '17 at 19:48
  • (in short: that should work anywhere, in any field where selectA and cut past are allowed, as a normal ctrl+x) – J. Does Jan 27 '17 at 19:58
  • If you need a hot key as a trigger for some actions in the editor, you can use `pywinauto.win32_hooks` module. Example: https://github.com/pywinauto/pywinauto/blob/master/examples/hook_and_listen.py – Vasily Ryabov Jan 28 '17 at 08:42
  • @VasilyRyabov It's good to know but I'm simpy looking for a way to press ctrlX (cut-past). pywinauto is awesome when it comes to navigate in app menu, but apparently it can't really emulate a ctrlX (ctypes has the same limitation apparently). Only Autoit seems to be able to really press these keys. – J. Does Jan 28 '17 at 11:53
  • Hmm... Did you try something like `app.UntitledNotepad.set_focus(); app.UntitledNotepad.Edit.type_keys('^a^x')` ? It must work. – Vasily Ryabov Jan 29 '17 at 06:38
  • And please provide AutoIt script example that you think works better or more consistent that the same for pywinauto. We're just preparing 0.6.1 and collecting as much feedback as possible. – Vasily Ryabov Jan 29 '17 at 06:42
  • @VasilyRyabov thanks for your patience! I know that pywinauto can send keys to a specific app (I tried it and it works) But that's *not* what I'm looking for. Please look again at my question I completely rewrite it. I apologize it was not clear enough. – J. Does Jan 29 '17 at 21:37
  • sorry it was not `Autoit`, but `autohotkey` – J. Does Jan 29 '17 at 21:51
  • Thanks for the example! I'll try this after publishing pywinauto 0.6.1 planned for today. :) I far as I understand Autohotkey uses Windows hooks for the hotkeys functionality. Probably `pywinauto.win32_hooks` can do the same as `shortcut.ahk` but not such a short way. Will think how to improve it. – Vasily Ryabov Jan 31 '17 at 19:04
  • @VasilyRyabov The lenght of the syntax isn't really important (for me) so even a complete page of code to generate a real ctrl-X (that would apply on the active windows) would be perfect. Even if you don't manage to make it I thank you for your great work. Pywinauto is super easy to work with on specific app, thanks for it! – J. Does Feb 01 '17 at 19:11
  • @VasilyRyabov another example: here is another ahk script I'm using daily. When I select any text (from a pdf, a website, or anywhere) by pressing ctrl+C it copies the text without any formatting (very useful to copy website text), and it unwrap the text (very useful to copy pdf text) http://superuser.com/a/1109920/669391 (skip the first line: #IfWinAc...) – J. Does Feb 01 '17 at 20:50

1 Answers1

0

Your AutoHotKey script should work, and does on my machine. However, I recommend that you just have one shortcut.ahk file containing the following:

!^+G::
Send, ^a
Send, ^x
Return

...and then put this in your python file:

subprocess.call("C:\\Path\\To\\AutoHotKey.exe /r C:\\Path\\To\\shortcut.ahk")

replacing the paths with wherever the AutoHotKey executable is, and wherever the shortcut.ahk file is.

Just as a side note: !^+G:: triggers on Alt+Shift+Ctrl+G, not Shift+Ctrl+G as you wrote in your question:

(meaning: when I hit ctrl+shift+G run the script select_copy.ahk)

EDIT: Also, from the phrase in the python console in your question it seems like you're trying to select all and then cut it in CMD. This will not work at all. Instead, if you want to simply clear the console, just use the command cls (Windows only; use clear in Linux). If you want to copy the entire console output and then clear it (i.e. cut) you're gonna need something different.

retnikt
  • 586
  • 4
  • 18
  • Thanks but I'm not using the AHK shortcut to launch the AHK (I don't need python for that). The shortcut is use to launch the python script (which call the .ahk), which is suppose to select all/past **in any text field where my cursor was standing before using the shortcut** (ex. it will select/cut the text contains in the google search field of my brownser). (I'm do not want to do anything with cmd, cf my edit). – J. Does Feb 07 '17 at 11:59