]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Non latin keyboard layout support player shortcut (#5684)
authorWicklow <123956049+wickloww@users.noreply.github.com>
Wed, 8 Mar 2023 14:08:46 +0000 (14:08 +0000)
committerGitHub <noreply@github.com>
Wed, 8 Mar 2023 14:08:46 +0000 (15:08 +0100)
* Non latin keyboard layout support player shortcut

* isNaked in charge of toUpperCase

client/src/assets/player/shared/hotkeys/peertube-hotkeys-plugin.ts

index f5b4b3919fdc5a96b1d75d701c302c0d61091969..2742b21a1cdce58aa41005fcacafee4768e02c53 100644 (file)
@@ -218,12 +218,37 @@ class PeerTubeHotkeysPlugin extends Plugin {
   }
 
   private isNaked (event: KeyboardEvent, key: string) {
-    return (!event.ctrlKey && !event.altKey && !event.metaKey && !event.shiftKey && event.key === key)
+    if (key.length === 1) key = key.toUpperCase()
+
+    return (!event.ctrlKey && !event.altKey && !event.metaKey && !event.shiftKey && this.getLatinKey(event.key, event.code) === key)
   }
 
   private isNakedOrShift (event: KeyboardEvent, key: string) {
     return (!event.ctrlKey && !event.altKey && !event.metaKey && event.key === key)
   }
+
+  // Thanks Maciej Krawczyk
+  // https://stackoverflow.com/questions/70211837/keyboard-shortcuts-commands-on-non-latin-alphabet-keyboards-javascript?rq=1
+  private getLatinKey (key: string, code: string) {
+    if (key.length !== 1) {
+      return key
+    }
+
+    const capitalHetaCode = 880
+    const isNonLatin = key.charCodeAt(0) >= capitalHetaCode
+
+    if (isNonLatin) {
+      if (code.indexOf('Key') === 0 && code.length === 4) { // i.e. 'KeyW'
+        return code.charAt(3)
+      }
+
+      if (code.indexOf('Digit') === 0 && code.length === 6) { // i.e. 'Digit7'
+        return code.charAt(5)
+      }
+    }
+
+    return key.toUpperCase()
+  }
 }
 
 videojs.registerPlugin('peerTubeHotkeysPlugin', PeerTubeHotkeysPlugin)