question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Strange issue with Italian keyboard layout

See original GitHub issue

Description

Hello, we are using mathlive with the react wrapper by ShaMan123 https://github.com/ShaMan123/react-math-view and we are experiencing a strange issue with the keyboard mappings using the Italian keyboard layout.

I’ve prepared a minimal environment reproducing our setup here: https://glitch.com/edit/#!/confirmed-bristle-digestion?path=src%2Fapp.jsx%3A56%3A45

Steps to Reproduce

In the test environment (https://confirmed-bristle-digestion.glitch.me):

  • configure your machine with the Italian keyboard layout

  • type y ^ 2 -

  • the layout is recognized as spanish (which is fine as it is functionally equivalent to the Italian layout, at least for mathlive) immagine

  • click the “Re-mount component” link in the page (this simulates the remount of the mathlive component)

  • focus the mathfield and delete all the text ( either using Ctrl+A and backspace or with multiple backspace)

  • type again y ^ 2 -

Actual Behavior

The second time you type y ^ 2 -, the - is mapped as if the keyboard layout was set to english and produces a fraction as output. immagine (note: the layout chosen by mathlive mapping logic is indeed english)

Expected Behavior

The keyboard layout should be correctly identified also the 2nd time we type y ^ 2 -

Possible solution

We tried to analyze the issue and found that here https://github.com/arnog/mathlive/blob/d459adf1756d1b459362733d1a49f52adee64ca9/src/editor/keyboard-layout.ts#L657 the layouts sorting is done in-place and it doesn’t account for the initial layouts ordering. We added some logic that keeps the ordering stable with regard to the initial ordering (ie if two layouts have the same score they are sorted using the initial layouts order) and this seems to solve our issue.

 src/editor/keyboard-layout.ts | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/editor/keyboard-layout.ts b/src/editor/keyboard-layout.ts
index f9548e94..595085f7 100644
--- a/src/editor/keyboard-layout.ts
+++ b/src/editor/keyboard-layout.ts
@@ -492,6 +492,7 @@ const BASE_LAYOUT_MAPPING = {
 };
 
 const gKeyboardLayouts: KeyboardLayout[] = [];
+const gKeyboardLayoutsIDs: KeyboardLayoutId[] = [];
 
 let gKeyboardLayout: KeyboardLayout | undefined;
 
@@ -512,6 +513,7 @@ export function platform(): 'apple' | 'windows' | 'linux' {
 export function register(layout: KeyboardLayout): void {
   if (!layout.platform || layout.platform === platform()) {
     gKeyboardLayouts.push(layout);
+    gKeyboardLayoutsIDs.push(layout.id);
   }
 }
 
@@ -654,7 +656,13 @@ export function validateKeyboardLayout(evt?: KeyboardEvent): void {
     }
   }
 
-  gKeyboardLayouts.sort((a, b) => b.score - a.score);
+  gKeyboardLayouts.sort((a, b) =>
+    (b.score === a.score) ?
+    // sort by original order when the score is equal
+    gKeyboardLayoutsIDs.indexOf(a.id) - gKeyboardLayoutsIDs.indexOf(b.id) :
+    // sort by decreasing score
+    b.score - a.score
+  );
 }
 
 export function setKeyboardLayoutLocale(locale: string): void {

We could open a PR with this solution but we would really appreciate your feedback first, since we don’t know if this is an isolated issue or if it’s part of a bigger problem and we still have doubt on the root cause of this issue.

Thank you, have a nice day!

Environment

MathLive version: 0.68.1

Operating System: macOS, Windows, Linux

Browser: Chrome, Firefox

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
arnogcommented, Jul 6, 2021

Thanks for reporting this and for taking the time to write an excellent report and to build a reproducible case.

Your fix can work, but I don’t think it’s quite right.

I think the problem is that when the mathfield is remounted the keyboard layouts get re-registered, the new ones override the previous ones and the score they had accumulated. Instead, the keyboard layout registration should avoid registering keyboards that are already known, i.e. in register(), if gKeyboardLayouts already has a matching ID, skip the registration.

Could you give that a try? I unfortunately have some problems fully debugging through your example and it might be easier for you to make that change and validate that it works. If it does, I’d be happy to merge a PR for it.

0reactions
Temez1commented, Sep 26, 2021

@arnog Sure, see #1194

Read more comments on GitHub >

github_iconTop Results From Across the Web

Some Italian keyboard characters do not type! - Ask LibreOffice
Some Italian keyboard characters do not type! ... Please try resetting the user profile, sometimes solves strange issues.
Read more >
strange keyboard layout · Issue #311 · pqrs-org/Karabiner ...
I have a macbook with italian keyboard. ... When I execute the Karabiner element the viewer show a full keyboard with numeric pad....
Read more >
Problems with US International Keyboard Layout - dead keys
I recently got a new laptop, and I use Windows 7 + the US-International keyboard layout. I intentionally use that one because I...
Read more >
Keyboard language keeps changing automatically to a ...
Such issues can occur if it says "Keyboard: None available" or "Not assigned" or more than one layout is assigned to a language....
Read more >
Strange keyboard layout issue - Windows 10 Forums
Strange keyboard layout issue :Hey, I'm German, so of course I want to use the german QWERTZ keyset in Windows 10. Everythings works...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found