about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMathieu David <mathieudavid@mathieudavid.org>2015-06-30 09:32:15 +0200
committerMathieu David <mathieudavid@mathieudavid.org>2015-07-01 10:11:23 +0200
commit49b73e46d68f96e8afc8346c5d3a0ccb38f7c634 (patch)
treef362f378e45c553d8ff975943e1406e792413ec4
parent27975c49a643d0b2f8cbcd7854931c4c9a8c5dbf (diff)
downloadrust-49b73e46d68f96e8afc8346c5d3a0ccb38f7c634.tar.gz
rust-49b73e46d68f96e8afc8346c5d3a0ccb38f7c634.zip
In js from the docs, change keyboard eventlistener to be compatible with non-english keyboard layouts. Fixes #26016 Fixes #16572
-rw-r--r--src/librustdoc/html/static/main.js39
1 files changed, 33 insertions, 6 deletions
diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js
index fb8f511795e..4a033452774 100644
--- a/src/librustdoc/html/static/main.js
+++ b/src/librustdoc/html/static/main.js
@@ -76,17 +76,46 @@
     highlightSourceLines(null);
     $(window).on('hashchange', highlightSourceLines);
 
-    $(document).on('keyup', function handleKeyboardShortcut(e) {
+    // Helper function for Keyboard events,
+    // Get's the char from the keypress event
+    //
+    // This method is used because e.wich === x is not
+    // compatible with non-english keyboard layouts
+    //
+    // Note: event.type must be keypress !
+    function getChar(event) {
+      if (event.which == null) {
+        return String.fromCharCode(event.keyCode) // IE
+      } else if (event.which!=0 && event.charCode!=0) {
+        return String.fromCharCode(event.which)   // the rest
+      } else {
+        return null // special key
+      }
+    }
+
+    $(document).on('keypress', function handleKeyboardShortcut(e) {
         if (document.activeElement.tagName === 'INPUT') {
             return;
         }
 
-        if (e.which === 191) { // question mark
+        if (getChar(e) === '?') {
             if (e.shiftKey && $('#help').hasClass('hidden')) {
                 e.preventDefault();
                 $('#help').removeClass('hidden');
             }
-        } else if (e.which === 27) { // esc
+        } else if (getChar(e) === 's' || getChar(e) === 'S') {
+            e.preventDefault();
+            $('.search-input').focus();
+        }
+    }).on('keydown', function(e) {
+        // The escape key event has to be captured with the keydown event.
+        // Because keypressed has no keycode for the escape key
+        // (and other special keys in general)...
+        if (document.activeElement.tagName === 'INPUT') {
+            return;
+        }
+
+        if (e.keyCode === 27) { // escape key
             if (!$('#help').hasClass('hidden')) {
                 e.preventDefault();
                 $('#help').addClass('hidden');
@@ -95,9 +124,6 @@
                 $('#search').addClass('hidden');
                 $('#main').removeClass('hidden');
             }
-        } else if (e.which === 83) { // S
-            e.preventDefault();
-            $('.search-input').focus();
         }
     }).on('click', function(e) {
         if (!$(e.target).closest('#help').length) {
@@ -105,6 +131,7 @@
         }
     });
 
+
     $('.version-selector').on('change', function() {
         var i, match,
             url = document.location.href,