8

I'm trying to make a simple python script that can use the keyboard to write / execute commands.

Example : open Photoshop and execute "select all and delete, then save" (control + a, delete, control + s) keys each after 1 second.

Example2 : open taskmanager (control + alt + del) use the N key to move to the N section in process and use end task (alt + e) every few minutes...

Moreover to create a function, while the python script is running if i hit alt+f1 (for example) it executes (control + alt + del)

nobody
  • 19,010
  • 17
  • 53
  • 73
Itachi Sama
  • 675
  • 1
  • 5
  • 17
  • 1
    You should check some regedit information. Even with that, some of them will not be possible to do. – GLHF Feb 21 '15 at 01:23
  • 2
    i'd appreciate any tips... all i want is to make python use the keyboard. – Itachi Sama Feb 21 '15 at 01:26
  • I'm really hoping it to be done with pure python, I've heard I can use it in like 1 line by using AutoIt.. – Itachi Sama Jul 18 '15 at 12:34
  • 1
    Instead of expressing these things in terms of UI actions, why not just write scripts to -do- the things? Like for example to kill a process, you don't need a script to interact with task manager. – roippi Jul 18 '15 at 12:34
  • You can take a look at [PyAutoIt](https://pypi.python.org/pypi/PyAutoIt/0.3) and [PYAHK AutoHotKey via Python](https://pyahk.readthedocs.org/en/latest/) – raymelfrancisco Jul 18 '15 at 12:36
  • @ItachiSama I've tried to provide an answer that seems to fit your desires. Do you wish me to provide some further explanation or tell me whether or not it's what you're looking for? – Juxhin Jul 18 '15 at 15:04
  • appreciate the effort but I really need to know, learn how to do it with pure python otherwise i could've easily went for AutoIt instead of SikuliX or anything... – Itachi Sama Jul 18 '15 at 17:34
  • @ItachiSama - You can use SikuliX with Python. I've gone ahead and updated the answer to give an example if it is relevant. – Juxhin Jul 24 '15 at 14:51

2 Answers2

2

In order to do this you need to integrate with the native messaging interfaces. Sikuli is a good test tool and so (as per @juxhin's suggestion) a sensible candidate if you can limit yourself to Jython.

However if you can't live with that, you'll probably need a different solution for Linux and Windows. For example:

Community
  • 1
  • 1
Peter Brittain
  • 12,717
  • 3
  • 34
  • 49
1

You may want to try SikuliX. This is an image-based framework that can be used with Python (via Jython), Ruby (via JRuby) and Java (via the optional Java API).

It is very useful to automate certain behavior on your screen such as opening Photoshop, clicking regions on your screen or typing via the key. For example:

//Using Java API, however the idea is the same for Python
Screen screen = new Screen()
screen.type(new Pattern("some-image.png"), "keyboard");

In Python:

def changed(event):
    print "something changed in ", event.region
    for ch in event.changes:
            ch.highlight() # highlight all changes
    sleep(1)
    for ch in event.changes:
            ch.highlight() # turn off the highlights

with selectRegion("select a region to observe") as r: # any change in r larger than 50 pixels would trigger the changed function onChange(50, changed) observe(background=True)

wait(30) # another way to observe for 30 seconds r.stopObserver()

It is quite a bit of work, but it allows you to create very robust scripts that perform your desired actions. You may also pipe console output back to your Python script via subprocess in order to change your scripts behavior based on the environment.

Rest is all limited by your imagination.

Note: Not EVERYTHING has to be done with SikuliX, infact I wouldn't recommend actually doing everything. Just certain things that may require specific behavior on your screen.

If you are strictly on Ubuntu, you may also want to look at Xpresser


Update

So I have worked around with AutoIt and PyAutoIt and would genuinely think they are suitable tools for what you wish to achieve as they can be very potent against certain applications.

Juxhin
  • 3,906
  • 5
  • 27
  • 50
  • @ItachiSama I have updated the answer for you. If you haven't checked out the alternatives already you may. If either of the answers are sufficient please mark them as correct to close this question off. – Juxhin Sep 04 '15 at 16:49