From: Alexander Ebert Date: Tue, 8 Mar 2016 13:12:54 +0000 (+0100) Subject: Added reliable checks for common key presses X-Git-Tag: 3.0.0_Beta_1~2030^2~60 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=e3e3727e45bedf7d0c5f7d87ac6053013beecefd;p=GitHub%2FWoltLab%2FWCF.git Added reliable checks for common key presses --- 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 index 0000000000..3467e48e29 --- /dev/null +++ b/wcfsetup/install/files/js/WoltLab/WCF/Event/Key.js @@ -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 + * @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); + } + }; +}); diff --git a/wcfsetup/install/files/js/require.config.js b/wcfsetup/install/files/js/require.config.js index 0d2d54dd59..88110584a9 100644 --- a/wcfsetup/install/files/js/require.config.js +++ b/wcfsetup/install/files/js/require.config.js @@ -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',