0

I have just started learning Python and this is my first script:

import os, time

class Key(object):

    def __init__(self):
        self.key = 'Hello World! '
        time.sleep(3) 
        for x in range(10):
            self.simulate()

    def simulate(self):
        cmd = "osascript -e 'tell application \"System Events\" to keystroke \"" + self.key + "\"'"
        os.system(cmd)

key = Key()
key

It will simply type Hello World! 10 times after 3 seconds.

This works great but my method simulate(self) only works on OS X. How can I make it work on Windows as well?

Reza
  • 513
  • 3
  • 7
  • 20
  • 2
    the problem is mostly that `osascript` isn't a windows function. The solution is easy to find if you search: https://stackoverflow.com/questions/2791839/which-is-the-easiest-way-to-simulate-keyboard-and-mouse-on-python#2791979 – Headhunter Xamd Jul 06 '17 at 12:09
  • @HeadhunterXamd exactly, that's the point! But is there an alternative function for Windows? – Reza Jul 06 '17 at 12:12
  • in the question I have linked you can see the easiest option for windows. as far as I know it isn't possible to use a cmd command for that. – Headhunter Xamd Jul 06 '17 at 12:15
  • @HeadhunterXamd thank you for this info. I will try that. – Reza Jul 06 '17 at 12:20

0 Answers0