This post is obsolete. Please see the updated post.
The phone pad below is messed up in Internet Explorer. I know. I don't care anymore. The plugin, however, works.
$('div.test input:button').click(function(){
$('.output').sendkeys($('div.test input:text').val());
});
$('.phonepad input').click(function(){
$('.output').sendkeys(this.name || this.value);
});
$('.output:first').bind('keypress', function(evt){
$('#keypress').text($('#keypress').text()+' '+evt.which);
}).bind('sendkeys', function(evt){
$('#sendkeys').text($('#sendkeys').text()+' '+evt.which);
});
<div>
<textarea class="output"></textarea>
<br/>
<input type="text" class="output" />
<div class="phonepad"><input type="button" name="{leftarrow}" value="←"/><input type="button" name="{rightarrow}" value="→"/><input type="button" name="{backspace}" value="BS"/><input type="button" value="7" /><input type="button" value="8" /><input type="button" value="9" /><input type="button" value="4" /><input type="button" value="5" /><input type="button" value="6" /><input type="button" value="1" /><input type="button" value="2" /><input type="button" value="3" /><input type="button" value="*" /><input type="button" value="0" /><input type="button" value="#" /></div>
<div class="test"><input type="text" /><input type="button" value="test"/></div>
<div id="keypress">keypress event.which:</div>
<div id="sendkeys">sendkeys event.which:</div>
</div>
The $.fn.sendkeys
Plugin
I wanted to make a general-purpose onscreen keypad, and wasted a huge amount of time trying to find a way to
simulate a keypress.
$(element).trigger("keypress",...)
won't work. Neither will keyup or keydown.
For security reasons, I guess, you can't tell an element to pretend a key was pressed. The browser is too
worried that you will access menus or something.
So I wrote my own plugin and named it after Microsoft's sendkeys
which does similar things. For any element elem
that
is a <textarea>
or <input type="text">
,
$(elem).sendkeys(string)
inserts string
at the insertion point or selection.
It's the insertion point sensitivity
that makes it more sophisticated than elem.value += string
.
Downloads
Download $.fn.sendkeys.
Continue reading ‘The$.fn.sendkeys
Plugin’ »