Usage - Mottie/Keyboard GitHub Wiki
Sections: Get keyboard Object | Open keyboard | Switch keyset | Add Content | Simulate Typing | Enable/Disable | window.prompt
The following methods are equivalent:
var keyboard = $('#keyboard').getkeyboard();
var keyboard = $('#keyboard').data('keyboard');The examples below use the following button HTML
<button class="open">Open Keyboard</button>Open the keyboard by clicking on a button:
$('button.open').click(function(){
$('#keyboard').getkeyboard().reveal();
});Pass a string to the showKeySet function.
- Use
normalfor the default keyset. - Use
shiftto show the shift keyset. - Use
altto show the alt keyset. - Use
meta{name}to show the meta keyset.- In version 1.26.8,
{name}includes any of the following characters:A-Za-z0-9_-, e.g.metakeys,meta_greek,meta1ormeta-astrology. - In older versions, the
{name}placeholder must be followed by at least one number, thenA-Za-z0-9_, e.g.meta1,meta1testormeta0_symbols.
- In version 1.26.8,
Combine any of the above keyset names with a + symbol separating each one. For example,
shift+meta_symbolsmeta-1+shift+alt- The order in which the keysets are listed does not matter.
$('button.show-altgr').click(function(){
var keyboard = $('#keyboard').data('keyboard');
keyboard.showKeySet('alt+shift');
});If the keyset does not exist, any of the matching keyset names will be used, or it will fallback to the normal keyset.
For example:
- Using
shift+meta1, butmeta1does not exist, then thenormal+shiftkeyset will be shown. - Using
shift+alt+meta_keysbut themeta_keysonly has ashiftkeyset defined, then themeta_keys+shiftkeyset will be shown. - Using
random text, will default to thenormalkeyset.
See the Methods page for more examples.
$('button.open').click(function(){
var keyboard = $('#keyboard').getkeyboard();
keyboard
.reveal()
.insertText('Hello, my name is Inigo Montoya. You killed my father, prepare to die!');
});Please note that the insertText function does not trigger a change event on the input. If you require this event, you will need to trigger it manually.
$('button.open').click(function(){
var keyboard = $('#keyboard').getkeyboard();
keyboard
.reveal()
.insertText('Anybody want a peanut?')
// added in v1.26.8; insertText returns the keyboard object
// this will trigger the change event on the original input
.$el.trigger('change');
});As of v1.26.8, the insertText() function will return the keyboard object to allow further chaining. See issue #506.
This needs the typing extension loaded and initialized - see the Setup page for more details.
$('button.open').click(function(){
var keyboard = $('#keyboard').getkeyboard();
keyboard
.reveal()
.typeIn('As you wish!', 500, function(kb) {
// do something after text is added (kb = keyboard)
// kb.accept(); // close keyboard and accept changes
});
});Add a {toggle} button to your keyboard, or if your keyboard is always open, you can add an external button to toggle the keyboard state:
$('button.toggle').click(function(){
var keyboard = $('#keyboard').getkeyboard();
// if keyboard.enable = false, all keys & preview will be disabled
keyboard.enable = !keyboard.enable;
keyboard.toggle();
});If we want the on-screen keyboard to emulate the built-in window.prompt function:
result = window.prompt(message, defaultValue);and so allow the app logic of accepted values to move out of "accepted" functions and events and into their natural UI handlers, we could write
function keyboardPrompt(message, defaultValue) {
var box = $('#keyboard');
box.show().val(defaultValue).attr('placeholder', message).getkeyboard().reveal();
return new Promise(accept => box.on('accepted cancelled', e => accept(e.type == 'accepted' ? box.val() : null)));
}Example usage:
async function enterName() {
var name = await keyboardPrompt('What is your name?', 'Anonymous');
if (name != null) console.log('Your name is ' + name);
}
enterName();