a shift spammer

92 views
Skip to first unread message

Kazii avali

unread,
Nov 9, 2022, 6:35:55 PM11/9/22
to autokey-users
so i have been wanting to make a autokey that just spams shift untill i press a keyboard button. i tried doing a import to a diffrent keyboard python thing but there are conflicts. i am pritty new to autokey so im wondering if there is any suggestions

my current code:
import time
import keyboard
while True:
    keyboard.send_keys("<shift>")
    if keyboard.is_pressed('y'):
        break
    time.sleep(1)
   

Little Girl

unread,
Dec 10, 2022, 1:02:27 PM12/10/22
to autokey-users
Hey there,

I have no idea how badly Google will mangle this, but I'm sending it out with high hopes. Here are my notes on a similar experiment we did a while back with someone who needed the right-arrow key to be pressed twice every so often, and you could replace <right><right> with <shift> to use any of it:

The following script is based on an example by Sam Sebastian in the Gitter chat. It will press the right arrow twice every two seconds until you press the Escape key. The problem is that you must time the press of the Escape key exactly or it will fail. It must be pressed at the exact end of the two seconds:

while True:
    keyboard.send_keys("<right><right>")
    time.sleep(2)
    if keyboard.wait_for_keypress("<escape", timeOut=0.5):
        break

Joe came up with a better method that uses two scripts: You start by setting. an AutoKey global variable to some value that represents true. Then, you loop sending the keypress(es), sleeping, then testing if the global variable is still set to True. You write a second script that sets that global variable to false. You assign a hotkey to that second script. Your first script will keep looping endlessly until you trigger the second script. When you trigger the second script, it sets the global variable's value to False. This breaks the loop in the first script, thereby stopping its loop.

###########################################################################################

SCRIPT 1:

# set the True value for the global x variable:
store.set_global_value("x",True)

# if the x global x variable exists:
try:
    # loop endlessly while the global x variable's value is True:
    while store.get_global_value("x") == True:
        # press the right key twice:
        keyboard.send_keys("<right><right>")
        # delay for 2 seconds:
        time.sleep(2)
# if the global x variable doesn't exist:
except:
    # do nothing rather than throwing an error:
    pass

###########################################################################################

SCRIPT 2:

# set the False value for the global x variable:
store.set_global_value("x",False)
# get rid of the global x variable entirely (optional):
store.remove_global_value("x")

###########################################################################################

Joe

unread,
Dec 11, 2022, 6:08:14 AM12/11/22
to autoke...@googlegroups.com

I remember answering this elsewhere, but I don't see it.

A couple of things.

The second method will work as written, but it might be a bit more intuitive if, instead of deleting the stored variable, you set it to a "false" value and retrieve and test that value. Then, there shouldn't be a need to handle an exception caused by attempting to get the value of a non-existent stored global variable.

Since AutoKey treats <shift> as a modifier key, I'm not sure what keyboard.send_keys() will do when given a string that only contains modifier keys. You might have to run an AutoKey trace  to see what's actually happening. You might be able to workaround this if it turns out to be a problem by sending "<shift> " (sending a shifted space key).

@Little Girl I wouldn't have thought of putting that whole thing in a try: block, but apparently, it works. That's interesting. I would have done a while True: loop with a break statement when the retrieved variable is false or non-existent. I don't know if my approach is any better than yours - it's just different.

Joe

--
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/af33976d-6da3-4631-b749-40178d222881n%40googlegroups.com.
Message has been deleted

Little Girl

unread,
Dec 11, 2022, 12:24:06 PM12/11/22
to autokey-users
Joe wrote:

>The second method will work as written, but it might be a bit more
>intuitive if, instead of deleting the stored variable, you set it to
>a "false" value and retrieve and test that value.

It wasn't obvious in the code, but that was an (optional) line of
code in case the OP wanted to do cleanup after running the scripts. I
should have commented it out.

>Then, there shouldn't be a need to handle an exception caused by
>attempting to get the value of a non-existent stored global variable.

That's true, but there shouldn't be an exception in this case since
SCRIPT 1 starts the cycle off by creating the global value, so we
know it's there.


>Since AutoKey treats <shift> as a modifier key, I'm not sure what
>keyboard.send_keys() will do when given a string that only contains
>modifier keys.

No idea, but it's listed as one of the special keys in the wiki's
"Special Keys" page, so I hope it can be used:
https://github.com/autokey/autokey/wiki/Special-Keys


>You might have to run an AutoKey trace  to see what's  actually
>happening. You might be able to workaround this if it turns out to
>be a problem by sending "<shift> " (sending a shifted space key).

It seems like this the Shift key is being used as a hotkey in
program, so I'm not sure how a space would be received, but the OP
will either know that or find it out. We can always try to figure
something else out if this doesn't work.


>@Little Girl I wouldn't have thought of putting that whole thing in
>a try: block, but apparently, it works. That's interesting.

Once bitten, twice shy and all that. I've been bitten hard by several
while loops, so I treat them with great distrust by putting them
inside of try blocks and giving them delays to give me time to react
to disasters by pressing Ctrl+c or bombing the script with xkill or
ending its process in a terminal or GUI.

Some while loops can make the computer completely useless, preventing
you from doing any of those things. It's enough to give a girl a cold
sweat while she sees the loop flying by, hears the processor fan
kicking on, and sees the machine not responding or responding badly
to attempts to stop it. At that point, the only option is to REISUB
your way out. As a result, try blocks and delays have become rather
good friends of mine.

I realize I could have put the try block inside of the while loop,
but with while loops, I tend to automatically reach for a
sledgehammer when a mallet would have done the job.


>I would have done a while True: loop with a break statement when the
>retrieved variable is false or non-existent. I don't know if my
>approach is any better than yours - it's just different.

I played around with the code to see what I could get away with and it
turns out it can be done even more simply yet. This new, shorter
version of the above scripts works without throwing an exception. I
also made the optional code more obvious and commented it out:

####################

SCRIPT 1:

# create and set the global x value to True:
store.set_global_value("x",True)

# if the global x value exists:

while store.get_global_value("x") == True:
    # press the right arrow key twice:

    keyboard.send_keys("<right><right>")
    # delay for 2 seconds:
    time.sleep(2)

####################

SCRIPT 2:

# set the global x value to False:
store.set_global_value("x",False)
# OPTIONAL: get rid of the global x value:
# store.remove_global_value("x")

####################

As a possible solution for the OP, these two scripts should work if
AutoKey plays nicely with the Shift key:

####################

SCRIPT 1:

# create and set the global x value to True:
store.set_global_value("x",True)

# if the global x value exists:

while store.get_global_value("x") == True:
    # press the shift key:
    keyboard.send_keys("<shift>")

SCRIPT 2:

# set the global x value to False:
store.set_global_value("x",False)
# OPTIONAL: get rid of the global x value:
# store.remove_global_value("x")

####################

Joe

unread,
Dec 11, 2022, 2:03:34 PM12/11/22
to autoke...@googlegroups.com

Just tried a  script which only sends one shift. AutoKey trace said it worked.

Tight loops can be a bear! I've been experiencing some in Brave and Vivaldi lately although I disabled some add-ons that were probably causing them. They might have also been driving swap crazy and may have massively aged my SSD drive. Not fun!

I have tried REISUB a number of times recently and determined that I can't reach all those keys at the same time. I just looked it up and I have been doing it wrong. You hold Ctrl+Alt down and slowly press REISUB one at a time. Raising Elephants Is So Utterly Boring!

 I do have Ctrl+Alt+Backspace enabled (which restarts X11) and that helps sometimes. I also have an icon in my panel that runs xkill and that gets me out of some tight jams when things aren't totally locked up.

Ctrl+Alt+Del is supposed to do something as well, but I haven't noticed anything when trying it. Just tried it again and it logs me out after 30 seconds if I don't cancel it. I don't use Gnome and I don't see any system manager to reassign those keys to.

Joe.

--
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.

Little Girl

unread,
Dec 12, 2022, 4:31:51 PM12/12/22
to autokey-users
Hey there,

I'm glad the Shift key worked in AutoKey. That means the last two scripts above should do it for @kaziit.

I agree on the loops. I have a healthy respect for them now that borders on downright fear, so I put them in a box every chance I get. I hadn't even thought about the possibility of them wearing out a drive, but it would be hard to wear out an SSD, so you're probably good.

There are apparently several ways to invoke REISUB and REISUO. Here's what I've found so far:
  • Slowly type REISUB while holding down the Alt and SysRq keys.
  • Slowly type REISUB while holding down the Alt and Shift and SysRq keys.
  • Slowly type REISUB while holding down the Ctrl and SysRq keys.
  • Slowly type REISUB while holding down the Ctrl and Shift and SysRq keys.
  • Slowly type REISUB while holding down the Ctrl and Alt keys. ← This was the one you suggested that has now been added to my list.
Thanks for the shortcut to restarting X11. That may solve one of those loop problems without having to resort to REISUB, so I'll try it the next time I get myself into a coding pickle.

I've got the same xkill shortcut in my panel. what image did you choose? Mine is a bomb. It's useless when the mouse won't respond, though, which sometimes happens with the more creative or elaborate lock-ups.

The behavior you're getting for Ctrl+Alt+Del is exactly what it should be. It won't work with some of the out-of-control loops, though, but it's nice to know and have in the toolbox.

jos...@main.nc.us

unread,
Dec 12, 2022, 7:06:38 PM12/12/22
to autoke...@googlegroups.com


> Hey there,
>
> I'm glad the Shift key worked in AutoKey. That means the last two scripts
> above should do it for @kaziit.
>
> I agree on the loops. I have a healthy respect for them now that borders
> on
> downright fear, so I put them in a box every chance I get. I hadn't even
> thought about the possibility of them wearing out a drive, but it would be
> hard to wear out an SSD, so you're probably good.
smartctl says otherwise.
>
> There are apparently several ways to invoke REISUB and REISUO. Here's what
> I've found so far:
>
> - Slowly type REISUB while holding down the Alt and SysRq keys.
> - Slowly type REISUB while holding down the Alt and Shift and SysRq
> keys.
> - Slowly type REISUB while holding down the Ctrl and SysRq keys.
> - Slowly type REISUB while holding down the Ctrl and Shift and SysRq
> keys.
> - Slowly type REISUB while holding down the Ctrl and Alt keys. ← This
> was the one you suggested that has now been added to my list.
>
> Thanks for the shortcut to restarting X11. That may solve one of those
> loop
> problems without having to resort to REISUB, so I'll try it the next time
> I
> get myself into a coding pickle.
It's useful when the mouse stops working or the current window stops
responding and won't let you out.
>
> I've got the same xkill shortcut in my panel. what image did you choose?
> Mine is a bomb. It's useless when the mouse won't respond, though, which
> sometimes happens with the more creative or elaborate lock-ups.
https://www.dropbox.com/s/fbc3f942dlfi0fj/xkill_light.svg?dl=0
https://www.dropbox.com/s/2otm6fvofwh8bqc/xkill_dark.svg?dl=0
https://www.dropbox.com/s/gnrttd42ws0z9x3/xkill.xpm?dl=0
>
> The behavior you're getting for Ctrl+Alt+Del is exactly what it should be.
> It won't work with some of the out-of-control loops, though, but it's nice
> to know and have in the toolbox.
>
> --
> 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/2bb90d5c-113e-4131-962a-5750a2dda9bcn%40googlegroups.com.
>

Little Girl

unread,
Dec 13, 2022, 10:31:19 PM12/13/22
to autoke...@googlegroups.com
Hey there,

>Little Girl wrote:
jos...@main.nc.us wrote:

>> I hadn't even thought about the possibility of them wearing out a
>> drive, but it would be hard to wear out an SSD, so you're probably
>> good.
>smartctl says otherwise.

That's a shame.

>> Thanks for the shortcut to restarting X11. That may solve one of
>> those loop problems without having to resort to REISUB, so I'll
>> try it the next time I get myself into a coding pickle.
>It's useful when the mouse stops working or the current window stops
>responding and won't let you out.

Good to know. Thanks.

--
Little Girl

There is no spoon.
Reply all
Reply to author
Forward
0 new messages