I am trying to make a function that will take a character, which is the value of the key pressed on the keyboard, convert it to another special character, and insert it into a particular input field (something like simulating another keyboard layout).
Example:
- User type x on keyboard
- The function takes this character and converts it into ḫ
- Than function insert this character into a particular input field
I made a function which converts those special characters:
$("input").keydown(function(e){
var keyCharacter = e.key;
});
var replaceFrom = [/A/, /i/, /a/, /s/, /z/, /S/, /q/, /H/, /x/, /X/, /T/, /D/];
var replaceTo = ["ꜣ", "j", "ꜥ", "s", "z", "š", "q", "ḥ", "ḫ", "ẖ", "ṯ", "ḏ"];
function transliterationConverter(user_input, transliteration_schema_to_replace, transliteration_schema_for_replace) {
for (i = 0; i < transliteration_schema_to_replace.length; i++) {
user_input = user_input.replace(new RegExp(transliteration_schema_to_replace[i], "g"), transliteration_schema_for_replace[i]);
}
return user_input;
}
var convertedKeyCharacter = transliterationConverter(keyCharacter, replaceFrom, replaceTo);
// Character typed by user: x
// Desired result in input field: ḫ
P.S. I am not happy with a solution that includes taking value from the input field and then converts its characters to other ones, because if that happening, the user can see the "conversion process" that is happening in half of a second.