Problem getting ctrl+1 hotkey to work

31 views
Skip to first unread message

kh1nav3

unread,
Jul 29, 2023, 10:19:06 PM7/29/23
to autokey-users
Hi there.

I recently installed the latest Linux Mint Cinnamon and I'm trying to figure out Autokey. One of the scripts I wrote assign the hotkey ctrl+1 to the command keyboard.send_key("1"), but, as simple as this script is, I couldn't get it to work. The reason I'm using this combination is that I already assign the 1 key to act as my left mouse button:

import keyboard
if not keyboard.is_pressed('ctrl'):
    mouse.click_relative_self(0, 0, 1)

but even after I deleted this last script, the first didn't work. What I'm doing wrong?

Little Girl

unread,
Jul 30, 2023, 4:59:17 PM7/30/23
to autoke...@googlegroups.com
Hey there,

The keyboard API[1] lives in the /lib/autokey/scripting/ directory of
AutoKey.

The /lib/autokey/scripting/__init__.py file calls that API on line
29[2] with this import statement:

from .keyboard import Keyboard

The /tests/scripting_api/test_keyboard.py file calls that API on line
25[3] with this import statement:

from autokey.scripting import Keyboard

Since it does that, it considers all *keyboard* calls to be in
reference to that module. You *might* be able to get around that by
importing your *keyboard* module with:

import keyboard as foo

And then use foo for your external keyboard calls. I haven't tested
it, though, because I don't have a keyboard module other than the one
that comes with AutoKey.

[1] https://github.com/autokey/autokey/blob/develop/lib/autokey/scripting/keyboard.py
[2] https://github.com/autokey/autokey/blob/develop/lib/autokey/scripting/__init__.py#L29
[3] https://github.com/autokey/autokey/blob/develop/tests/scripting_api/test_keyboard.py#L25

--
Little Girl

There is no spoon.

kh1nav3

unread,
Jul 30, 2023, 7:51:35 PM7/30/23
to autokey-users
Thanks for the reply, I now understand why that last bit of code did not work and I did try your suggestion (import keyboard as foo), but it did not work.

About the initial code with the <ctrl>+1 hotkey,  I don't know what I did or did not do, but now is working. Anyways, I ended assigning keyboard.send_key("1") to <alt>+q, so that I didn't need to import is_pressed() and this solved my problem.

Thanks again.

Little Girl

unread,
Jul 30, 2023, 7:58:03 PM7/30/23
to autoke...@googlegroups.com
Hey there,

kh1nav3 wrote:

>Thanks for the reply, I now understand why that last bit of code did
>not work and I did try your suggestion (import keyboard as foo), but
>it did not work.

Yeah, I wasn't sure that it would and couldn't test it since I don't
have that module. Ah, well.

>About the initial code with the <ctrl>+1 hotkey, I don't know what
>I did or did not do, but now is working.

Maybe it was that it hadn't been saved yet?

>Anyways, I ended assigning keyboard.send_key("1") to <alt>+q, so
>that I didn't need to import is_pressed() and this solved my problem.

Good to know, and I'm glad it all ended up well.

Joe

unread,
Jul 31, 2023, 6:23:03 AM7/31/23
to autoke...@googlegroups.com

Welcome to the AutoKey community, kh1nav3!

You can't get there from here (at least not directly).

First, the keyboard module (along with the rest of our API calls) is already exposed to AutoKey scripts. IIUC, importing it explicitly does nothing because Python sees it. It doesn't hurt anything though.

What I think you want for the first script is

keyboard.send_keys("1")

with  a hotkey set (in the GUI) to Ctrl+1.

The second script should be triggered by 1

mouse.click_relative(0, 0, 1)

The only problem with this is that you have to press and hold the Ctrl key *before* pressing the 1 key to have the first script triggered instead of the second one.

Generally, it's not a good idea to trigger AutoKey actions with single keypresses because it makes that key unavailable for normal use. You can get around this to some extent by using AutoKey window filters so that only specific windows are affected.

If you have a broken keyboard that can't easily be replaced or an accessibility issue and you need to remap your keyboard, the xmodmap and xkb do that. I don't think they let you map a key to a mouse action though. For that, the bind command lets you map a hotkey to a command such as running a script.

Joe

but even after I deleted this last script, the first didn't work. What I'm doing wrong? --
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/2fc981e6-cfd7-41c0-b92f-5103705d4d93n%40googlegroups.com.

kh1nav3

unread,
Aug 1, 2023, 7:19:37 PM8/1/23
to autokey-users
Thanks Joe, I was trying to bind this key because my mouse's left button is faulty. Another solution I found is using this xmodmap's command:

xmodmap -e "keycode 10 = Pointer_Button1"

But, unlike with Autokey, it doesn't let me assign the used key (keycode 10) as a hotkey in combination with ctrl or alt to another command.

Thanks and have a good week.
Reply all
Reply to author
Forward
0 new messages