Page 1 of 1

Rogue accents appearing after hotstring replacement

Posted: 16 Mar 2022, 12:08
by sn1perwild
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:

Re: Rogue accents appearing after hotstring replacement

Posted: 17 Mar 2022, 07:34
by sn1perwild
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ê^

Re: Rogue accents appearing after hotstring replacement

Posted: 21 Mar 2022, 13:39
by sn1perwild
Can someone help me with this? is this a bug?

Re: Rogue accents appearing after hotstring replacement

Posted: 21 Mar 2022, 14:29
by mikeyww
I do not have a way to test it-- US keyboard-- but an easy workaround would be to send Backspace.

Re: Rogue accents appearing after hotstring replacement

Posted: 24 Mar 2022, 16:42
by sn1perwild
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?

Re: Rogue accents appearing after hotstring replacement

Posted: 24 Mar 2022, 18:37
by mikeyww
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.

Re: Rogue accents appearing after hotstring replacement

Posted: 31 Mar 2022, 13:50
by sn1perwild
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:

Re: Rogue accents appearing after hotstring replacement

Posted: 04 Jun 2022, 15:33
by Emile HasKey
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

Re: Rogue accents appearing after hotstring replacement

Posted: 04 Jun 2022, 15:49
by Emile HasKey
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)

Re: Rogue accents appearing after hotstring replacement

Posted: 04 Jun 2022, 16:55
by Helgef
Consider using the :arrow: T option.

Cheers.

Re: Rogue accents appearing after hotstring replacement

Posted: 04 Jun 2022, 22:04
by lexikos
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.

Re: Rogue accents appearing after hotstring replacement

Posted: 05 Jun 2022, 03:54
by gonduana
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,

Re: Rogue accents appearing after hotstring replacement

Posted: 06 Jun 2022, 06:11
by lexikos
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.

Re: Rogue accents appearing after hotstring replacement

Posted: 23 Aug 2022, 16:09
by sn1perwild
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!

Re: Rogue accents appearing after hotstring replacement  Topic is solved

Posted: 02 Oct 2022, 13:02
by gonduana
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.

Re: Rogue accents appearing after hotstring replacement

Posted: 30 Oct 2022, 05:33
by lexikos
@sn1perwild Fixed by v1.1.35.