|
3 | 3 | <head>
|
4 | 4 | <meta charset="UTF-8">
|
5 | 5 | <title>Key Detection</title>
|
6 |
| - <script type="text/javascript" src="http://www.cornify.com/js/cornify.js"></script> |
| 6 | + <script type="text/javascript" src="https://www.cornify.com/js/cornify.js"></script> |
7 | 7 | </head>
|
8 | 8 | <body>
|
| 9 | + <h1>Press Enter</h1> |
9 | 10 | <script>
|
| 11 | + const pressed = [], |
| 12 | + str = 'marco', |
| 13 | + strArr = [...str.split(''), 'S']; |
| 14 | + |
| 15 | + window.addEventListener('keyup', (e) => { |
| 16 | + const strLength = str.length; |
| 17 | + if ( 'Enter' == e.key ) { |
| 18 | + rainUnicorn() |
| 19 | + } |
| 20 | + pressed.push(e.key); |
| 21 | + console.groupCollapsed() |
| 22 | + console.log( 'e.key: ', e.key ); |
| 23 | + console.log( 'strLength: ', strLength ); |
| 24 | + console.log( 'pressed.length: ', pressed.length ); |
| 25 | + let splice = pressed.splice(-strLength -1, pressed.length - strLength), |
| 26 | + joined = pressed.join(''); |
| 27 | + console.log( 'pressed.length after splice: ', pressed.length ); |
| 28 | + console.log( 'splice: ', splice ); |
| 29 | + console.log( 'joined: ', joined ); |
| 30 | + console.groupEnd() |
| 31 | + if ( joined.includes(str) ) { |
| 32 | + cornify_add(); |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + function unicorn(arr) { |
| 37 | + arr |
| 38 | + .map(char => ({ code : char.charCodeAt(), char : char }) ) |
| 39 | + .forEach(key => window.dispatchEvent ( |
| 40 | + new KeyboardEvent('keyup', { 'keyCode':key.code, 'key':key.char }) |
| 41 | + )); |
| 42 | + } |
| 43 | + function rainUnicorn() { |
| 44 | + let arr = []; |
| 45 | + for(var i = 0; i < 88; i++){ |
| 46 | + arr = [...arr, ...strArr]; |
| 47 | + } |
| 48 | + unicorn(arr); |
| 49 | + } |
| 50 | + |
| 51 | + unicorn(strArr); |
| 52 | + |
10 | 53 | </script>
|
11 | 54 | </body>
|
12 | 55 | </html>
|
0 commit comments