Skip to content

Commit

Permalink
This is the first attempt to correct the bug with diacritics (see Mot…
Browse files Browse the repository at this point in the history
…tie#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.
  • Loading branch information
franck committed Jun 23, 2012
1 parent 04f2a86 commit 23aa221
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
46 changes: 45 additions & 1 deletion demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ********************
Expand Down
55 changes: 55 additions & 0 deletions js/jquery.keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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])){
Expand Down Expand Up @@ -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 =(
Expand All @@ -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();

})
Expand Down

0 comments on commit 23aa221

Please sign in to comment.