Search code examples
javascriptnode.jsutf-8asciikeycode

Keycode / encoding problem for cyrrilic letters in key send operations


I am facing a very rare problem, that I couldn't solve myself even after hours of googling it.

I use node-key-sender (but the problem is similar for other look-a-like modules, such as robots.js) for keyboard emittance (Keyboard button send operations)

The following function works fine when I am using any English word:

const ks = require('node-key-sender');

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

(async function query (query = 'Bread') {
    try {
        query = query.toLowerCase()
        await sleep(1000)
        ks.sendText(query); //prints bread in real time after 1 second delay
    } catch (e) {
        console.error(e)
    }
})();

But when I am trying to use it for any Cyrillic word, it does nothing, because it doesn't contain any «alphabet map», but If I add it, it works fine, but...

const ks = require('node-key-sender');

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

const RuMap = {
    "й": 'q', "ц": 'w', "у": 'e', "к": 'r', "е": 't', "н": 'y', "г": 'u', "ш": 'i', "щ": 'o', "з": 'p', "х": 'shift-@55', "ъ": 'close_bracket',
    "ф": 'a', "ы": 's', "в": 'd', "а": 'f', "п": 'g', "р": 'h', "о": 'j', "л": 'k', "д": 'l', "ж": ';', "э": "'",
    "я": 'z', "ч": 'x', "с": 'c', "м": 'v', "и": 'b', "т": 'n', "ь": 'm', "б": '', "ю": 'slash',
};

(async function query (query = 'Хлеб') {
    try {
        query = query.toLowerCase()
        ks.aggregateKeyboardLayout(RuMap);
        await sleep(1000)
        ks.sendText(query); //prints only 'л' and 'е'
    } catch (e) {
        console.error(e)
    }
})();

The problem is, that if I want to print any non-English word, I should change language keyboard in my OS, and run this function, so the Cyrillic letters have been converted to the English counterparts, like Q => Й, according to language layout map: enter image description here

Example:

So, if I want to print the word ЙЦУКЕН I should convert every letter for QWERTY, and machine prints emit the following key, and prints ЙЦУКЕН, because my language layout is Cyrillic right now in the OS.

enter image description here

And it works fine, until I don't face the cyrrilic Х => [ problem. Open bracket has KeyCode @219 or VK_OPEN_BRACKET (in Java KeyEvent). But even if I add to layout { "х": '@219' } or { "х": 'open_bracket' } it still doesn't work.

As I figure it out, KeySend somehow changes or it does relevant with the non-ASCI symbols problem.

So in Cyrillic layout, shift+@55 KeyCode is relevant to ?, but in the English layout ? doesn't have keyCode @55 so, how to find KeyCodes for Cyrillic layout and solve this problem?


Solution

  • Unicode Subtrange with Character Map helps me out after 4 hours of coding:

    enter image description here

    "х": '@18-@96-@98-@100-@101' which equals to Alt+0245 for all characters, which doesn't have KeyCodes in active OS layout.