views:

4582

answers:

5

How can I programmatically generate keypress events from Javascript code running in Safari? It looks like WebKit is using the DOM level 3 model for creating keyboard events from Javascript, and the DOM level 3 keyboard event model does not support the keypress event. Is there another way that I can use?

I'm looking for as pure a Safari/WebKit DOM solution as possible. I'd really prefer not to modify the web page, and I'd also rather not add dependencies on external libraries. I need to activate any existing keypress handlers, so it won't work to add a new handler and directly call it.

It looks like WebKit has the keyCode and charCode properties of the keypress event defined in its UIEvent class, but they are read-only. Is there any way to set those properties? The following does not work:

var evt = document.createEvent('UIEvents');
evt.initUIEvent('keypress', true, true, window, 0);
evt.keyCode = 114; // 'r'
evt.charCode = 114;
alert("keyCode = " + evt.keyCode + ", charCode = " + evt.charCode); // both 0

Setting the detail property in the call to initUIEvent also seems to have no effect.

A: 

http://docs.jquery.com/Events/keypress#fn

In other words, you attach a keypress event to some element. Using an element with the ID of "someid", for example:

<script language="text/javascript" src="jquery.js"></script>
<script language="text/javascript">
$(function() {
    // add a keypress handler
    $("#someid").keypress(function(e){
        alert('you just pressed ' + e.which);
    });
    // imitate the keypress
    $("#someid").keypress();
});
</script>
Matt
That example doesn't look like it's generating a keypress event. It looks to me like it's adding a keypress handler function to the target element, and then directly calling the handler function.
Greg
the second part, ie: '$("#someid").keypress();', is generating the keypress.
Mark Rogers
A: 

Sounds like a very similar (non browser specific) question was asked and answered already:

http://stackoverflow.com/questions/202285/trigger-a-keypress-with-jqueryand-specify-which-key-was-pressed

Dan Esparza
I can create keypress events in Internet Explorer, so there's nothing in general stopping the generation of keypress events. It also looks like Safari supports DOM level 3's keydown, keyup, and textinput events. But I'm trying to automate pages that may already have keypress handlers.
Greg
A: 

Have you tried the following?

var evt = document.createEventObject()
evt.keyCode = 114
evt.charCode = 114
window.fireEvent("keypress", evt)
TM
Safari's document object does not have a createEventObject() method, and I'm pretty sure that fireEvent() is also Internet Explorer-specific. So no luck there.
Greg
+2  A: 

Use the TextEvent (introduced by DOM3). Since you're looking to generate keypress events, I'm guessing you're working with characters. In the code below, textToInsert is a string, and textarea the element I'm dispatching the event to.

var eventObject = document.createEvent('TextEvent');
eventObject.initTextEvent('textInput',
                          true,
                          true,
                          null,
                          textToInsert);

textarea.dispatchEvent(eventObject);

This works on Safari 3.1.2 (consequently on Chrome).

I will try that and let you know how it works for me when I have a chance to get back to that project. Thanks.
Greg
A: 
todd