Unicode in Abbreviations

57 views
Skip to first unread message

Rowan Ackerman

unread,
Jun 25, 2021, 12:28:32 PM6/25/21
to autokey-users
I’m trying to use AutoKey to automatically create a for loop for Java when I press <Alt-Gr>+w.  The obvious way to do this would be to write a script and then configure a Hotkey, however I want it to do it only when I have it set to a specific keyboard layout.  I set the character for <Alt-Gr>+w to  (\ue005, I have my reasons), and set that as an Abbreviation.  But it didn’t work. it just typed the character.  I thought it was be because it wasn’t a defined character in Unicode (just in the private use area), so I tried using ש because it is defined (and I have the Hebrew keyboard installed).  This didn’t work either, causing me to conclude that only ASCII characters can be used in abbreviations. 
Is this true?  Is there any way around it?  Any other ideas for how to make it only do this on this keyboard? 
Any input is welcome. 
Thanks!

jos...@main.nc.us

unread,
Jun 25, 2021, 1:38:05 PM6/25/21
to autoke...@googlegroups.com
There are several things to unpack here.

* AutoKey is not very good with remapped keyboards. it will usually act as
if you typed keys (hotkeys and trigger abbreviations) on an unmapped
keyboard. You might be able to get some fancier things to work somewhat,
but you're way better off using ASCII as you have surmised.

* I'm not sure if you need it at the moment, but Alt-Gr is not defined as
a modifier key in AutoKey prior to 0.96 - which is being beta tested now.

*There is nothing native to AutoKey which detects/handles keyboard layouts.

* However, since AutoKey scripts are written in native Python 3.x, *if*
you can figure out how to get something to work in standalone Python
(where it's much easier to debug and test), you can either put all that
code into an AutoKey script and use AutoKey to invoke it - and add some
API calls to perform keyboard and mouse events or you can write a simpler
AutoKey script that directly runs your external code using the subprocess
module to invoke it and optionally retrieve its exit status and any
results it may have written to standard output (stdout).

* Subprocess will run almost anything that Linux will run, so you're not
restricted to Python for external programs either.

* Also, since you're probably not using the EN-US locale, be sure to read
https://github.com/autokey/autokey/wiki/Adding-Unicode-Characters-or-Emojis-to-Your-Scripts-or-Phrases
and browse through the rest of our wiki where you will find tips and
tricks for a number of other situations you may encounter.

On a personal note, if you figure out how to get AutoKey to reliably emit
properly indented code of any sort anywhere, please come back and tell us
about it! I have tried to do this with bash in kate without much success.
I'm sure it can be done, I just don't know how.

Joe

> I’m trying to use AutoKey to automatically create a for loop for Java when
> I press <Alt-Gr>+w. The obvious way to do thisI would be to write a script
> and then configure a Hotkey, however I want it to do it only when I have
> it
> set to a specific keyboard layout. I set the character for <Alt-Gr>+w to
> 
> (\ue005, I have my reasons), and set that as an Abbreviation. But it
> didn’t work. it just typed the character. I thought it was be because it
> wasn’t a defined character in Unicode (just in the private use area), so I
> tried using ש because it *is* defined (and I have the Hebrew keyboard
> installed). This didn’t work either, causing me to conclude that only
> ASCII characters can be used in abbreviations.
> Is this true? Is there any way around it? Any other ideas for how to
> make
> it only do this on this keyboard?
> Any input is welcome.
> Thanks!
>
> --
> 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/7d295ed1-f70c-4233-8e7e-a75045248b74n%40googlegroups.com.
>


jack

unread,
Jun 25, 2021, 2:29:47 PM6/25/21
to autoke...@googlegroups.com, Rowan Ackerman
you could try espanso at https://espanso.org/

i have just tried this definition:

  - trigger: ":test"
    replace: "tried using ש "

and when i type the trigger i get: tried using ש

espanso doesn't do anything other than expand things. it does not have all the python power that  autokey has. but in this instance it's a quick fix.


jack
off-switches are illegal

jos...@main.nc.us

unread,
Jun 25, 2021, 2:50:27 PM6/25/21
to autoke...@googlegroups.com
Another thought:

AutoKey doesn't know anything about the modes your system is in, but you do.

There are at least two ways to leverage this.

* Use AutoKey global variables.
Write one or more scripts that set or toggle AutoKey global variables
and assign them to hotkeys. E.g. Hebrew_Keyboard == true/false after
Super+H is pressed (and released).

Then write your modally sensitive scripts to retrieve the relevant
global variables and use their values to control script logic.
Invoke a set/toggle script before running the context sensitive scripts.

* Use external assets.
Same idea as above, but get the information from an external asset like
the existence or contents of a file, environment variable, or even the
result of running a program/script.

*Complementary to these - Window filters.
If you have actions that are only useful in one type of window, specify
a window filter in the action definition in AutoKey and that action will
only be invoked when a match occurs.

Currently, window filters are Python regexes which are matched against
*both* the active window title *and* the active window class. The filter
will succeed if it matches *either* one of these. This means that negative
filters (allow everything except this window) can be quite challenging to
write.
There is test code to greatly improve this capability, but it will not be
available until some time *after* 0.96.

Two or more AutoKey actions can have the same trigger abbreviations and/or
hotkeys as long as they have mutually exclusive regexes. Enforcing this
turns out to be a non-trivial problem. AutoKey will detect and disallow
identical regexes, but cannot detect equivalent or overlapping regexes.
That's up to the user to enforce.

If you use these capabilities to do anything interesting, please come back
and tell us about it.

Joe

> I’m trying to use AutoKey to automatically create a for loop for Java when
> I press <Alt-Gr>+w. The obvious way to do this would be to write a script
> and then configure a Hotkey, however I want it to do it only when I have
> it
> set to a specific keyboard layout. I set the character for <Alt-Gr>+w to
> 
> (\ue005, I have my reasons), and set that as an Abbreviation. But it
> didn’t work. it just typed the character. I thought it was be because it
> wasn’t a defined character in Unicode (just in the private use area), so I
> tried using ש because it *is* defined (and I have the Hebrew keyboard
> installed). This didn’t work either, causing me to conclude that only
> ASCII characters can be used in abbreviations.
> Is this true? Is there any way around it? Any other ideas for how to
> make
> it only do this on this keyboard?
> Any input is welcome.
> Thanks!
>

Rowan Ackerman

unread,
Jun 26, 2021, 8:20:49 AM6/26/21
to autokey-users
OK, that was a lot. 

There are several things to unpack here.

* AutoKey is not very good with remapped keyboards. it will usually act as
if you typed keys (hotkeys and trigger abbreviations) on an unmapped
keyboard. You might be able to get some fancier things to work somewhat,
but you're way better off using ASCII as you have surmised.
How does AutoKey know if the information that it’s intercepting (presumably
from X11) is from a mapped keyboard and not from a different layout? 
It just doesn’t make sense to me. 
[note that I’ve done all of the remapping directly through xkb]
Or by remapped, do you mean ‘anything other than en-us’?
Considering that Abbreviations can be more than one ASCII character, I
don’t see why a multi-bit Unicode character wouldn’t work, again it just
doesn’t make sense. 

* I'm not sure if you need it at the moment, but Alt-Gr is not defined as
a modifier key in AutoKey prior to 0.96 - which is being beta tested now.

*There is nothing native to AutoKey which detects/handles keyboard layouts.
That’s unfortunate. 
 
* However, since AutoKey scripts are written in native Python 3.x, *if*
you can figure out how to get something to work in standalone Python
Yeah, if it comes to that, I’ll have to, but I hope I can figure something
else out. 
(where it's much easier to debug and test), you can either put all that
code into an AutoKey script and use AutoKey to invoke it - and add some
API calls to perform keyboard and mouse events or you can write a simpler
AutoKey script that directly runs your external code using the subprocess
module to invoke it and optionally retrieve its exit status and any
results it may have written to standard output (stdout).
I can’t claim I understood all of that, but I still hope I won’t have to. 
 
* Subprocess will run almost anything that Linux will run, so you're not
restricted to Python for external programs either.
I tried doing something in xdotool a while ago, but it sent keys, not
characters, so on my custom keyboard it didn’t work wonderfully. 

* Also, since you're probably not using the EN-US locale, be sure to read
EN-US locale: I assume you mean the en-us keyboard in X11?
https://github.com/autokey/autokey/wiki/Adding-Unicode-Characters-or-Emojis-to-Your-Scripts-or-Phrases
and browse through the rest of our wiki where you will find tips and
tricks for a number of other situations you may encounter.
I have looked through much of this, I only posted here when I
couldn’t find anything there. 

On a personal note, if you figure out how to get AutoKey to reliably emit
properly indented code of any sort anywhere, please come back and tell us

about it! I have tried to do this with bash in kate without much success.
I'm sure it can be done, I just don't know how.
It's a bit of a mess, and probably objectively terrible, but here's something:

# Automaticaly create a Processing for loop

# Remember the original clipboard content
OriginalClipboard = clipboard.get_clipboard()

# Gets the indent (isn't perfect)
def indent():
    keyboard.send_keys("<shift>+<home>")
    keyboard.send_keys("<ctrl>+x")
    time.sleep(.2)
    return clipboard.get_clipboard()

# Send the string via the clipboard
def SendKeys(Text):
    clipboard.fill_clipboard(Text)
    keyboard.send_keys("<ctrl>+v")
    time.sleep(0.5)

# Make the loop
ind = indent()
text = ind + 'for (int i = 0; i < ; i++) {\n' + ind + '\t//Here\'s some indented code\n' + ind + '\t//and some more\n' + ind + '}'
SendKeys(text)

#Move the cursur to after <
keyboard.send_key("<up>", 2)
keyboard.send_key("<right>", 19)

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

Suggestions on improvement are welcome.  Note, I’m testing this with ‘asdf’ as
the Abbreviation. 


Joe
 
Another thought:

AutoKey doesn't know anything about the modes your system is in, but you do.

There are at least two ways to leverage this.

* Use AutoKey global variables.
Write one or more scripts that set or toggle AutoKey global variables
and assign them to hotkeys. E.g. Hebrew_Keyboard == true/false after
Super+H is pressed (and released).
I did think of this, but I think that I will inevitably forget, so I dismissed this idea

Then write your modally sensitive scripts to retrieve the relevant
global variables and use their values to control script logic.
Invoke a set/toggle script before running the context sensitive scripts.

* Use external assets.
Same idea as above, but get the information from an external asset like
the existence or contents of a file, environment variable, or even the
result of running a program/script.
I’m not sure how to have the current keyboard state in a file, but it’s a good idea.  

*Complementary to these - Window filters.
If you have actions that are only useful in one type of window, specify
a window filter in the action definition in AutoKey and that action will
only be invoked when a match occurs.  
I think this will be the way I go, if Unicode Abbreviations don’t work.  I need
to look into it more. 

Currently, window filters are Python regexes which are matched against
*both* the active window title *and* the active window class. The filter
will succeed if it matches *either* one of these. This means that negative
filters (allow everything except this window) can be quite challenging to
write.
There is test code to greatly improve this capability, but it will not be
available until some time *after* 0.96.

Two or more AutoKey actions can have the same trigger abbreviations and/or
hotkeys as long as they have mutually exclusive regexes. Enforcing this
turns out to be a non-trivial problem. AutoKey will detect and disallow
identical regexes, but cannot detect equivalent or overlapping regexes.
That's up to the user to enforce.

If you use these capabilities to do anything interesting, please come back
and tell us about it.

Joe

Thanks for all of your help!

bjo...@gmail.com

unread,
Jun 26, 2021, 9:51:06 AM6/26/21
to autokey-users
Some interesting thoughts here! Just to contribute: I have remapped my keyboard via xkb as shown here https://github.com/bjohas/Ubuntu-keyboard-map-like-OS-X/tree/master/maps (the version posted there is a little older, but if people are interested, I can post the latest one). The keyboard map remaps modifier keys (introducing Hyper, but also swithcing around other keys). As far as I can tell, it doesn't give me any problems. For example, I can easily assign scripts to shortcuts involving <Hyper>.

Björn

Little Girl

unread,
Jun 26, 2021, 12:05:21 PM6/26/21
to autokey-users
Hey there,

This seems to be a bug. Although I had originally thought that the AutoKey could only display Unicode character-codes that use the base-ten positional numeral system (that contain all digits and no letters), it display some Unicode characters that have some letters in them. It seems to matter where the letters are, though. For instance, you're more likely to get a good result if the letter is one of the last two characters in the code. It has to be taken on a case-by-case basis, though.

The character code for the character that you want to display (E005) starts with an E and the character code for the character that Joe tried to display (05E9) has an E as the second-to-last letter.

Last, but not least, the letters could have absolutely nothing to do with it. It's food for thought, though.

Little Girl

unread,
Jun 26, 2021, 12:10:19 PM6/26/21
to autokey-users
Hey there,

I thought I'd add that if the Unicode page in the wiki doesn't cover everything, please feel free to add to it. Anybody with a free GitHub account can edit the wiki and we would all welcome contributors.

On Friday, June 25, 2021 at 12:28:32 PM UTC-4 diese...@gmail.com wrote:

Rowan Ackerman

unread,
Jun 27, 2021, 9:17:28 AM6/27/21
to autokey-users
Hi,

Hey there,

This seems to be a bug. Although I had originally thought that the AutoKey could only display Unicode character-codes that use the base-ten positional numeral system (that contain all digits and no letters), it display some Unicode characters that have some letters in them. It seems to matter where the letters are, though. For instance, you're more likely to get a good result if the letter is one of the last two characters in the code. It has to be taken on a case-by-case basis, though.

The character code for the character that you want to display (E005) starts with an E and the character code for the character that Joe tried to display (05E9) has an E as the second-to-last letter.

Last, but not least, the letters could have absolutely nothing to do with it. It's food for thought, though.
 
I thought I'd add that if the Unicode page in the wiki doesn't cover everything, please feel free to add to it. Anybody with a free GitHub account can edit the wiki and we would all welcome contributors.

On Friday, June 25, 2021 at 12:28:32 PM UTC-4 diese...@gmail.com wrote:
I set the character for <Alt-Gr>+w to  (\ue005, I have my reasons), and set that as an Abbreviation.  But it didn’t work. it just typed the character. 

The issue isn't displaying the character, but rather using it as an Abbreviation, as I said quoted above.  I tried again with \u0598 (  ֮ ), which is entirely base 10 digits, but this resulted in the same thing, simply typing the character. 

jos...@main.nc.us

unread,
Jun 28, 2021, 8:34:24 PM6/28/21
to autoke...@googlegroups.com
Finally got around to trying this script.

Nice idea to get the current indent level from where the cursor is. That
was the part I was missing.

Seems to work fine except for final positioning of the cursor. I think
your code emitted the arrow keys too quickly for my system to get all of
them.
I'm going to play with it some more.

As for how it is written, it looks fine to me aside from everything being
hard coded just to see if the concept works.

When you're emulating a user typing and making sure the code doesn't emit
events too quickly, it's just not elegant no matter what you do, so if it
works and is easy to maintain, don't worry about how it looks.

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/7034efa7-cb09-40b6-bc84-6a09740a7ecbn%40googlegroups.com.
>


Reply all
Reply to author
Forward
0 new messages