|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <title> |
| 6 | + Capturing Keyboard Event Modifiers Across Operating Systems In JavaScript |
| 7 | + </title> |
| 8 | + |
| 9 | + <link rel="stylesheet" type="text/css" href="./demo.css" /> |
| 10 | +</head> |
| 11 | +<body> |
| 12 | + |
| 13 | + <h1> |
| 14 | + Capturing Keyboard Event Modifiers Across Operating Systems In JavaScript |
| 15 | + </h1> |
| 16 | + |
| 17 | + <p> |
| 18 | + <strong>OS Modifier:</strong> |
| 19 | + <code class="osModifier"> |
| 20 | + <!-- To be populated via JavaScript. --> |
| 21 | + </code> |
| 22 | + </p> |
| 23 | + |
| 24 | + <dl class="event"> |
| 25 | + <div class="key"> |
| 26 | + <dt>Key</dt> |
| 27 | + <dd><!-- To be populated via JavaScript. --></dd> |
| 28 | + </div> |
| 29 | + <div class="modifier"> |
| 30 | + <dt>Modified</dt> |
| 31 | + <dd><!-- To be populated via JavaScript. --></dd> |
| 32 | + </div> |
| 33 | + </dl> |
| 34 | + |
| 35 | + <script type="text/javascript"> |
| 36 | + |
| 37 | + // The "modifier" key is different across the various operating systems (OS). |
| 38 | + // Let's calculate both the human-readable key (for UI hints) and the event |
| 39 | + // property that we're going to use when checking for a modified key-operation. |
| 40 | + var osModifier = buildOsModifier(); |
| 41 | + |
| 42 | + // Gather our DOM nodes to populate. |
| 43 | + var osModifierNode = document.querySelector( ".osModifier" ); |
| 44 | + var keyNode = document.querySelector( ".event .key dd" ); |
| 45 | + var modifierNode = document.querySelector( ".event .modifier dd" ); |
| 46 | + |
| 47 | + // Render which modifier key the current OS is using. |
| 48 | + osModifierNode.textContent = osModifier.long; |
| 49 | + |
| 50 | + window.addEventListener( "keydown", handleKeydown ); |
| 51 | + |
| 52 | + // --------------------------------------------------------------------------- // |
| 53 | + // --------------------------------------------------------------------------- // |
| 54 | + |
| 55 | + // I handle the keydown event on the Window. |
| 56 | + function handleKeydown( event ) { |
| 57 | + |
| 58 | + // If this is the REFRESH PAGE event, allow the default behavior to happen - |
| 59 | + // I need this to work so that building this demo isn't a total pain. |
| 60 | + if ( isPageRefreshEvent( event ) ) { |
| 61 | + |
| 62 | + return; |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + // Since we want to capture and render key combinations that have system |
| 67 | + // modifiers, we need to prevent the default behavior (otherwise the demo |
| 68 | + // will do some funky stuff). |
| 69 | + event.preventDefault(); |
| 70 | + |
| 71 | + // Render the key properties to the page. |
| 72 | + keyNode.textContent = event.key; |
| 73 | + // Check the OS-specific property for modification. On the MacOS, this will |
| 74 | + // be the "meta" key; and elsewhere, it will be the "control" key. |
| 75 | + modifierNode.textContent = event[ osModifier.modifier ] |
| 76 | + ? "Yes" |
| 77 | + : "No" |
| 78 | + ; |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + // I build the modifier object that will best fit the current operating system. |
| 84 | + function buildOsModifier() { |
| 85 | + |
| 86 | + if ( isMacish() ) { |
| 87 | + |
| 88 | + return({ |
| 89 | + os: "Mac(ish)", |
| 90 | + long: "Command", // For human-facing views. |
| 91 | + short: "Cmd", // For human-facing views. |
| 92 | + modifier: "metaKey" // For event property inspection. |
| 93 | + }); |
| 94 | + |
| 95 | + } else { |
| 96 | + |
| 97 | + return({ |
| 98 | + os: "Non-Mac(ish)", |
| 99 | + long: "Control", // For human-facing views. |
| 100 | + short: "Ctrl", // For human-facing views. |
| 101 | + modifier: "ctrlKey" // For event property inspection. |
| 102 | + }); |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + // I determine if the current OS is Mac related (MacOS, iOS, etc). |
| 110 | + // -- |
| 111 | + // CAUTION: The "navigator.platform" property - and other properties within the |
| 112 | + // navigator object - are deprecated. As such, we should take care to make sure |
| 113 | + // that OS-detection is isolated within a function or service that can be re-used |
| 114 | + // across our application. This way, when / if it breaks, we only need to update |
| 115 | + // the operating-system detection logic within a single point-of-failure. |
| 116 | + function isMacish() { |
| 117 | + |
| 118 | + // Pattern borrowed from TinyKeys library. |
| 119 | + // -- |
| 120 | + // https://github.com/jamiebuilds/tinykeys/blob/e0d23b4f248af59ffbbe52411505c3d681c73045/src/tinykeys.ts#L50-L54 |
| 121 | + var macOsPattern = /Mac|iPod|iPhone|iPad/; |
| 122 | + |
| 123 | + return( macOsPattern.test( window.navigator.platform ) ); |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + // I determine if the given event is for a page-refresh operation. |
| 129 | + function isPageRefreshEvent( event ) { |
| 130 | + |
| 131 | + return( ( event.key === "r" ) && event[ osModifier.modifier ] ); |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + </script> |
| 136 | + |
| 137 | +</body> |
| 138 | +</html> |
0 commit comments