www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Multi character mapping the [alt gr] key(?)

  1. #1
    Join Date
    Mar 2010
    Location
    Finland
    Posts
    88

    Lightbulb Multi character mapping the [alt gr] key(?)

    I was wondering if it was possible to program in JavaScript, something that was similar to how cell phones are able to scroll though a character map by the continuous pressing of the same key?

    I was thinking of something like, holding down the [alt gr] key and have a change of letter state every time another key is pressed repeatedly. Most importantly, the specific alternative letters can be coded into the base letter.

    ie. hold down [alt gr] + [standard "n" key]
    = ṅ
    ...press n again
    = ṇ
    ...press n again
    = ñ
    ...
    until [alt gr] is released.

    Does anyone know how to do this, or could someone point me to a good tutorial about how JS deals with key presses?

    Thanks.

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Before anything else, I must remind you that all the browsers have implemented key shortcuts, thus, for instance many combinations which use the ALT key will open some tool bar sub-menus (ALT+C, ALT+S, ALT+T,.... and so on...). So that, even if you might capture these combinations (which is, I said, a quite intricate thing across the browsers), you can not prevent their default behavior.

  3. #3
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,270
    An old but instructive page about this «quite intricate thing» !
    Last edited by 007Julien; 10-18-2010 at 08:03 AM. Reason: I forgot the link

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by 007Julien View Post
    An old but instructive page about this «quite intricate thing» !
    It is intricate for many reasons. The first one is that you need to capture 2 Key codes within the same event: onkeydown. The other one is that for a full crossbrowser code you might need to use other approach in case of Mac browsers to be able to capture some how the ALT key (the property altKey). You may combine those (capturing ALT with altKey, and the other key with onkeydown/onkeyup, but it will not be possible, now, to capture the ALT onkeyup... Isn't that intricate enough?

    Anyway, I have not bothered the OP with all these as, for me, the main obstacle remains the implemented default short keys. Just press ALT+T in Firefox or IE and see what happens.

  5. #5
    Join Date
    Mar 2010
    Location
    Finland
    Posts
    88
    Quote Originally Posted by Kor View Post
    Before anything else, I must remind you that all the browsers have implemented key shortcuts, thus, for instance many combinations which use the ALT key will open some tool bar sub-menus (ALT+C, ALT+S, ALT+T,.... and so on...). So that, even if you might capture these combinations (which is, I said, a quite intricate thing across the browsers), you can not prevent their default behavior.
    Hi, thanks for the feedback. I was actually going to use the [alt gr] (alternative grammar (i think)) to achieve this. AFAIK, Alt gr has no shortcuts mapped to various combinations.
    I could run in to problems because foreign keys are usually mapped when alt gr is pressed. I hope my code will suppress this though.

    Thanks for the link 007Julien. I'll see what I can muster up from this.

  6. #6
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,270
    I wanted only illustrate Kor response (with my bad english language). I agree with his terms. It's ofen easier to read the input (after keyup) than to work work with keycode or keychar (after keypressed or keydown). In this conditions, it's quite impossible to catch digit witout text input (See this experimental page)

  7. #7
    Join Date
    Mar 2010
    Location
    Finland
    Posts
    88
    I managed to get what I wanted (kind of), but every time I press the "alt gr + a", all the typed text in the textarea is reset
    Anyone know a workaround for this?
    Here is my code (Currently only works for google chrome, and only the "a" character has anything mapped to it).
    Code:
    <!DOCTYPE html>
    <html><head><title>KeyBoard Input Test</title><meta charset="utf-8"><script type="text/javascript">
    
    var alt_gr_bool = false; // checks whether the [alt gr] key is pressed or not
    var array_position = 0; // monitors the array position
    
    var foreign_array_A = ["\u3041", "\u30A1"]; // more to be added
    
    document.onkeydown = enable_alt_gr;
    document.onkeyup = reset_alt_gr;
    
    function enable_alt_gr(e) { // What happens when [alt gr] is pressed.
    
    if (e.keyCode == 0)
    {
     alt_gr_bool = true;
    }
    
    var KeyID = (window.event) ? event.keyCode : e.keyCode;
    
    switch(KeyID) {
     case 65: // The letter A/a
      if (alt_gr_bool == true) 
      {
       document.myForm.text_field.value = foreign_array_A[array_position];
       e.preventDefault();
        if (array_position < foreign_array_A.length - 1) 
        {
         array_position ++;
        }
        else 
        {
         array_position = 0;
        }
      }
    break;
     }
    }
    
    function reset_alt_gr(e) { // alt gr key is released. Everything back to normal.
     if (e.keyCode == 0) {
      alt_gr_bool = false;
      array_position = 0;
     }
    }
    
    </script></head><body>
    
    <form name="myForm">
    <textarea name="text_field" rows="10" cols="30"></textarea>
    </form>
    
    </body></html>

  8. #8
    Join Date
    Mar 2010
    Location
    Finland
    Posts
    88
    Ok, finished it!
    It still only works on Google's browser, but I'll find out why someday.

    This version is for Finnish, so only "alt gr" + "a" and "o" keys will give the various characters mapped to them.

    link: http://rebol.vlexo.net/Programming/k...n_keyboard.htm

  9. #9
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    I don't have ALTGR key. It is not a standard key. And I think that 80&#37; of the users don't have it either (keep in mind that the special keyboards with the ALTGR key are rather rare). It is true that on a standard US-International keyboard layout (and similar) the CTRL+ALT could supply the ALTGR functionality. Not in your case. Your code does not work for me (with CTRL+ALT instead of ALTGR), neither in Chrome, nor in any other browser.
    Last edited by Kor; 11-03-2010 at 09:13 AM.

  10. #10
    Join Date
    Mar 2010
    Location
    Finland
    Posts
    88
    Quote Originally Posted by Kor View Post
    I don't have ALTGR key. It is not a standard key. And I think that 80% of the users don't have it either (keep in mind that the special keyboards with the ALTGR key are rather rare). It is true that on a standard US-International keyboard layout (and similar) the CTRL+ALT could supply the ALTGR functionality. Not in your case. Your code does not work for me (with CTRL+ALT instead of ALTGR), neither in Chrome, nor in any other browser.
    That's a pity. I was hoping more people would have it
    Thanks for the info.

  11. #11
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    I must remind you that the Operating System (at least Window, which I commonly use) has the possibility to install different languages and a simple Language bar which can switch, at will, the meaning of some keys in order to return some special characters, characteristic of that foreign language. Of course, if the Font Family used permits that.

    I am also using often that switch, as I am from Romania and our language, even if it is a Latin one, has several special characters. For instance, if I press the key "[" when my Language Bar is set to English, I get this: "[". When I switch to Romanian, I get this:"&#238;".

    So: what would be the use of your toy? To emulate the special characters in the absence of an installed Language Bar? Interesting and ambitious aim. I don't say it is impossible, but could it be really useful? The Romanians who know that behind the "[" key may be found the "&#238;" character usually have installed the Romanian Language Bar on their computer, so that they might type that after a single click on the Language bar (or to use a shortcut - which can even be made as custom).

    Don't mention that if I want to use that character I was told you ("&#238;") and I work on a computer which has no Romanian language installed, I now how to use the Keystroke ASCII combination: ALT+a numbers combination (right side NumPad numbars), thus for "&#238;" i know that I have to keep pressing the ALT key while I type on NumPad: 0238. When I release the ALT, my special character appears.

    http://www.starr.net/is/type/altnum.htm
    Last edited by Kor; 11-03-2010 at 02:10 PM.

  12. #12
    Join Date
    Mar 2010
    Location
    Finland
    Posts
    88
    Quote Originally Posted by Kor View Post
    I must remind you that the Operating System (at least Window, which I commonly use) has the possibility to install different languages and a simple Language bar which can switch, at will, the meaning of some keys in order to return some special characters, characteristic of that foreign language. Of course, if the Font Family used permits that.

    I am also using often that switch, as I am from Romania and our language, even if it is a Latin one, has several special characters. For instance, if I press the key "[" when my Language Bar is set to English, I get this: "[". When I switch to Romanian, I get this:"î".

    So: what would be the use of your toy? To emulate the special characters in the absence of an installed Language Bar? Interesting and ambitious aim. I don't say it is impossible, but could it be really useful? The Romanians who know that behind the "[" key may be found the "î" character usually have installed the Romanian Language Bar on their computer, so that they might type that after a single click on the Language bar (or to use a shortcut - which can even be made as custom).
    I am aware of the language bar. It's very handy to easily switch between character sets. What I was trying to accomplish, was a more temporary/emergency solution to the issue. Perhaps you were using a PC that didn't have the language bar installed and perhaps you did not have user rights to do so. Maybe you are learning an unsupported language, (maybe a constructed language) or you might want to write some phonetic Latin (if it's not supported) for educational reasons.
    The idea could be integrated with language community forum fields for example, but the only reason actually made it was not so serious. I just usually get an idea into my head and like to see how it turns out. I quite like how modern mobile phone UIs handle internationalisation and wanted to see if I could replicate/prototype it in the web browser.
    Thanks for you interest

  13. #13
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by leke View Post
    Maybe you are learning an unsupported language, (maybe a constructed language) or you might want to write some phonetic Latin (if it's not supported) for educational reasons.
    OK. In response - I have had lately added something (maybe you have not seen it, as it was a later thought. Again:

    "Don't mention that if I want to use that character I was told you ("î") and I work on a computer which has no Romanian language installed, I now how to use the Keystroke ASCII combination: ALT+a numbers combination (right side NumPad numbars), thus for "î" i know that I have to keep pressing the ALT key while I type on NumPad: 0238. When I release the ALT, my special character appears."

    So, maybe you may focus on this capturing keystrokes possibility?

  14. #14
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Or: what about some buttons visible on the document. If your aim is to use form's elements like a text field or a textarea. Could be something like in the editor if this forum: when pressing a button, you can emulate a BOLD or ITALIC. This method is not so intricate, and it can be used to insert special characters. Except that you should have in the backstage a complete "dictionary". As I said, in Romanian language bar on, when I press [ I obtain î. You may simply capture the keys and, according to a switch button (on/off) you may replace the typed character with it's corresponded in that dictionary. I'll think about that, if I have some time...

  15. #15
    Join Date
    Mar 2010
    Location
    Finland
    Posts
    88
    Quote Originally Posted by Kor View Post
    Or: what about some buttons visible on the document. If your aim is to use form's elements like a text field or a textarea. Could be something like in the editor if this forum: when pressing a button, you can emulate a BOLD or ITALIC. This method is not so intricate, and it can be used to insert special characters. Except that you should have in the backstage a complete "dictionary". As I said, in Romanian language bar on, when I press [ I obtain î. You may simply capture the keys and, according to a switch button (on/off) you may replace the typed character with it's corresponded in that dictionary. I'll think about that, if I have some time...
    Yeah, this is a pretty good idea. It's a little like changing the language on the touch screen phones. If there are more than 2 characters mapped to one key though, it could get a bit tricky to implement, but I guess it would work really well on some languages.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles