Skip to content

Commit 4c00480

Browse files
committed
feat: Use charCodeGenerator() generator function (#4)
1 parent 1f049e5 commit 4c00480

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

html/charkey.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ <h1>Character key shortcuts</h1>
2121
{
2222
"imports": {
2323
"charKeyBookmarklet": "../lib/characterKeyShortcuts.esm.marklet.js",
24+
"charCodeGenerator": "../lib/util/charCodeGenerator.js",
2425
"ndf-bookmarklets": "../index.js",
2526
"my-elements": "https://nfreear.github.io/elements/i.js"
2627
},
@@ -33,10 +34,13 @@ <h1>Character key shortcuts</h1>
3334
<script type="module">
3435
import 'my-elements';
3536
import { characterKeyShortcutsBookmarklet } from 'charKeyBookmarklet';
37+
import charCodeGenerator from 'charCodeGenerator';
3638

3739
const bookmarkletLinkElem = document.querySelector('my-bookmarklet');
3840

3941
bookmarkletLinkElem.fromFunction(characterKeyShortcutsBookmarklet);
42+
43+
console.debug('Generator:', charCodeGenerator.prototype);
4044
</script>
4145

4246
</html>

lib/characterKeyShortcuts.esm.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
/**
22
*/
3+
import charCodeGenerator from './util/charCodeGenerator.js';
34
import makeAsyncCharCodeIterator from './util/charCodeIterator.js';
45

56
const { KeyboardEvent } = window;
67

7-
export async function characterKeyShortcuts () {
8+
export function characterKeyShortcuts () {
9+
[...charCodeGenerator()].forEach(async (num) => {
10+
console.log(`>* ${num}: ${String.fromCharCode(num)}`);
11+
fireKBEvent('keydown', num);
12+
fireKBEvent('keypress', num);
13+
fireKBEvent('keyup', num);
14+
await delay(20);
15+
});
16+
}
17+
18+
export async function characterKeyShortcutsIter () {
819
const IT = makeAsyncCharCodeIterator();
920
let RES = await IT.next();
1021

@@ -17,6 +28,10 @@ export async function characterKeyShortcuts () {
1728
}
1829
}
1930

31+
async function delay (delayMs) {
32+
return new Promise((resolve) => setTimeout(() => resolve(), delayMs));
33+
}
34+
2035
export function fireKBEvent (evName, num) {
2136
const ev = new KeyboardEvent(evName, {
2237
key: String.fromCharCode(num),

lib/characterKeyShortcuts.esm.marklet.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
export function characterKeyShortcutsBookmarklet () {
1212
(async () => {
13-
const MOD = await import('{__ORIGIN__}/lib/characterKeyShortcuts.js');
13+
const MOD = await import('{__ORIGIN__}/lib/characterKeyShortcuts.esm.js');
1414
const { characterKeyShortcuts } = MOD;
1515
console.debug('Character Key Shortcuts:', MOD);
1616
await characterKeyShortcuts();

lib/util/charCodeGenerator.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* A character code generator.
3+
* 32 — 126 (inclusive),
4+
* 161 — 191 (inclusive),
5+
* 8364 (€)
6+
* @see https://jsdev.space/js-generators-guide/
7+
*/
8+
export function * charCodeGenerator () {
9+
for (let i = 32; i < 127; i++) {
10+
yield i;
11+
}
12+
for (let k = 161; k <= 191; k++) {
13+
yield k;
14+
}
15+
yield 8364;
16+
}
17+
18+
export default charCodeGenerator;

0 commit comments

Comments
 (0)