rudminda
rudminda

Reputation: 147

Enable special characters in form input

I'm looking for a (preferrably HTML or Javascript) way to enable my users to place their cursor into any one of my many text input-fields, and type "CTRL-SHIFT-A," and have it display a macron-A entity ("Ā", also coded as Ā). I've already figured out a Javascript way to recognize when "CTRL-SHIFT-A" is pressed, but I just don't know how to display "Ā".


Javascript

    <script>

       window.addEventListener("keydown", keysPressed, false);
       window.addEventListener("keyup", keysReleased, false);

       var keys = [];

       function keysPressed(e) {
             keys[e.keyCode] = true;
             if (keys[17] && keys[16] && keys[65]) {

           /*The solution should probably go here.*/

             e.preventDefault(); 
          }
        }

    function keysReleased(e) {
        keys[e.keyCode] = false;
    };

    </script>

HTML

<input style="width: 55px;" onkeyup="lettersOnly(this);"></input>

Charset is "windows-1252" and it's encoded as "UTF-8 with BOM."

Upvotes: 1

Views: 2279

Answers (1)

sal
sal

Reputation: 3593

You can use String.fromCharCode() once you find the code of your special character. I looked up your character and found details on Wikipedia, and in there I used the Unicode value, which is 256. So here an example:

alert(String.fromCharCode(256))

To go on the "how to place the string into the input control that has focus" you could modify the function as follows:

function keysPressed(e) {
         keys[e.keyCode] = true;
         if (keys[17] && keys[16] && keys[65]) {
           // e.target.value is the read/write property of the
           // input field that received the keyboard sequence
           e.target.value=String.fromCharCode(256);
           e.preventDefault(); 
         }
}

See it in action at: https://jsfiddle.net/kn3ko9oy/

Upvotes: 1

Related Questions