1

I have a file in a user folder for Autokey, that has a function I'd like to call from Autokey scripts. The function uses keyboard.send_key (and .send_keys), but Autokey throws the error global name 'keyboard' is not defined.

In Autokey:

import myfile

time.sleep(.3)

keyboard.send_keys("<shift>+<home>")
time.sleep(.1)

keyboard.send_keys("<ctrl>+x")
time.sleep(.1)

keyboard.send_keys("die('<pre>' . print_r(")
time.sleep(.1)
keyboard.send_keys("<ctrl>+v")

time.sleep(.1)
keyboard.send_keys(", 1));")

myfile.twoStepSave()

In myfile.py:

def twoStepSave():

    keyboard.send_keys("<ctrl>+s")
    time.sleep(1)

    window.activate("File Changed")
    time.sleep(0.1)

    active_title = window.get_active_title()
    if (active_title == "File Changed"):
        keyboard.send_key("<enter>")
    time.sleep(1)

    window.activate("File Already Exists")
    time.sleep(0.1)

    active_title = window.get_active_title()
    if (active_title == "File Already Exists"):
        keyboard.send_keys("<alt>+o")

I've seen other questions and answers about other globals, but can't figure out how to access and use keyboard. The script runs fine if I leave the code in the second script inside the AutoKey script. What am I missing that would allow me to use "keyboard" in the second script?

Bill in Kansas City
  • 360
  • 1
  • 5
  • 21
  • 1
    don't you need to import autokey at some point? – SuperStew Jul 09 '20 at 16:49
  • I don't know. Do I? – Bill in Kansas City Jul 09 '20 at 17:16
  • Would you be referring to the Autokey documentation behind the help menu (404) or the multiple google searches that fail to refer to the particulars of this question? AutoHOTkey has fine documentation, perhaps you're referring to that? – Bill in Kansas City Jul 09 '20 at 20:59
  • 2
    imported file may not have access to variables in main script but you can send it as argument `def twoStepSave(keyboard):` and `myfile.twoStepSave(keyboard)` – furas Jul 14 '20 at 18:57
  • @furas That was exactly it, thank you. I ended up having to send keyboard, time, and window. Curious that they weren't just available to the script. – Bill in Kansas City Jul 16 '20 at 04:39
  • @BillinKansasCity We didn't have a Debian packager for half a decade. The Help function (and lots of other things) works in the current version in Debian testing and derivatives such as Ubuntu 20.04 and newer. We also have a support list https://groups.google.com/forum/#!forum/autokey-users, Gitter page https://gitter.im/autokey/autokey, and a Wiki https://github.com/autokey/autokey/wiki. These all can be improved, but they're way better than a 404! – Joe Oct 11 '20 at 14:07

1 Answers1

1

Thanks to a comment from Joe I mananged to solve the problem by passing keyboard into the function call.

This is the code I use:

File Main_PGM.py:

import os
import sys

 sys.path.append(< Path to your AutoKey files>)
 import my_Sub_PGM
 my_Sub_PGM.SendSlowNew(keyboard, time, "Hello World!", 1)

File my_Sub_PGM.py:

import os
import time

def SendSlowNew(keyboard, time, myText, myDelay):
    for EachSign in myText:
        keyboard.send_keys(EachSign)
        time.sleep(myDelay)
    return

I use it to send text slow, otherwise AutoKey tends to "forget" characters sometimes.

gre_gor
  • 6,669
  • 9
  • 47
  • 52
  • All good except AutoKey doesn't "forget" characters. It sends them at machine speed which can cause buffer overflows/lost characters by the receiving application which was probably designed for normal speed human typing. That's why it's usually better to paste using the clipboard or add intercharacter delays as you have done. – Joe Feb 08 '23 at 12:34