Can you modify this script so it also turns the letters into keycodes? function…
- User
Can you modify this script so it also turns the letters into keycodes?
function ahkToKeyCodes(ahkScript) { // Split the script into pairs const pairs = ahkScript.match(/{\d+ \d+}/g);
let str = ''; pairs.forEach(pair => { // Extract the numbers from each pair const [char, times] = pair.match(/\d+/g).map(Number);
// Repeat the first number by the second number of times str += String(char).repeat(times);});
// Convert the string into keycodes const keyCodes = []; for (let i = 0; i < str.length; i++) { keyCodes.push(str.charCodeAt(i)); }
return keyCodes.join(','); }
console.log(ahkToKeyCodes('vui{1 2}{2 3}{3 2}{4 6}{5 5}{6 7}{7 9}{8 4}{9 1}{0 3}'));
- Assistant
I've noticed that the original script has a regex pattern that seems to be looking for pairs within curly braces, indicating a digit repeated a specific number of times. However, it doesn't account for literal characters like the 'vui' at the beginning of the provided input, which should also be converted into keycodes.
Here's a modified version of the script which includes support for individual characters and turns them into keycodes:
function ahkToKeyCodes(ahkScript) { // This pattern now also matches single characters outside of pairs const matches = ahkScript.match(/({\d+ \d+})|./g); let keyCodes = []; matches.forEach(match => { if (match.startsWith('{') && match.endsWith('}')) { // Extract the numbers from each pair within the curly braces const [charCode, times] = match.slice(1, -1).split(' ').map(Number); // Repeat the keycode the specified number of times for (let i = 0; i < times; i++) { keyCodes.push(charCode); } } else { // Convert each character to its keycode equivalent keyCodes.push(match.charCodeAt(0)); } }); return keyCodes.join(','); } console.log(ahkToKeyCodes('vui{1 2}{2 3}{3 2}{4 6}{5 5}{6 7}{7 9}{8 4}{9 1}{0 3}')); - User
I got this output... 118,117,105,1,1,2,2,2,3,3,4,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,8,8,8,8,9,0,0,0
- Assistant
It appears I made a mistake in my explanation. The function should not convert characters like 'v', 'u', 'i' directly into keycodes using their character codes; it should convert them into the keycodes corresponding to their positions on a standard keyboard.
The AHK script you provided suggests that the numbers within the curly braces are actually meant to represent the ASCII values of keyboard characters (e.g., '1', '2', '3', etc.), and the subsequent number represents how many times they should be repeated.