arnog / mathlive

A web component for easy math input
https://cortexjs.io/mathlive
MIT License
1.56k stars 277 forks source link

[Bug] Backslash key doesn't switch to Latex mode with Norwegian layout on Win/Linux #2418

Open Mathias-S opened 3 months ago

Mathias-S commented 3 months ago

Description

Pressing the Backslash key doesn't switch to Latex mode with Norwegian layout on Win/Linux.

Steps to Reproduce

  1. Run MathLive in any browser on Windows or Linux
  2. Switch to Norwegian keyboard layout
  3. Press the backslash key image

Actual Behavior

MathLive inserts a backslash, rendered as \backslash in Latex. The video below shows the issue using an on-screen keyboard, but the issue is the same with a physical keyboard.

https://github.com/arnog/mathlive/assets/225531/bbc9f459-fea6-48c5-b759-e7664b8b43bc

Expected Behavior

MathLive should switch to Latex mode.

Environment

Tested with MathLive 0.100.0 on Windows 11 or Linux (Ubuntu), latest Chrome/Edge/Firefox. Also reproducible in Parallels Desktop on Mac if using Windows' Norwegian keyboard (not the one provided by Parallels).

Other relevant information

Commit 3f7b5d4 changed the behaviour. Before that commit, only \ was inserted (instead of \backslash), but it still didn't switch to Latex mode.

The KeyboardEvent for the backslash key (Firefox on Windows) is pasted below, the most relevant properties at the top:

code: "Equal"
​key: "\\"
​keyCode: 220

​altKey: false
bubbles: true
cancelBubble: false
cancelable: true
charCode: 0
composed: true
ctrlKey: false
currentTarget: null
defaultPrevented: false
detail: 0
eventPhase: 0
isComposing: false
​isTrusted: true
​layerX: 0
​layerY: 0
​location: 0
​metaKey: false
rangeOffset: 0
​rangeParent: null
​repeat: false
​returnValue: true
​shiftKey: false​
type: "keydown"​
which: 220
Mathias-S commented 3 months ago

Also related, so I'll leave it here: Backslash on Mac with Norwegian layout is Alt+Shift+7, and typing that causes the following error:

Ambiguous key binding [IntlBackslash] (insert(\"^\")) matches [IntlBackslash] (switchMode(\"latex\", \"\", \"\\\")) with the German keyboard layout

The two keybinding definitions which seem to clash are the following:

  {
    key: '\\',
    ifMode: 'math',
    command: ['switchMode', 'latex', '', '\\'],
  },
// and
  {
    key: '[IntlBackslash]',
    ifLayout: ['apple.german'],
    ifMode: 'math',
    command: ['insert', '^'],
  }

Luckily, the first one is the one that gets used, so it ends up not causing any problems for the user. However, this suggests that a different way of detecting backslashes altogether (just using ev.key?) would be appropriate.

Here are the properties for the Keydown event:

altKey: true
shiftKey: true
code: "Digit7"
key: "\\"
keyCode: 55

bubbles: true
cancelBubble: false
cancelable: true
charCode: 0
composed: true
ctrlKey: false
currentTarget: null
defaultPrevented: false
detail: 0
eventPhase: 0
isComposing: false
location: 0
metaKey: false
repeat: false
returnValue: true