How can I get the keyboard to start Shifted?
I'm using the keyboard to add comments to a thread and personally hate it when poeple dont capitalise the first letter of a sentence.
How can I make the keyboard start up shitfted? Like it does on my Android phone and autoshift after a full stop '.' ?
You could use the callback methods to check if it's at the start of the input and if there is a . (period + space) immediately to the left of it. I set up a demo here, and the code I used is shown below.
The main issue is that making the keyboard show a shifted key set doesn't force the user to use the shift key on their actual keyboard. I guess we could use javascript to auto-capitalize these characters in the input if needed.
$('#keyboard').keyboard({
visible: function(e, kb, el) {
// start with shifted key set showing
if (kb.$preview.caret().start === 0) {
kb.shiftActive = true;
kb.showKeySet(el);
}
},
change: function(e, kb, el) {
var caret = kb.$preview.caret(),
end = caret.end - 2 >= 0 ? caret.end - 2 : caret.end;
// find the two characters immediately to the left of the caret
str = kb.$preview.val().substring(end, caret.end);
if (caret.start === 0 || str.indexOf('. ') >= 0) {
kb.shiftActive = true;
kb.showKeySet(el);
}
}
});
Fantastic response. Works perfectly!
The above code from Mottie resulted in the Caps Lock and Shift being completely unresponsive. I amended it slightly so I could have both the caps lock and shift key functional while having the auto-shift working too.
Change: kb.shiftActive = (caret.start === 0 || str.indexOf('. ') >= 0); kb.showKeySet(el);
To: if (caret.start === 0 || str.indexOf('. ') >= 0) { kb.shiftActive = true; kb.showKeySet(el); }
However, you can't take the auto-shift off when on a caret position that has been auto-shifted (the first character or after a ". ")
Any ideas?
I'd noticed this but not got around to fixing it! Just implemented your suggestion and its working well! Thanks!
@blookie Thanks! That change looks like it would work better. I'll update the code above.
@bencrundwell You're welcome!
Clicking shift changes keyboard to all caps then right back to lower case?
A couple improvements to the logic:
-
Automatically un-capitalize as the user types.
-
Also include ! and ? as sentence enders.
-
Don't capitalize if text box already has text in it.
$('#keyboard').keyboard({ visible: function(e, kb, el) { autoshift_keyboard(kb, el); }, change: function(e, kb, el) { autoshift_keyboard(kb, el); } }); function autoshift_keyboard(keyboard, element){ var caret = keyboard.$preview.caret(); var end = caret.end - 2 >= 0 ? caret.end - 2 : caret.end; // find the two characters immediately to the left of the caret var str = keyboard.$preview.val().substring(end, caret.end); // start with shifted key set showing var is_at_beginning = keyboard.$preview.caret().start === 0; var is_at_new_sentence = str.indexOf('. ') >= 0 || str.indexOf('! ') >= 0 || str.indexOf('? ') >= 0; if (is_at_beginning || is_at_new_sentence) { keyboard.shiftActive = true; keyboard.showKeySet(element); }else{ keyboard.shiftActive = false; keyboard.showKeySet(element); } }