1
votes

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.

1
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
@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
(in short: that should work anywhere, in any field where selectA and cut past are allowed, as a normal ctrl+x)J. Does
If you need a hot key as a trigger for some actions in the editor, you can use pywinauto.win32_hooks module. Example: github.com/pywinauto/pywinauto/blob/master/examples/…Vasily Ryabov
@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

1 Answers

0
votes

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.