@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.
@@ -508,4 +508,70 @@ export declare class DOMUtilsEvent {
508
508
  * })
509
509
  */
510
510
  keypress(target: HTMLElement | Window | typeof globalThis | string, handler: (event: DOMUtils_Event["keypress"]) => void, option?: boolean | AddEventListenerOptions): void;
511
+ /**
512
+ * 监听某个元素键盘按键事件或window全局按键事件
513
+ * 按下有值的键时触发,按下Ctrl\Alt\Shift\Meta是无值键。按下先触发keydown事件,再触发keypress事件。
514
+ * @param target 需要监听的对象,可以是全局Window或者某个元素
515
+ * @param eventName 事件名,默认keypress
516
+ * @param callback 自己定义的回调事件,参数1为当前的key,参数2为组合按键,数组类型,包含ctrl、shift、alt和meta(win键或mac的cmd键)
517
+ * @param options 监听事件的配置
518
+ * @example
519
+ Utils.listenKeyboard(window,(keyName,keyValue,otherKey,event)=>{
520
+ if(keyName === "Enter"){
521
+ console.log("回车按键的值是:"+keyValue)
522
+ }
523
+ if(otherKey.indexOf("ctrl") && keyName === "Enter" ){
524
+ console.log("Ctrl和回车键");
525
+ }
526
+ })
527
+ * @example
528
+ 字母和数字键的键码值(keyCode)
529
+ 按键 键码 按键 键码 按键 键码 按键 键码
530
+ A 65 J 74 S 83 1 49
531
+ B 66 K 75 T 84 2 50
532
+ C 67 L 76 U 85 3 51
533
+ D 68 M 77 V 86 4 52
534
+ E 69 N 78 W 87 5 53
535
+ F 70 O 79 X 88 6 54
536
+ G 71 P 80 Y 89 7 55
537
+ H 72 Q 81 Z 90 8 56
538
+ I 73 R 82 0 48 9 57
539
+
540
+ 数字键盘上的键的键码值(keyCode)
541
+ 功能键键码值(keyCode)
542
+ 按键 键码 按键 键码 按键 键码 按键 键码
543
+ 0 96 8 104 F1 112 F7 118
544
+ 1 97 9 105 F2 113 F8 119
545
+ 2 98 * 106 F3 114 F9 120
546
+ 3 99 + 107 F4 115 F10 121
547
+ 4 100 Enter 108 F5 116 F11 122
548
+ 5 101 - 109 F6 117 F12 123
549
+ 6 102 . 110
550
+ 7 103 / 111
551
+
552
+ 控制键键码值(keyCode)
553
+ 按键 键码 按键 键码 按键 键码 按键 键码
554
+ BackSpace 8 Esc 27 → 39 -_ 189
555
+ Tab 9 Spacebar 32 ↓ 40 .> 190
556
+ Clear 12 Page Up 33 Insert 45 /? 191
557
+ Enter 13 Page Down 34 Delete 46 `~ 192
558
+ Shift 16 End 35 Num Lock 144 [{ 219
559
+ Control 17 Home 36 ;: 186 \| 220
560
+ Alt 18 ← 37 =+ 187 ]} 221
561
+ Cape Lock 20 ↑ 38 ,< 188 '" 222
562
+
563
+ 多媒体键码值(keyCode)
564
+ 按键 键码
565
+ 音量加 175
566
+ 音量减 174
567
+ 停止 179
568
+ 静音 173
569
+ 浏览器 172
570
+ 邮件 180
571
+ 搜索 170
572
+ 收藏 171
573
+ **/
574
+ listenKeyboard(target: Window | Node | HTMLElement | typeof globalThis, eventName: ("keyup" | "keypress" | "keydown") | undefined, callback: (keyName: string, keyValue: string, otherCodeList: string[], event: KeyboardEvent) => void, options?: AddEventListenerOptions | boolean): {
575
+ removeListen(): void;
576
+ };
511
577
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whitesev/domutils",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "使用js重新对jQuery的部分函数进行了仿写",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",
package/src/DOMUtils.ts CHANGED
@@ -11,7 +11,7 @@ class DOMUtils extends DOMUtilsEvent {
11
11
  super(option);
12
12
  }
13
13
  /** 版本号 */
14
- version = "2024.7.24";
14
+ version = "2024.8.30";
15
15
  /**
16
16
  * 获取元素的属性值
17
17
  * @param element 目标元素
@@ -1256,4 +1256,108 @@ export class DOMUtilsEvent {
1256
1256
  }
1257
1257
  DOMUtilsContext.on(target, "keypress", null, handler, option);
1258
1258
  }
1259
+
1260
+ /**
1261
+ * 监听某个元素键盘按键事件或window全局按键事件
1262
+ * 按下有值的键时触发,按下Ctrl\Alt\Shift\Meta是无值键。按下先触发keydown事件,再触发keypress事件。
1263
+ * @param target 需要监听的对象,可以是全局Window或者某个元素
1264
+ * @param eventName 事件名,默认keypress
1265
+ * @param callback 自己定义的回调事件,参数1为当前的key,参数2为组合按键,数组类型,包含ctrl、shift、alt和meta(win键或mac的cmd键)
1266
+ * @param options 监听事件的配置
1267
+ * @example
1268
+ Utils.listenKeyboard(window,(keyName,keyValue,otherKey,event)=>{
1269
+ if(keyName === "Enter"){
1270
+ console.log("回车按键的值是:"+keyValue)
1271
+ }
1272
+ if(otherKey.indexOf("ctrl") && keyName === "Enter" ){
1273
+ console.log("Ctrl和回车键");
1274
+ }
1275
+ })
1276
+ * @example
1277
+ 字母和数字键的键码值(keyCode)
1278
+ 按键 键码 按键 键码 按键 键码 按键 键码
1279
+ A 65 J 74 S 83 1 49
1280
+ B 66 K 75 T 84 2 50
1281
+ C 67 L 76 U 85 3 51
1282
+ D 68 M 77 V 86 4 52
1283
+ E 69 N 78 W 87 5 53
1284
+ F 70 O 79 X 88 6 54
1285
+ G 71 P 80 Y 89 7 55
1286
+ H 72 Q 81 Z 90 8 56
1287
+ I 73 R 82 0 48 9 57
1288
+
1289
+ 数字键盘上的键的键码值(keyCode)
1290
+ 功能键键码值(keyCode)
1291
+ 按键 键码 按键 键码 按键 键码 按键 键码
1292
+ 0 96 8 104 F1 112 F7 118
1293
+ 1 97 9 105 F2 113 F8 119
1294
+ 2 98 * 106 F3 114 F9 120
1295
+ 3 99 + 107 F4 115 F10 121
1296
+ 4 100 Enter 108 F5 116 F11 122
1297
+ 5 101 - 109 F6 117 F12 123
1298
+ 6 102 . 110
1299
+ 7 103 / 111
1300
+
1301
+ 控制键键码值(keyCode)
1302
+ 按键 键码 按键 键码 按键 键码 按键 键码
1303
+ BackSpace 8 Esc 27 → 39 -_ 189
1304
+ Tab 9 Spacebar 32 ↓ 40 .> 190
1305
+ Clear 12 Page Up 33 Insert 45 /? 191
1306
+ Enter 13 Page Down 34 Delete 46 `~ 192
1307
+ Shift 16 End 35 Num Lock 144 [{ 219
1308
+ Control 17 Home 36 ;: 186 \| 220
1309
+ Alt 18 ← 37 =+ 187 ]} 221
1310
+ Cape Lock 20 ↑ 38 ,< 188 '" 222
1311
+
1312
+ 多媒体键码值(keyCode)
1313
+ 按键 键码
1314
+ 音量加 175
1315
+ 音量减 174
1316
+ 停止 179
1317
+ 静音 173
1318
+ 浏览器 172
1319
+ 邮件 180
1320
+ 搜索 170
1321
+ 收藏 171
1322
+ **/
1323
+ listenKeyboard(
1324
+ target: Window | Node | HTMLElement | typeof globalThis,
1325
+ eventName: "keyup" | "keypress" | "keydown" = "keypress",
1326
+ callback: (
1327
+ keyName: string,
1328
+ keyValue: string,
1329
+ otherCodeList: string[],
1330
+ event: KeyboardEvent
1331
+ ) => void,
1332
+ options?: AddEventListenerOptions | boolean
1333
+ ): {
1334
+ removeListen(): void;
1335
+ } {
1336
+ let keyboardEventCallBack = function (event: KeyboardEvent) {
1337
+ let keyName = event.key || event.code;
1338
+ let keyValue = event.charCode || event.keyCode || event.which;
1339
+ let otherCodeList = [];
1340
+ if (event.ctrlKey) {
1341
+ otherCodeList.push("ctrl");
1342
+ }
1343
+ if (event.altKey) {
1344
+ otherCodeList.push("alt");
1345
+ }
1346
+ if (event.metaKey) {
1347
+ otherCodeList.push("meta");
1348
+ }
1349
+ if (event.shiftKey) {
1350
+ otherCodeList.push("shift");
1351
+ }
1352
+ if (typeof callback === "function") {
1353
+ callback(keyName, keyValue.toString(), otherCodeList, event);
1354
+ }
1355
+ };
1356
+ this.on(target, eventName, keyboardEventCallBack as any, options);
1357
+ return {
1358
+ removeListen: () => {
1359
+ this.off(target, eventName, keyboardEventCallBack, options);
1360
+ },
1361
+ };
1362
+ }
1259
1363
  }