AK Will Not Send Ctrl+D, also how to use multiple hotkeys in one script

321 views
Skip to first unread message

AKNewbie

unread,
Aug 16, 2022, 10:02:48 AM8/16/22
to autokey-users
Long time AutoHotkey user. First time Linux user. Really struggling to find something as elegant as AutoHotkey.

Playing a browser based game, it has you use accessibility commands to speed up playing. E.g. Ctrl+1 to select choice 1.

So my end goal is to be able to just press the "Numpad1" key (Which apparently is just <np_end> to AutoKey regardless of the state of the NumLock) and have it send Ctrl+1. This way I can play with just one hand as opposed to two.

The code I would work is

keyboard.press_key('<ctrl>')
keyboard.send_key('1')
keyboard.release_key('<ctrl>')

But all that does is send a 1, no ctrl. I tested it with other classic commands like ctrl+a or ctrl+s by substituting the send_key('1') for send_key('a') or send_key('s') to make it do a select all or save page command. It's not doing that. It's like the Ctrl key doesn't register at all. It just keeps typing the letter.

This is on 0.96.0 or whatever is the latest as of Aug 15 2022.

I googled up an example documentation here:


And it says this should work just fine:

keyboard.press_key('<ctrl>')
keyboard.send_key('d', repeat=5)
keyboard.release_key('<ctrl>')

But, it doesn't. It just types the letter d 5 times instead of trying to open up add-to-bookmarks in my browser. I don't believe Firefox would have any "anti automatic input" detection that it would ignore the Ctrl key, but, hey, it doesn't work anyway!

I am trying to use the window filter setting to match only when I'm on the correct webpage in Firefox. It almost works. I can't actually use the Numpad1 key outside of the webpage, like when typing up this conversation, but it does work in other applications like AutoKey's GUI itself or in Thunderbird.

Further request would be is it possible like in AutoHotkey to define multiple hotkeys in a script? Like if I want to use Numpad1, Numpad2, Numpad3, Numpad4, etc. all in one script, where a Numpad0 can actually serve as a toggle for this remapping functionality? This would be an example AutoHotkey script:

Numpad0::toggle:=!toggle ; changes value of toggle variable from false to true or true to false
#If toggle ; if this is true
Numpad1::SendInput ^{Numpad1} ; ^ being the Ctrl modifier
Numpad2::SendInput ^{Numpad2}

#If ; close out the if because of the weird #if directive having an effect of "looping" to the top of the script, so this is telling Numpad0 to always work in any context

The way I'm envisioning myset up in AutoKey for now is making separate scripts each with a different hotkey for each remapping I want, but I don't see a way to easily enable/disable them with a 10th hotkey.

Little Girl

unread,
Aug 16, 2022, 4:48:28 PM8/16/22
to autoke...@googlegroups.com
Hey there,

AKNewbie wrote:

>Playing a browser based game, it has you use accessibility commands
>to speed up playing. E.g. Ctrl+1 to select choice 1.

Hopefully one of these examples below will work to send your
key-combination. If not, there may be other things that can be tried.

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

# This presses two keys at the same time:

keyboard.send_keys("<ctrl>+1")

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

# This presses one key, then presses and releases another key, then
releases the first key:

keyboard.press_key("<ctrl>")
keyboard.press_key("1")
keyboard.release_key("1")
keyboard.release_key("<ctrl>")

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

# This presses and releases one key after another:

keyboard.press_key("<ctrl>")
keyboard.release_key("<ctrl>")
keyboard.press_key("1")
keyboard.release_key("1")

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

# This can be useful if your program doesn't respond to any of the
ones above and presses two keys at the same time for me even though
it seems like it ought to press them one by one:

keyboard.fake_keypress("<ctrl>")
keyboard.fake_keypress("1")

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

>Further request would be is it possible like in AutoHotkey to define
>multiple hotkeys in a script?

Not by default, but if it can be scripted in Python, it should be
possible.

>Like if I want to use Numpad1, Numpad2, Numpad3, Numpad4, etc. all
>in one script, where a Numpad0 can actually serve as a toggle for
>this remapping functionality? This would be an example AutoHotkey
>script:
>
>Numpad0::toggle:=!toggle ; changes value of toggle variable from
>false to true or true to false
>#If toggle ; if this is true
>Numpad1::SendInput ^{Numpad1} ; ^ being the Ctrl modifier
>Numpad2::SendInput ^{Numpad2}
>
>#If ; close out the if because of the weird #if directive having an
>effect of "looping" to the top of the script, so this is telling
>Numpad0 to always work in any context
>
>The way I'm envisioning myset up in AutoKey for now is making
>separate scripts each with a different hotkey for each remapping I
>want, but I don't see a way to easily enable/disable them with a
>10th hotkey.

The "Toggle a looped script" example in the AutoKey wiki demonstrates
one way that you could create a toggle by using the AutoKey global
store. See it on this page:

https://github.com/autokey/autokey/wiki/Contributed-Scripts-2#toggleLoopedScript

--
Little Girl

There is no spoon.

Johnny Rosenberg

unread,
Aug 16, 2022, 5:40:50 PM8/16/22
to autoke...@googlegroups.com
Den tis 16 aug. 2022 kl 16:02 skrev AKNewbie <x_seiy...@hotmail.com>:
Long time AutoHotkey user. First time Linux user. Really struggling to find something as elegant as AutoHotkey.

Playing a browser based game, it has you use accessibility commands to speed up playing. E.g. Ctrl+1 to select choice 1.

So my end goal is to be able to just press the "Numpad1" key (Which apparently is just <np_end> to AutoKey regardless of the state of the NumLock) and have it send Ctrl+1. This way I can play with just one hand as opposed to two.

The code I would work is

keyboard.press_key('<ctrl>')
keyboard.send_key('1')
keyboard.release_key('<ctrl>')

The common way to do this is to just do:
keyboard.send_keys("<ctrl>+1")
(note the ”s” in ”keys”).

 

But all that does is send a 1, no ctrl. I tested it with other classic commands like ctrl+a or ctrl+s by substituting the send_key('1') for send_key('a') or send_key('s') to make it do a select all or save page command. It's not doing that. It's like the Ctrl key doesn't register at all. It just keeps typing the letter.

This is on 0.96.0 or whatever is the latest as of Aug 15 2022.

I googled up an example documentation here:


And it says this should work just fine:

keyboard.press_key('<ctrl>')
keyboard.send_key('d', repeat=5)
keyboard.release_key('<ctrl>')
But, it doesn't. It just types the letter d 5 times instead of trying to open up add-to-bookmarks in my browser. I don't believe Firefox would have any "anti automatic input" detection that it would ignore the Ctrl key, but, hey, it doesn't work anyway!

I guess keyboard.send_keys("<ctrl>+d"*5) could work. If not, try to just do keyboard.send_keys("<ctrl>+d") in a loop with a short delay, maybe 10 ms or so.
 


 
I am trying to use the window filter setting to match only when I'm on the correct webpage in Firefox. It almost works. I can't actually use the Numpad1 key outside of the webpage, like when typing up this conversation, but it does work in other applications like AutoKey's GUI itself or in Thunderbird.

Further request would be is it possible like in AutoHotkey to define multiple hotkeys in a script? Like if I want to use Numpad1, Numpad2, Numpad3, Numpad4, etc. all in one script, where a Numpad0 can actually serve as a toggle for this remapping functionality? This would be an example AutoHotkey script:

Numpad0::toggle:=!toggle ; changes value of toggle variable from false to true or true to false
#If toggle ; if this is true
Numpad1::SendInput ^{Numpad1} ; ^ being the Ctrl modifier
Numpad2::SendInput ^{Numpad2}

 
Sorry, I don't quite understand this one.
If you only want to be able to trigger one script using one of several hotkeys, then it can't be done in an obvious way, but you can copy your script and trigger different hotkeys to them, but I feel like that is not what you asked for, right?

 
#If ; close out the if because of the weird #if directive having an effect of "looping" to the top of the script, so this is telling Numpad0 to always work in any context

The way I'm envisioning myset up in AutoKey for now is making separate scripts each with a different hotkey for each remapping I want, but I don't see a way to easily enable/disable them with a 10th hotkey.

I still don't understand, sorry. It looks like something that could be solved in a completely different way, but I'm not sure.



Kind regards

Johnny Rosenberg
 

--
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/4f8bfd40-bb01-4142-9cda-0b9c855be243n%40googlegroups.com.

Kreezxil

unread,
Aug 17, 2022, 6:32:49 AM8/17/22
to autokey-users

AutoKey has a global storage for variables to access. You can see it in action in my contributed AutoClicker script, which is on the wiki. Lots of a great examples there to help you to cut your teeth on AutoKey.

AutoKey does allow you to bind phrases and scripts to windows/apps, incidentally, so in that way you can use the same hotkey value for many types of phrases and scripts.

Wow, you've found a bug with 0.96 beta 5! Awesome, if you haven't already, I'll post on the issue tracker about it. I just tested, and you're right, the number pad doesn't care whether the numlock is depressed or not.

Seems you found another bug too, the press_key function isn't working right either, but the following script I present does work. Apparently, when we asked the community at large to test this, they only tested with scripts using "send_keys" and not send_key or press_key.

So, this script works for sending CTRL+d to my browser 15 times in a row. Without the sleep, my browser decided I was sending it too fast and refused to do it.

for i in range(15):
   keyboard.send_keys("<ctrl>+d")
   time.sleep(0.2)

Kreezxil

unread,
Aug 17, 2022, 7:01:17 AM8/17/22
to autokey-users
Updated and appended the issue regarding press_key and send_keys not working together, might be more of a problem with the wiki as it's not legal as far as Python is concerned. press_key and release_key are asynchronous calls while send_key and send_keys are synchronous, they get higher priority in a code block and will fire first. You can see the discussion here https://github.com/autokey/autokey/issues/117#issuecomment-1217853849 and post your experience there too.

And as for the numpad issue, while this is a feature request, it details the issues with the numpad. https://github.com/autokey/autokey/issues/408

Reply all
Reply to author
Forward
0 new messages