multiKeys

为脚本提供快捷键

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/473644/1238976/multiKeys.js

  1. class multiKeys {
  2. constructor() {
  3.  
  4. this.Created = "2023/1/14 17:14";
  5. this.last_modified = "2023/08/22 12:33:38";
  6. this.author = 'leizingyiu';
  7. console.log(`multiKeys by ${this.author}; created ${this.Created} ; last modified ${this.last_modified}`);
  8.  
  9. var that = this;
  10. this.ignore = false;
  11. this.ignoreList = [];
  12. this.ignoreDict = [];
  13.  
  14. this.console = function () {
  15. console.log(...arguments);
  16. };
  17. this.registration = {};
  18. this.allKeysArr = [];
  19. this.keys = [];
  20. this.waterKeys = [];
  21.  
  22. this.tokenJoining = '_';
  23. this.tokenSortFunction = (_a, _b) => {
  24. let a = String(_a),
  25. b = String(_b);
  26. if (a.length != b.length) {
  27. return a.length - b.length;
  28. } else {
  29. return a.localeCompare(b);
  30. }
  31. };
  32. this.tokenFn = (arr) => arr.sort(this.tokenSortFunction).join(this.tokenJoining);
  33. this.evKey = (ev) => {
  34. console.log(ev, ev.key);
  35.  
  36. let result = ev.key.length == 1 || ev.key.toLowerCase() == 'dead' ?
  37. ev.code.toLowerCase().replace('key', '').replace('digit', '') :
  38. ev.key.toLowerCase();
  39. that.console(ev.type, ev, result);
  40. return result;
  41. };
  42.  
  43. window.addEventListener('keydown', function (ev) {
  44. if (typeof ev.key == 'undefined') { return false; }
  45. let k = that.evKey(ev);
  46.  
  47. /*ignore start */
  48. if (that.ignoreDict.includes(k)) {
  49. that.console('ignore ' + k);
  50. that.ignore = true;
  51. that.ignoreList.push(k);
  52. that.ignoreList = [...new Set(that.ignoreList)];
  53. return false;
  54. }
  55.  
  56. if (that.ignore == true) {
  57. that.console('ignore true: ', that.ignoreList);
  58. return false;
  59. }
  60. /*ignore end */
  61.  
  62.  
  63. that.keys.push(k);
  64. that.keys = [...new Set(that.keys)];
  65.  
  66. let hitKeys = that.keys.filter((key) => that.allKeysArr.includes(key)),
  67. hitToken = that.tokenFn(hitKeys);
  68.  
  69. that.console('keydown', '\n\t',
  70. ev, k, '\n\t',
  71. hitKeys, hitToken, '\n\t',
  72. that);
  73.  
  74. Object.entries(that.registration).map(o => {
  75. let [token, setting] = o;
  76. if (hitToken == token && (!that.waterKeys.includes(k))) {
  77. setting.callback(that.keys, ev);
  78. that.console('down', token, ' run callback ');
  79. if (setting.fireBoolean == false) {
  80. // that.keys = that.keys.filter(key => key != k);
  81. that.waterKeys.push(k);
  82. that.waterKeys = [... new Set(that.waterKeys)];
  83. that.console('down, add ', k, ' to ', that.waterKeys, that);
  84. }
  85. }
  86. });
  87.  
  88. });
  89.  
  90. window.addEventListener("keyup", function (ev) {
  91. if (typeof ev.key == 'undefined') { return false; }
  92.  
  93. let hitKeys_before = [...that.keys.filter((key) => that.allKeysArr.includes(key))],
  94. hitToken_before = String(that.tokenFn(hitKeys_before));
  95.  
  96. let k = that.evKey(ev);
  97. that.keys = that.keys.filter((_k) => _k != k);
  98.  
  99. /*ignore start */
  100. if (that.ignoreDict.includes(k)) {
  101. that.console('ignore release :' + k);
  102.  
  103. that.ignoreList = that.ignoreList.filter((_k) => _k != k);
  104.  
  105. if (that.ignoreList.length == 0) {
  106. that.ignore = false;
  107. }
  108.  
  109. return false;
  110. }
  111.  
  112. if (that.ignore == true) {
  113. that.console('ignore true: ', that.ignoreList);
  114. return false;
  115. }
  116. /*ignore end */
  117.  
  118.  
  119. let hitKeys_after = [...that.keys.filter((key) => that.allKeysArr.includes(key))],
  120. hitToken_after = String(that.tokenFn(hitKeys_after));
  121.  
  122. that.console('keyup', '\n\t',
  123. ev, k, '\n\t',
  124. hitKeys_before, hitToken_before, '\n\t',
  125. hitKeys_after, hitToken_after, '\n\t',
  126. that);
  127.  
  128.  
  129. Object.entries(that.registration).map(o => {
  130.  
  131. let [token, setting] = o;
  132.  
  133. if (hitToken_before == token &&
  134. hitKeys_before != hitToken_after
  135. ) {
  136.  
  137. if (typeof setting.releaseCallback === 'function') {
  138. that.console('up', token, 'run release callback');
  139. setting.releaseCallback(that.keys, ev);
  140. }
  141.  
  142.  
  143.  
  144. }
  145.  
  146. if (setting.fireBoolean == false &&
  147. that.waterKeys.includes(k) &&
  148. setting.keysArr.includes(k)
  149. ) {
  150. that.waterKeys = that.waterKeys.filter(key => key != k);
  151. that.console('up, remove ', k, ' from ', that.waterKeys, that);
  152. }
  153.  
  154. });
  155. });
  156. }
  157.  
  158. registerIgnore() {
  159. [...arguments].map(k => {
  160. if (typeof k != 'string') {
  161. console.error('register ignore dict error: ', k, ' is not a string');
  162. return false;
  163. }
  164. this.ignoreDict.push(k);
  165. });
  166. this.ignoreDict = [...new Set(this.ignoreDict)];
  167. };
  168.  
  169. register(keysArr, callback, releaseCallback = () => { }, fireBoolean = false, coverBoo = false) {
  170. keysArr = [...new Set(keysArr)];
  171. const keyToken = this.tokenFn(keysArr);
  172. this.allKeysArr.push(...keysArr);
  173. this.allKeysArr = [...new Set(this.allKeysArr)];
  174.  
  175. let keysTocken = this.tokenFn(keysArr);
  176. if (!(keysTocken in this.registration) || coverBoo == true) {
  177. this.registration[keysTocken] = {
  178. 'keysArr': keysArr,
  179. 'callback': callback,
  180. 'releaseCallback': releaseCallback,
  181. 'fireBoolean': fireBoolean
  182. };
  183. } else {
  184. this.console.error(`${keysArr} has been registered: ${this.registration[keysTocken]}`);
  185. }
  186. };
  187. coverRegister(keysArr, callback, releaseCallback = () => { }, fireBoolean = false) {
  188. register(keysArr, callback, releaseCallback, fireBoolean, coverBoo = true);
  189. }
  190.  
  191. }
  192.  
  193. // const m = new multiKeys();
  194. // m.registerIgnore('control', 'meta');
  195. // m.register(['alt', 'a'],
  196. // () => {
  197. // console.log('a')
  198. // },
  199. // () => {
  200. // console.log('release a')
  201. // },
  202. // false);
  203.  
  204. // m.register(['alt', 'b'],
  205. // () => {
  206. // console.log('b')
  207. // },
  208. // () => {
  209. // console.log('release b')
  210. // },
  211. // true);
  212.  
  213. // by leizingyiu

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址