Added reliable checks for common key presses
authorAlexander Ebert <ebert@woltlab.com>
Tue, 8 Mar 2016 13:12:54 +0000 (14:12 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Tue, 8 Mar 2016 13:12:54 +0000 (14:12 +0100)
wcfsetup/install/files/js/WoltLab/WCF/Event/Key.js [new file with mode: 0644]
wcfsetup/install/files/js/require.config.js

diff --git a/wcfsetup/install/files/js/WoltLab/WCF/Event/Key.js b/wcfsetup/install/files/js/WoltLab/WCF/Event/Key.js
new file mode 100644 (file)
index 0000000..3467e48
--- /dev/null
@@ -0,0 +1,85 @@
+/**
+ * Provides reliable checks for common key presses, uses `Event.key` on supported browsers
+ * or the deprecated `Event.which`.
+ * 
+ * @author     Alexander Ebert
+ * @copyright  2001-2016 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module     WoltLab/WCF/Event/Key
+ */
+define([], function() {
+       "use strict";
+       
+       function _isKey(event, key, which) {
+               if (!(event instanceof Event)) {
+                       throw new TypeError("Expected a valid event when testing for key '" + key + "'.");
+               }
+               
+               return event.key === key || event.which === which;
+       }
+       
+       /**
+        * @exports     WoltLab/WCF/Event/Key
+        */
+       return {
+               /**
+                * Returns true if pressed key equals 'ArrowDown'.
+                * 
+                * @param       {Event}         event           event object
+                * @return      {boolean}
+                */
+               ArrowDown: function(event) {
+                       return _isKey(event, 'ArrowDown', 40);
+               },
+               
+               /**
+                * Returns true if pressed key equals 'ArrowLeft'.
+                * 
+                * @param       {Event}         event           event object
+                * @return      {boolean}
+                */
+               ArrowLeft: function(event) {
+                       return _isKey(event, 'ArrowLeft', 37);
+               },
+               
+               /**
+                * Returns true if pressed key equals 'ArrowRight'.
+                * 
+                * @param       {Event}         event           event object
+                * @return      {boolean}
+                */
+               ArrowRight: function(event) {
+                       return _isKey(event, 'ArrowRight', 39);
+               },
+               
+               /**
+                * Returns true if pressed key equals 'ArrowUp'.
+                * 
+                * @param       {Event}         event           event object
+                * @return      {boolean}
+                */
+               ArrowUp: function(event) {
+                       return _isKey(event, 'ArrowUp', 38);
+               },
+               
+               /**
+                * Returns true if pressed key equals 'Enter'.
+                * 
+                * @param       {Event}         event           event object
+                * @return      {boolean}
+                */
+               Enter: function(event) {
+                       return _isKey(event, 'Enter', 13);
+               },
+               
+               /**
+                * Returns true if pressed key equals 'Escape'.
+                * 
+                * @param       {Event}         event           event object
+                * @return      {boolean}
+                */
+               Escape: function(event) {
+                       return _isKey(event, 'Escape', 27);
+               }
+       };
+});
index 0d2d54dd59d2f2c379690293362207a02bcad170..88110584a9bdb58f2603dc15fbb7042321e7af06 100644 (file)
@@ -23,6 +23,7 @@ requirejs.config({
                        'Dom/Util': 'WoltLab/WCF/Dom/Util',
                        'Environment': 'WoltLab/WCF/Environment',
                        'EventHandler': 'WoltLab/WCF/Event/Handler',
+                       'EventKey': 'WoltLab/WCF/Event/Key',
                        'Language': 'WoltLab/WCF/Language',
                        'List': 'WoltLab/WCF/List',
                        'ObjectMap': 'WoltLab/WCF/ObjectMap',