/* * Javascript Thaana Keyboard 3.0 * * Copyright (c) 2008 Jawish Hameed (jawish.org) * Licensed under the MIT (MIT-LICENSE.txt) license. */ var thaanaKeyboard = { thaanaClassName: "thaanaKeyboardInput", /** * Set key event handlers for the required elements in page */ setHandlers: function () { // Setup regular expression for finding the target class var rePat = new RegExp('\\b' + this.thaanaClassName + '\\b'); // Get all the elements on page var elemList = document.getElementsByTagName('*'); // Loop through the elements for (i = 0; i < elemList.length; i++) { // Ensure the current element is of the target class if (rePat.test(elemList[i].className)) { // Set right-to-left text direction elemList[i].style.direction = 'rtl'; // Add event handlers to monitor key events elemList[i].onkeypress = this.handleKey; } } }, /** * Do character translation on key activity */ handleKey: function (e) { // Get the event data if (!e) var e = window.event; // Get the pressed key var keycode = (e.which) ? e.which : e.keyCode; // Check for CTRL modifier key if (e.modifier) { var ctrl = e.modifiers & Event.CONTROL_MASK; } else if (typeof(e.ctrlKey) != 'undefined') { var ctrl = e.ctrlKey; } // Setup Ascii Thaana -> Unicode translation matrix var transArray = { 'h': '\u0780', 'S': '\u0781', 'n': '\u0782', 'r': '\u0783', 'b': '\u0784', 'L': '\u0785', 'k': '\u0786', 'w': '\u0787', 'v': '\u0788', 'm': '\u0789', 'f': '\u078A', 'd': '\u078B', 't': '\u078C', 'l': '\u078D', 'g': '\u078E', 'N': '\u078F', 's': '\u0790', 'D': '\u0791', 'z': '\u0792', 'T': '\u0793', 'y': '\u0794', 'p': '\u0795', 'j': '\u0796', 'c': '\u0797', 'X': '\u0798', 'H': '\u0799', 'K': '\u079A', 'J': '\u079B', 'R': '\u079C', 'x': '\u079D', 'B': '\u079E', 'M': '\u079F', 'Y': '\u07A0', 'Z': '\u07A1', 'W': '\u07A2', 'G': '\u07A3', 'Q': '\u07A4', 'V': '\u07A5', 'a': '\u07A6', 'A': '\u07A7', 'i': '\u07A8', 'I': '\u07A9', 'u': '\u07AA', 'U': '\u07AB', 'e': '\u07AC', 'E': '\u07AD', 'o': '\u07AE', 'O': '\u07AF', 'q': '\u07B0', 'F': '\uFDF2', ',': '\u060C', ';': '\u061B', '?': '\u061F' }; // Look up the translated char using the matrix var transChar = transArray[String.fromCharCode(keycode)]; // Return immediately if pressed key does not require translation if (!transChar || ctrl) return true; // Cancel default action for the key if (typeof e.preventDefault == 'function') { e.preventDefault(); } else { e.returnValue = false; } // Insert text at cursor position if (this.selectionStart) { // For Firefox/Safari: var selOld = this.selectionStart + 1; this.value = this.value.substring(0, this.selectionStart) + transChar + this.value.substring(this.selectionEnd, this.value.length); this.focus(); this.setSelectionRange(selOld, selOld); } else if (document.selection) { // For IE/Opera: this.focus(); sel = document.selection.createRange(); sel.text = transChar; } else { // Fallback if all else fails: this.value += transChar; } }, /** * Initialize class */ init: function () { this.setHandlers('thaanaKeyboardInput'); } }; // Start the Thaana Keyboard soon as the page is loaded var old = (window.onload) ? window.onload : function () {}; window.onload = function(){old(); thaanaKeyboard.init();};