@whitesev/domutils 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.amd.js CHANGED
@@ -765,6 +765,97 @@ define((function () { 'use strict';
765
765
  }
766
766
  DOMUtilsContext.on(target, "keypress", null, handler, option);
767
767
  }
768
+ /**
769
+ * 监听某个元素键盘按键事件或window全局按键事件
770
+ * 按下有值的键时触发,按下Ctrl\Alt\Shift\Meta是无值键。按下先触发keydown事件,再触发keypress事件。
771
+ * @param target 需要监听的对象,可以是全局Window或者某个元素
772
+ * @param eventName 事件名,默认keypress
773
+ * @param callback 自己定义的回调事件,参数1为当前的key,参数2为组合按键,数组类型,包含ctrl、shift、alt和meta(win键或mac的cmd键)
774
+ * @param options 监听事件的配置
775
+ * @example
776
+ Utils.listenKeyboard(window,(keyName,keyValue,otherKey,event)=>{
777
+ if(keyName === "Enter"){
778
+ console.log("回车按键的值是:"+keyValue)
779
+ }
780
+ if(otherKey.indexOf("ctrl") && keyName === "Enter" ){
781
+ console.log("Ctrl和回车键");
782
+ }
783
+ })
784
+ * @example
785
+ 字母和数字键的键码值(keyCode)
786
+ 按键 键码 按键 键码 按键 键码 按键 键码
787
+ A 65 J 74 S 83 1 49
788
+ B 66 K 75 T 84 2 50
789
+ C 67 L 76 U 85 3 51
790
+ D 68 M 77 V 86 4 52
791
+ E 69 N 78 W 87 5 53
792
+ F 70 O 79 X 88 6 54
793
+ G 71 P 80 Y 89 7 55
794
+ H 72 Q 81 Z 90 8 56
795
+ I 73 R 82 0 48 9 57
796
+
797
+ 数字键盘上的键的键码值(keyCode)
798
+ 功能键键码值(keyCode)
799
+ 按键 键码 按键 键码 按键 键码 按键 键码
800
+ 0 96 8 104 F1 112 F7 118
801
+ 1 97 9 105 F2 113 F8 119
802
+ 2 98 * 106 F3 114 F9 120
803
+ 3 99 + 107 F4 115 F10 121
804
+ 4 100 Enter 108 F5 116 F11 122
805
+ 5 101 - 109 F6 117 F12 123
806
+ 6 102 . 110
807
+ 7 103 / 111
808
+
809
+ 控制键键码值(keyCode)
810
+ 按键 键码 按键 键码 按键 键码 按键 键码
811
+ BackSpace 8 Esc 27 → 39 -_ 189
812
+ Tab 9 Spacebar 32 ↓ 40 .> 190
813
+ Clear 12 Page Up 33 Insert 45 /? 191
814
+ Enter 13 Page Down 34 Delete 46 `~ 192
815
+ Shift 16 End 35 Num Lock 144 [{ 219
816
+ Control 17 Home 36 ;: 186 \| 220
817
+ Alt 18 ← 37 =+ 187 ]} 221
818
+ Cape Lock 20 ↑ 38 ,< 188 '" 222
819
+
820
+ 多媒体键码值(keyCode)
821
+ 按键 键码
822
+ 音量加 175
823
+ 音量减 174
824
+ 停止 179
825
+ 静音 173
826
+ 浏览器 172
827
+ 邮件 180
828
+ 搜索 170
829
+ 收藏 171
830
+ **/
831
+ listenKeyboard(target, eventName = "keypress", callback, options) {
832
+ let keyboardEventCallBack = function (event) {
833
+ let keyName = event.key || event.code;
834
+ let keyValue = event.charCode || event.keyCode || event.which;
835
+ let otherCodeList = [];
836
+ if (event.ctrlKey) {
837
+ otherCodeList.push("ctrl");
838
+ }
839
+ if (event.altKey) {
840
+ otherCodeList.push("alt");
841
+ }
842
+ if (event.metaKey) {
843
+ otherCodeList.push("meta");
844
+ }
845
+ if (event.shiftKey) {
846
+ otherCodeList.push("shift");
847
+ }
848
+ if (typeof callback === "function") {
849
+ callback(keyName, keyValue.toString(), otherCodeList, event);
850
+ }
851
+ };
852
+ this.on(target, eventName, keyboardEventCallBack, options);
853
+ return {
854
+ removeListen: () => {
855
+ this.off(target, eventName, keyboardEventCallBack, options);
856
+ },
857
+ };
858
+ }
768
859
  }
769
860
 
770
861
  class DOMUtils extends DOMUtilsEvent {
@@ -772,7 +863,7 @@ define((function () { 'use strict';
772
863
  super(option);
773
864
  }
774
865
  /** 版本号 */
775
- version = "2024.7.24";
866
+ version = "2024.8.30";
776
867
  attr(element, attrName, attrValue) {
777
868
  let DOMUtilsContext = this;
778
869
  if (typeof element === "string") {