Rogue accents appearing after hotstring replacement Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
sn1perwild
Posts: 62
Joined: 04 Aug 2021, 15:11

Rogue accents appearing after hotstring replacement

Post by sn1perwild » 16 Mar 2022, 12:08

Hey, all!

I'm building my own autocorrect script with my most common typos. However, when I use accented words (I'm using a latin keyboard), I've noticed that a new accent appears after the hotstring is replaced.

Here is an example from code:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#NoEnv ; For security
#SingleInstance force

:?*:ò::ó
The goal is for
ò
to be always replaced with
ó
However, what actually happens is, right after I type
ò
the replacement happens and I press space, a new accent appears after the letter. So it ends up being
ó´
Is this normal behavior? Is there a way I can make sure this doesn't happen? :crazy:

User avatar
sn1perwild
Posts: 62
Joined: 04 Aug 2021, 15:11

Re: Rogue accents appearing after hotstring replacement

Post by sn1perwild » 17 Mar 2022, 07:34

Further investigation showed that the problem also happens with other replacements that have accents at the end:

Code: Select all

:?*:voê::você
I end up with:
você^

User avatar
sn1perwild
Posts: 62
Joined: 04 Aug 2021, 15:11

Re: Rogue accents appearing after hotstring replacement

Post by sn1perwild » 21 Mar 2022, 13:39

Can someone help me with this? is this a bug?

User avatar
mikeyww
Posts: 26605
Joined: 09 Sep 2014, 18:38

Re: Rogue accents appearing after hotstring replacement

Post by mikeyww » 21 Mar 2022, 14:29

I do not have a way to test it-- US keyboard-- but an easy workaround would be to send Backspace.

User avatar
sn1perwild
Posts: 62
Joined: 04 Aug 2021, 15:11

Re: Rogue accents appearing after hotstring replacement

Post by sn1perwild » 24 Mar 2022, 16:42

mikeyww wrote:
21 Mar 2022, 14:29
I do not have a way to test it-- US keyboard-- but an easy workaround would be to send Backspace.
Thank you for the reply!

I have added {backspace} to the end of the word as following:

Code: Select all

:?*:voê::você{backspace}
However, when the replacement happens, I'm left with
voc
If I press space bar after that, the accent insists in appearing:
voc^
Are there more commands I can add perhaps?

User avatar
mikeyww
Posts: 26605
Joined: 09 Sep 2014, 18:38

Re: Rogue accents appearing after hotstring replacement

Post by mikeyww » 24 Mar 2022, 18:37

If that is the entire script, then I do not know how to rectify it. Others here have European keyboards or others and may know more.

User avatar
sn1perwild
Posts: 62
Joined: 04 Aug 2021, 15:11

Re: Rogue accents appearing after hotstring replacement

Post by sn1perwild » 31 Mar 2022, 13:50

mikeyww wrote:
24 Mar 2022, 18:37
If that is the entire script, then I do not know how to rectify it. Others here have European keyboards or others and may know more.
It's a regular text replacement script based off the Autocorrect AHK. Other combinations work fine, it's just the ones that end with accented words.

I hope someone can shine a light :wtf:

Emile HasKey
Posts: 27
Joined: 06 Mar 2022, 17:45

Re: Rogue accents appearing after hotstring replacement

Post by Emile HasKey » 04 Jun 2022, 15:33

This link shows the special ^ key on a Portuguese/Brazilian keyboard layout.
https://en.wikipedia.org/wiki/Portuguese_keyboard_layout

This link explains how to produce an accented character.
https://www.practiceportuguese.com/how-to-type-portuguese-accents-on-an-english-keyboard/
To type ê as in mês: Shift ^ (release) e
Hopefully that provides some clues. @Gio @lexikos

Emile HasKey
Posts: 27
Joined: 06 Mar 2022, 17:45

Re: Rogue accents appearing after hotstring replacement

Post by Emile HasKey » 04 Jun 2022, 15:49

You say that the hotstrings work when the accented letter is not the last character.
You could try this workaround, if nothing else works:

Code: Select all

chars := "-()[]{}:;'""/\,.?!`n `t"
Loop Parse, chars
	Hotstring(":?*:voê" A_LoopField, "você" A_LoopField)

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Rogue accents appearing after hotstring replacement

Post by Helgef » 04 Jun 2022, 16:55

Consider using the :arrow: T option.

Cheers.

lexikos
Posts: 9560
Joined: 30 Sep 2013, 04:07
Contact:

Re: Rogue accents appearing after hotstring replacement

Post by lexikos » 04 Jun 2022, 22:04

It is a bug, fixed by v1.1.34.03.

Dead key support is very tricky, because the only system functions for (accurately) translating key codes to characters also affect the "pending dead key" state of the kernel-mode keyboard buffer.
  • When we translate a dead key VK, it puts the buffer into a "pending dead key" state. If we leave that as is and do not suppress the dead key, the active window will then translate the dead key and the result will be like pressing the dead key twice. So to work around that, we put the buffer back into its previous state by "re-translating" the key.
  • When we translate any key while there's a pending dead key, it takes the pending dead key out of the buffer. That's perfect if we're going to suppress the key, because it means the next keypress won't be affected by the pending dead key. However, if we don't end up suppressing the key, the pending dead key needs to be placed back into the buffer so that whoever else translates the key (typically the active window, but could be another keyboard hook) will get the right result.
  • There is no way to ask the OS whether there's a pending dead key in the buffer, so the keyboard hook needs to record that information whenever it translates a dead key VK. (If VK_SPACE translates to something other than " ", we can infer that there was a pending dead key, but not which key combination produced it. Also, that translation would take the pending dead key out of the buffer, and putting it back in would be difficult since we don't know the key combination.)
The main problem in your case is that when a hotstring suppresses the final character, it avoids reinserting the dead key for this keypress, but forgets to clear the hook's own record of the pending dead key. Next time the hook translates a key, it reinserts the pending dead key. Fixing this is a simple case of adding sPendingDeadKeyVK = 0; in the right place.

A second problem is that when there are multiple scripts collecting input for hotstrings or InputHook, only the process responsible for suppressing the hotstring end char knows that the pending dead key should be discarded. All other processes assume that the pending dead key is still in effect, and will presume that it needs to be reinserted to restore the buffer to the correct state (which is correct only for hooks that act before the one that will suppress the key). The fix I have come up with is to perform the translation again - if the result is the same, either there wasn't really a dead key in the buffer, or it didn't affect the result anyway and doesn't need to be reinserted.

gonduana
Posts: 2
Joined: 05 Jun 2022, 03:27

Re: Rogue accents appearing after hotstring replacement

Post by gonduana » 05 Jun 2022, 03:54

Hi Lexikox,
If I understood the bug correctly, I don't see it fixed...
I'm using AutoHotkey v1.1.34.03 (latest version just released today) and with this script:

Code: Select all

:?*:ò::ó
after pressing dead key ` followed by oooo
I get óòoo
As you can see, the hotstring runs ok for the first O, but the original dead key is applied to the second O as well (following Os are ok).

BTW: Same bug with these other dead keys existing in a Spanish keyboard (acute, diaeresis and circumflex accents):

Code: Select all

:?*:á::á ; dead key ´ + any vowel
:?*:ü::ü ; dead key ¨ + any vowel 
:*?:ê::ê ; dead key ^ + any vowel
Best regards,

lexikos
Posts: 9560
Joined: 30 Sep 2013, 04:07
Contact:

Re: Rogue accents appearing after hotstring replacement

Post by lexikos » 06 Jun 2022, 06:11

Make sure you do not have any v1.1.34.02 or earlier (or v2.0-beta.3 or earlier) processes running.

Code: Select all

#Requires AutoHotkey v1.1.34.03

:?*:ò::ó

; Input: `oooo
; Output: óooo
Looks fixed to me.

User avatar
sn1perwild
Posts: 62
Joined: 04 Aug 2021, 15:11

Re: Rogue accents appearing after hotstring replacement

Post by sn1perwild » 23 Aug 2022, 16:09

lexikos wrote:
06 Jun 2022, 06:11
Make sure you do not have any v1.1.34.02 or earlier (or v2.0-beta.3 or earlier) processes running.

Code: Select all

#Requires AutoHotkey v1.1.34.03

:?*:ò::ó

; Input: `oooo
; Output: óooo
Looks fixed to me.
Hey, Lexikos, thanks for the replies -- the most recent version fixes the issue I highlighted in the original post.

However, I noticed a new problem with dead keys whenever there is an accent in the last replaced character.

From your example:

Code: Select all

:?*:ò::ó
Whenever I type ò, it replaces with ó correctly. However, if I press Enter after that, a ` appears in the new line. This also happens with other accents such as ´, ~ and ^.

Like this:

Code: Select all

ó
`
I'm currently running AutoHotkey v1.1.34.04.

Cheers!

gonduana
Posts: 2
Joined: 05 Jun 2022, 03:27

Re: Rogue accents appearing after hotstring replacement  Topic is solved

Post by gonduana » 02 Oct 2022, 13:02

lexikos wrote:
06 Jun 2022, 06:11
Make sure you do not have any v1.1.34.02 or earlier (or v2.0-beta.3 or earlier) processes running.

Code: Select all

#Requires AutoHotkey v1.1.34.03
:?*:ò::ó
; Input: `oooo
; Output: óooo
Looks fixed to me.
Thanks for your answer and sorry for my late reply.
You are right: I confirm the culprit was an old script running an old AutoHotkey version. Closing it fixes the issue.

lexikos
Posts: 9560
Joined: 30 Sep 2013, 04:07
Contact:

Re: Rogue accents appearing after hotstring replacement

Post by lexikos » 30 Oct 2022, 05:33

@sn1perwild Fixed by v1.1.35.

Post Reply

Return to “Ask for Help (v1)”