hey there

48 views
Skip to first unread message

heythereyo

unread,
Dec 26, 2020, 11:37:28 AM12/26/20
to autokey-users
im trying to port an autohotkey thing to autokey without much success

i understand python basics, so i truly don't understand what i'm doing wrong here...

screenshots to follow


heythereyo

unread,
Dec 26, 2020, 11:43:21 AM12/26/20
to autokey-users
this is the code i was trying to port
^j::

 Loop, 20 
 send, {Up} 
 sleep, 300 
 send, ^a 
 sleep, 300 
 send, {BS} 
 sleep, 400 
 send, {Enter} 
 sleep, 400 
 send, {Enter} 
 sleep, 400 
 send, {TAB}
 } 
Return

this is what i made of it, but cant get it to work!

import time
time.sleep(1)
for main in range (20):
    keyboard.send_key("<up>")
    time.sleep(0.3)
    keyboard.press_key("<ctrl>")
    time.sleep(0.3)
    keyboard.send_key("a")
    time.sleep(0.3)
    keyboard.release_key(<"ctrl>")  
    time.sleep(0.4)
    keyboard.send_key("<delete>")
    time.sleep(0.4)
    keyboard.send_key("<enter>")
    time.sleep(0.4)
    keyboard.send_key("<enter>")
    time.sleep(0.4)
    keyboard.send_key("<tab>")

what am i doing wrong? thanks!

jack horsfield

unread,
Dec 26, 2020, 12:16:16 PM12/26/20
to autokey-users
At first glance, change all send_key to send_keys

Send_key always seems to have problems.

Jack

Little Girl

unread,
Dec 26, 2020, 12:44:54 PM12/26/20
to autoke...@googlegroups.com
Hey there,

heythereyo wrote:

You've got a typo in this line:

> keyboard.release_key(<"ctrl>")

The opening bracket around ctrl ended up outside of the quote. Change
it to this and the script will work fine:

keyboard.release_key("<ctrl>")

By the way, when AutoKey throws an error when you run a script, you
can click the "Tools" menu and choose "View script error" and it will
display a dialog showing you what went wrong. That's saved me
countless times. It doesn't always give me the answer, but at least
lets me know what went wrong.

--
Little Girl

There is no spoon.

Little Girl

unread,
Dec 26, 2020, 12:56:02 PM12/26/20
to autoke...@googlegroups.com
Hey there,

heythereyo wrote:

>im trying to port an autohotkey thing to autokey without much success

Hopefully that's already been solved now, but I thought I'd add
another way that you could code the same thing without ever having to
press and release the Ctrl key:

import time
time.sleep(1)
for main in range (20):
keyboard.send_key("<up>")
time.sleep(0.3)
keyboard.send_keys("<ctrl>+a")
time.sleep(0.4)
keyboard.send_key("<delete>")
time.sleep(0.4)
keyboard.send_key("<enter>")
time.sleep(0.4)
keyboard.send_key("<enter>")
time.sleep(0.4)
keyboard.send_key("<tab>")

Also, Jack is right that keyboard.send_key can cause problems because
it only accepts one key, so you have to be very careful not to use it
when you want it to press more than one. If you use
keyboard.send_keys instead, it accepts one or more keys, in which
case that won't be an issue. So, you could do this, for instance:

import time
time.sleep(1)
for main in range (20):
keyboard.send_keys("<up>")
time.sleep(0.3)
keyboard.send_keys("<ctrl>+a")
time.sleep(0.4)
keyboard.send_keys("<delete>")
time.sleep(0.4)
keyboard.send_keys("<enter>")
time.sleep(0.4)
keyboard.send_keys("<enter>")
time.sleep(0.4)
keyboard.send_keys("<tab>")

Johnny Rosenberg

unread,
Dec 26, 2020, 7:30:02 PM12/26/20
to autoke...@googlegroups.com
Den lör 26 dec. 2020 kl 17:43 skrev heythereyo <rex.ge...@gmail.com>:
this is the code i was trying to port
^j::

 Loop, 20 
 send, {Up} 
 sleep, 300 
 send, ^a 
 sleep, 300 
 send, {BS} 

Is BS Backspace? Just wondering since I always use BACKSPACE for that
 
 sleep, 400 
 send, {Enter} 
 sleep, 400 
 send, {Enter} 
 sleep, 400 
 send, {TAB}
 } 
Return

this is what i made of it, but cant get it to work!

import time

You don't have to import time, I think it's already done. At least my scripts work without that line.

time.sleep(1)
for main in range (20):
    keyboard.send_key("<up>")
    time.sleep(0.3)
    keyboard.press_key("<ctrl>")
    time.sleep(0.3)
    keyboard.send_key("a")

I have never tried press_key release_key, so I don't know if it is any good, but fail to see what you need it for in this case. I would have replaced the three last rows above with:
keyboard.send_keys("<ctrl>+a")
 
    time.sleep(0.3)
    keyboard.release_key(<"ctrl>")  
    time.sleep(0.4)
    keyboard.send_key("<delete>")

Is that for substituting {BS}? And is {BS} really the delete key? If you mean Backspace, it's:
keyboard.send_keys("<backspace>")
 
    time.sleep(0.4)
    keyboard.send_key("<enter>")
    time.sleep(0.4)
    keyboard.send_key("<enter>")
    time.sleep(0.4)
    keyboard.send_key("<tab>")

Also, I don't usually need those ridiculous long delays in Autokey (I need them at work though, in Windows 10).

If I was going to give it a try, I would try something like:
time.sleep(1)
for main in range (20):
    keyboard.send_keys("<up>")
    keyboard.send_keys("<ctrl>+a")
    keyboard.send_keys("<backspace>") # Or <delete>, if that's what you want.
    keyboard.send_keys("<enter>"*2)
    keyboard.send_keys("<tab>")

Or, if you really need those delays between everything, I would make a function for it:

def SendKeys(TextToSend, Delay):
    keyboard.send_keys(TextToSend)
    time.sleep(Delay)

time.sleep(1)
for main in range (20):
    SendKeys("<up>", 0.1)
    SendKeys("<ctrl>+a", 0.1)
    SendKeys("<backspace>", 0.15) # Or <delete>, if that's what you want.
    SendKeys("<enter>"*2, 0.15) # Maybe separate this into two lines, if it doesn't work properly otherwise.
    SendKeys("<tab>", 0.15)

Those delays are usually not needed. The only time I needed delays so far, was when using the clipboard. Here's an example for that:
def SendStringViaClipboard(Text):
    # Remember the original clipboard content.
    OriginalClipboard=clipboard.get_clipboard()

    clipboard.fill_clipboard(Text)
    keyboard.send_keys("<ctrl>+v")
    time.sleep(0.5)

    # Restore the clipboard to its original value.
    clipboard.fill_clipboard(OriginalClipboard)

SendStringViaClipboard("This is a test string.")
keyboard.send_keys("<enter>"*2)

In this case I think the Ctrl+v thing takes some time, so a delay is needed before putting back the old contents to the clipboard. Otherwise the old contents will be in the clipboard before the paste is completed, so the old content will be pasted instead…

Another good thing to know is that if you try to send Unicode characters (I know this isn't the problem in this particular case), except the most common ones, send_keys will probably fail. That's when sending text via the clipboard comes in handy as a workaround.In the above example, this will work:
SendStringViaClipboard("На Сопках Манчжурии")

And this will fail:
keyboard.send_keys("На Сопках Манчжурии")

Okay, I'm being a bit off topic now. Just thought it could be worth mentioning, since some people have problems with this.


what am i doing wrong? thanks!
Probably nothing really, but as someone else said, send_key is probably not very reliable, and I'm not sure if press_key or release_key are any better, and they are rarely needed anyway, I think.



Kind regards

Johnny Rosenberg

 
El sábado, 26 de diciembre de 2020 a la(s) 16:37:28 UTC, heythereyo escribió:
im trying to port an autohotkey thing to autokey without much success

i understand python basics, so i truly don't understand what i'm doing wrong here...

screenshots to follow


--
You received this message because you are subscribed to the Google Groups "autokey-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to autokey-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/autokey-users/349a5990-581a-4219-be8f-6cae343bf42an%40googlegroups.com.

Johnny Rosenberg

unread,
Dec 26, 2020, 7:33:19 PM12/26/20
to autoke...@googlegroups.com
I meant: ”(I need them at work though, using AutoHotKey in Windows 10)”.

heythereyo

unread,
Dec 26, 2020, 8:19:26 PM12/26/20
to autokey-users
thank you all! I didn't expect to get such quick and informative responses on Boxing Day!

Unfortunately, in Discord there is no way to mass delete messages. instead, one has to click through each one by one, which is why I wanted to test this out. I did find some javascript in github to do it, but when I tried it out it seemed quite erratic, crashed my browser constantly and I wanted the challenge of making my own code for this!

In the end, the tab button didn't work well, so I used a left click instead. After minimising delays and changing the animation settings inside discord, I came to the following code, which worked best:

def SendKeys(TextToSend, Delay):
    keyboard.send_keys(TextToSend)
    time.sleep(Delay)

time.sleep(0.1)
for main in range (20):
    mouse.click_relative_self(0, 0, 1)
    SendKeys("<up>", 0)
    SendKeys("<ctrl>+a", 0.1)
    SendKeys("<backspace>", 0) 
    SendKeys("<enter>", 0.4)
    SendKeys("<enter>", 0.4) 

i guess the time=0 ones could just be a send.keys() instead of going through the method, but i liked the way this looked.

p.s.
Thanks everyone for your contributions, it's the first time I try a forum from my recent w10-> mint switch!
I hope someone else finds it useful in future too!

:)

heythereyo

unread,
Dec 26, 2020, 8:24:02 PM12/26/20
to autokey-users
actually, you need a brief pause on this line:
SendKeys("<up>", 0.05)

awesome! many thanks

heythereyo

unread,
Dec 26, 2020, 9:04:08 PM12/26/20
to autokey-users
after testing it out, the only other way I can think of making this better is using a while loop instead of a for loop, so that the autohotkey keeps on going until, say, you do a right click.

Is there any specific to autokey to do this? 

By now it's not a need, I'm just wanting to know more.

the code below is just an endless loop... I didn't get any further today. 
Is the only alternative to use the clipboard in some way?

--

mouseClick = "N"
def SendKeys(TextToSend, Delay):
    keyboard.send_keys(TextToSend)
    time.sleep(Delay)

time.sleep(0.1)

while mouseClick == "N":
    mouse.click_relative_self(0, 0, 1)
    SendKeys("<up>", 0.05)
    SendKeys("<ctrl>+a", 0.1)
    SendKeys("<backspace>", 0) 
    SendKeys("<enter>", 0.4)
    SendKeys("<enter>", 0.4)
        
quit()

thanks!

Joe

unread,
Dec 27, 2020, 2:28:09 AM12/27/20
to autoke...@googlegroups.com
Great that it works for you!

Since these scripts are Python, there are probably many ways to do what
you want.

One simple "AutoKey" way to do it would be as follows.

Before the loop, set a global AutoKey variable to false or 0.

Use while true (infinite loop)

Inside the loop retrieve the AutoKey global variable. If it is set to
true or 1, break out of the loop.

Write another script bound to another hotkey which sets the AutoKey
global variable to true or 1.

When your first script is running, press the hotkey for the second
script when you want it to stop.

You will probably have to experiment with delays/timing to get the
responsiveness you need to stop at the right time. Start with long
delays and shorten them until it stops working as desired.

Joe
> *time.sleep(1)*
> *for main in range (20):*
> *    keyboard.send_keys("<up>")*
> *    keyboard.send_keys("<ctrl>+a")*
> *    keyboard.send_keys("<backspace>") # Or <delete>,
> if that's what you want.*
> *    keyboard.send_keys("<enter>"*2)
> *
> *    keyboard.send_keys("<tab>")*
>
> Or, if you really need those delays between
> everything, I would make a function for it:
>
> *def SendKeys(TextToSend, Delay):*
> *    keyboard.send_keys(TextToSend)*
> *    time.sleep(Delay)*
> *
> *
> *time.sleep(1)*
> *for main in range (20):*
> *    SendKeys("<up>", 0.1)*
> *    SendKeys("<ctrl>+a", 0.1)*
> *    SendKeys("<backspace>", 0.15) # Or <delete>, if
> that's what you want.
> *
> *    SendKeys("<enter>"*2, 0.15) # Maybe separate this
> into two lines, if it doesn't work properly otherwise.
> *
> *    SendKeys("<tab>", 0.15)*
>
> Those delays are usually not needed. The only time I
> needed delays so far, was when using the clipboard.
> Here's an example for that:
> *def SendStringViaClipboard(Text):
>     # Remember the original clipboard content.
>     OriginalClipboard=clipboard.get_clipboard()
>
>     clipboard.fill_clipboard(Text)
>     keyboard.send_keys("<ctrl>+v")
>     time.sleep(0.5)
>
>     # Restore the clipboard to its original value.
>     clipboard.fill_clipboard(OriginalClipboard)
>
> SendStringViaClipboard("This is a test string.")
> keyboard.send_keys("<enter>"*2)*
>
> In this case I think the Ctrl+v thing takes some time,
> so a delay is needed before putting back the old
> contents to the clipboard. Otherwise the old contents
> will be in the clipboard before the paste is
> completed, so the old content will be pasted instead…
>
> Another good thing to know is that if you try to send
> Unicode characters (I know this isn't the problem in
> this particular case), except the most common ones,
> send_keys will probably fail. That's when sending text
> via the clipboard comes in handy as a workaround.In
> the above example, this will work:*
> *
> *SendStringViaClipboard("На Сопках Манчжурии")*
>
> And this will fail:
> *keyboard.send_keys("На Сопках Манчжурии")*
> <https://groups.google.com/d/msgid/autokey-users/349a5990-581a-4219-be8f-6cae343bf42an%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "autokey-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to autokey-user...@googlegroups.com
> <mailto:autokey-user...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/autokey-users/eaf880a1-58fa-492f-9892-74c07a0858dan%40googlegroups.com
> <https://groups.google.com/d/msgid/autokey-users/eaf880a1-58fa-492f-9892-74c07a0858dan%40googlegroups.com?utm_medium=email&utm_source=footer>.

Johnny Rosenberg

unread,
Dec 27, 2020, 7:40:39 AM12/27/20
to autoke...@googlegroups.com
Den sön 27 dec. 2020 kl 03:04 skrev heythereyo <rex.ge...@gmail.com>:
after testing it out, the only other way I can think of making this better is using a while loop instead of a for loop, so that the autohotkey keeps on going until, say, you do a right click.

Is there any specific to autokey to do this?

I have never worked with that kind of stuff, but I would guess it's a Python thing rather than an Autokey thing. As far as I have seen others write about this, I think that everything you can do in Python you can do with Autokey, in some way or another. I'm not very good at Python though, I've only learned what I needed to learn for certain tasks, so far…  That is some basics and a very few ”tricks”.

Anyway, I found some thoughts about mouse clicking detection in Python here. Maybe they can be some kind of inspiration or something.



Kind regards

Johnny Rosenberg
Reply all
Reply to author
Forward
0 new messages