Confirmed users
478
edits
m (→Proposed API) |
|||
Line 181: | Line 181: | ||
== Examples == | == Examples == | ||
The following "snowman filler" Keyboard app will start filling snowman character ("☃") and follow by characters "SNOW" with key events to the input field whenever the user is focus on a input field and switch to the keyboard app. | |||
If the field is a numeric field, it will fill "1337". | |||
var im = navigator.inputMethod; | |||
// | im.addEventListener('inputcontextchange', function contextchanged(evt) { | ||
if (evt.inputContext) { | |||
// Got a new context, start working with it. | |||
startTyping(evt.inputContext); | |||
} else { | |||
// The user have removed the focus, we are not allow to type into the text field anymore. | |||
stopTyping(); | |||
} | |||
}); | |||
var timer; | |||
function startTyping(inputContext) { | |||
clearTimeout(timer); | |||
timer = setInterval(function () { | |||
if (inputContext.inputmode = 'numeric' || inputContext.type = 'number') { | |||
inputContext.commitText('1337'); | |||
} else { | |||
inputContext.commitText('☃'); | |||
'SNOW'.split('').forEach(function (k) { | |||
// For capital Latin letters, keyCode is same as the charCode. | |||
inputContext.sendKey(k.charCodeAt(0), k.charCodeAt(0)); | |||
}); | |||
}); | |||
} | |||
function stopTyping() { | |||
clearTimeout(timer); | |||
} | } | ||
== Related == | == Related == |