From 23aa2218c88d0eef0cdec2bbc2434e1bb5aaa790 Mon Sep 17 00:00:00 2001 From: franck Date: Sat, 23 Jun 2012 18:23:40 +0200 Subject: [PATCH] This is the first attempt to correct the bug with diacritics (see https://github.com/Mottie/Keyboard/issues/79). Note that the solution has not been fully tested and only works for charaters entered from the physical keyboard, not the virtual one by clicking on the keys. A new option keyboard.deadkeys has been added (see the first qwerty keyboard in demo.js). the option useCombos needs to be set to false. --- demo/demo.js | 46 +++++++++++++++++++++++++++++++++++- js/jquery.keyboard.js | 55 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) diff --git a/demo/demo.js b/demo/demo.js index ffa44652..68736f6c 100644 --- a/demo/demo.js +++ b/demo/demo.js @@ -7,7 +7,51 @@ jQuery(function($) { // QWERTY Text Input // The bottom of this file is where the autocomplete extension is added // ******************** - $('.qwerty:first').keyboard({ layout: 'qwerty' }); + $('.qwerty:first').keyboard({ + layout : 'qwerty', + useCombos : false, + deadKeys : { + '^' : { + a : "[â]", + e : "[ê]", + i : "[î]", + o : "[ô]", + u : "[û]", + A : "[Â]", + E : "[Ê]", + I : "[Î]", + O : "[Ô]", + U : "[Û" + + }, + '`' : { + a : "[à]", + e : "[è]", + u : "[ù]", + A : "[À]", + E : "[È]", + U : "[Ù" + + }, + '"' : { + e : "[ë]", + i : "[ï]", + u : "[ü]", + E : "[Ë]", + I : "[Ï]", + U : "[Ü]" + + }, + "'" : { + e : "[é]", + E : "[É]" + }, + "," : { + c : "[ç]", + C : "[Ç]" + } + }, + }); // QWERTY Password // ******************** diff --git a/js/jquery.keyboard.js b/js/jquery.keyboard.js index faf8e2ff..762f96da 100644 --- a/js/jquery.keyboard.js +++ b/js/jquery.keyboard.js @@ -99,6 +99,47 @@ CSS: $.keyboard = function(el, options){ var base = this, o; + function isDeadKey(charStr) { + return o.deadKeys && o.deadKeys.hasOwnProperty(charStr); + } + + /** + * Return: + * - the parameter 'charStr' if no dead key has been previously pressed. + * - the dead key previously pressed if charStr is 'space' or a dead key. + * - a new character if a dead key has just been previously pressed and a mapping exists for charStr (option keyboard.deadkeys). + * - the empty string if charStr is a dead key. + */ + function getCharModifiedByDeadKey(charStr) { + var newCharStr = ''; + + if (base.lastDeadKeyStr == '') { + if (isDeadKey(charStr)) { + newCharStr = ''; + base.lastDeadKeyStr = charStr; + } else { + newCharStr = charStr; + } + } else { + if (isDeadKey(charStr)) { + newCharStr = base.lastDeadKeyStr; + base.lastDeadKeyStr = charStr; + } else { + if (charStr == ' ') { + newCharStr = base.lastDeadKeyStr; + } else { + newCharStr = o.deadKeys[base.lastDeadKeyStr][charStr]; + if (typeof newCharStr === 'undefined') { + newCharStr = base.lastDeadKeyStr + charStr; + } + } + base.lastDeadKeyStr = ''; + } + } + return newCharStr; + } + + // Access to jQuery and DOM versions of element base.$el = $(el); base.el = el; @@ -136,6 +177,8 @@ $.keyboard = function(el, options){ base.temp = [ '', 0, 0 ]; // used when building the keyboard - [keyset element, row, index] + base.lastDeadKeyStr = ''; // The last dead key entered to take into account, '' for none. + // Bind events $.each('initialized visible change hidden canceled accepted beforeClose'.split(' '), function(i,f){ if ($.isFunction(o[f])){ @@ -305,7 +348,9 @@ $.keyboard = function(el, options){ if (o.enterNavigation) { base.alwaysAllowed.push(13); } // add enter to allowed keys base.$preview .bind('keypress.keyboard', function(e){ + var k = String.fromCharCode(e.charCode || e.which); + if (base.checkCaret) { base.lastCaret = base.$preview.caret(); } // update caps lock - can only do this while typing =( @@ -329,6 +374,16 @@ $.keyboard = function(el, options){ e.preventDefault(); } } + var charWithDeadKey = getCharModifiedByDeadKey(k); + if (charWithDeadKey == '') { + e.preventDefault(); + return; + } + else if (charWithDeadKey != k) { + base.insertText( charWithDeadKey ); + e.preventDefault(); + } + base.checkMaxLength(); })