about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-07-01 16:21:10 +0000
committerbors <bors@rust-lang.org>2015-07-01 16:21:10 +0000
commit9d67b9f0f90a43e822d09201291603fc0f7601b3 (patch)
treeeea50ea481ceb11b276bcd42402aa7481eaab3fd
parentd4fe2a00276fa14a526409ccaee740ba01a17f3e (diff)
parent49b73e46d68f96e8afc8346c5d3a0ccb38f7c634 (diff)
downloadrust-9d67b9f0f90a43e822d09201291603fc0f7601b3.tar.gz
rust-9d67b9f0f90a43e822d09201291603fc0f7601b3.zip
Auto merge of #26675 - azerupi:doc-js-keyevent, r=alexcrichton
Like explained in #26016, typing `?` had no effect with non-english keyboard layouts in the docs. 

This patch seems to resolve this issue, **tested with AZERTY keyboard in Google Chrome and Firefox**. I haven't tested it with more exotic keyboard layouts or with other browsers though.

This code is based on the information found on: http://javascript.info/tutorial/keyboard-events

**More specifically:**

> The only event which reliably provides the character is keypress.

**And**

>```
// 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
  }
}
```

`?` and `S` work, `escape` however does not (on an Azerty keyboard). 

It would be good if some people could test it with other browsers and keyboard layouts: http://www.mathieudavid.org/test/rustdoc/std/index.html

**Edit:**
- swedish layout works on Firefox and Chromium
- french (azerty) mac layout works on Safari
-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,