3

I need a way to convert the a string, or a char, to it's equivalent on the keyboard on a different language. and as such, in hebrew, for example, "ש" will become "a" or "A".

Couldn't find the proper way to do so without creating a giant switch statement or a dictionary.

any answer on either JS or C# will be great.

Sahar Zehavi
  • 580
  • 6
  • 16
  • 7
    _Either_ JS or C#? That’s weird… Anyway, in JavaScript you can create an object like this: `var letters={"ש":"a", "etc.":"b",…}` and so on, then use `letters["ש"]` to get `"a"`. – Sebastian Simon Aug 26 '15 at 08:16
  • 1
    @Xufox The equivalent in C# can be a dictionary – TheLethalCoder Aug 26 '15 at 08:19
  • well, in my app, i can write server side code in JS and in C# (using V8) so if it's easier to answer it on one or the other, then there's no need in putting up constrains. – Sahar Zehavi Aug 26 '15 at 08:19
  • Refer these stackoveflow answers, http://stackoverflow.com/questions/2041859/javascript-how-to-find-hebrew – Saba Aug 26 '15 at 08:20
  • 1
    This is a more complex task than I think you realize. I believe the best way would be to provide a mapping for each character in a multidimensional array or dictionary and implement your own function (JS) or method (C#) to perform translation. – Marcus Aug 26 '15 at 08:20
  • ya, i want to avoid that, if possible. – Sahar Zehavi Aug 26 '15 at 08:20
  • @SaharZehavi http://www.greywyvern.com/code/javascript/keyboard – Eser Aug 26 '15 at 08:46

3 Answers3

2

Use keyCode of the keydown or keyup events. Property of this event contains code of keyboard key but not code of symbol.

document.addEventListener("keyup", function(event){
  console.log(String.fromCharCode(event.keyCode));
});
vihtor
  • 265
  • 2
  • 10
  • I need a way to convert a given string (or char) and i can't access the keyboard event that generated the key. – Sahar Zehavi Aug 26 '15 at 08:41
  • 2
    if there is only a line, you want to convert (and no other useful information on how this line has been received), it can only be done dictionaries. By itself, the symbol has nothing to do with the key code keyboard – vihtor Aug 26 '15 at 09:00
2

here is how i do it in javascript

qwerty_mapping = {
"ת":"," ,",":"ת"
,"ף":";"    ,";":"ף"
,"ץ":"."    ,".":"ץ"
,"ש":"a"    ,"a":"ש"
,"נ":"b"    ,"b":"נ"
,"ב":"c"    ,"c":"ב"
,"ג":"d"    ,"d":"ג"
,"ק":"e"    ,"e":"ק"
,"כ":"f"    ,"f":"כ"
,"ע":"g"    ,"g":"ע"
,"י":"h"    ,"h":"י"
,"ן":"i"    ,"i":"ן"
,"ח":"j"    ,"j":"ח"
,"ל":"k"    ,"k":"ל"
,"ך":"l"    ,"l":"ך"
,"צ":"m"    ,"m":"צ"
,"מ":"n"    ,"n":"מ"
,"ם":"o"    ,"o":"ם"
,"פ":"p"    ,"p":"פ"
,"/":"q"    ,"q":"/"
,"ר":"r"    ,"r":"ר"
,"ד":"s"    ,"s":"ד"
,"א":"t"    ,"t":"א"
,"ו":"u"    ,"u":"ו"
,"ה":"v"    ,"v":"ה"
,"'":"w"    ,"w":"'"
,"ס":"x"    ,"x":"ס"
,"ט":"y"    ,"y":"ט"
}
function correctChar(old_char){
if(qwerty_mapping[old_char] == undefined)
    {
        return(old_char)
    } 
    else
    {
        return(qwerty_mapping[old_char])
    }
}

function correctString(old_string){
new_string = ""
for(i=0;i<old_string.length;i++){
    new_string = new_string+correctChar(old_string[i])
}
return new_string;
}
Itay
  • 21
  • 2
1

in case someone will look for this answer, It seems like that isn't a clear way of doing that without using a dictionary or a switch statement.

basa.

Sahar Zehavi
  • 580
  • 6
  • 16
  • I see I'm not the only one who got frustrated enough by typing out. a whole paragraph before realizing I had Hebrew selected instead of English :) – Yechiel K Mar 30 '22 at 20:41