PDA

View Full Version : creating a fake keydown event in Safari/Dashboard




Oats
Dec 19, 2006, 10:17 AM
Hi,
I am trying to create a fake keydown event for a Dashboard widget I am creating. I am having extreme trouble trying to figure this out. Basically, I cannot seem to create the proper event structure. I want to create an event that simulates the user pressing the "return" key.

Below is a quote from a page that seems to get me most of the way, though Safari does not seem to implement the initUIEvent routine:

From http://www.howtocreate.co.uk/tutorials/javascript/domevents

Key events are a little harder, because Mozilla/Firefox does not implement the standard correctly, and does not allow you to create key events using generic UIEvents. Instead, it provides a proprietary module 'KeyEvents', and initialisation method 'initKeyEvent'. The proprietary initKeyEvent method expects the following parameters: 'type', bubbles, cancelable, windowObject, ctrlKey, altKey, shiftKey, metaKey, keyCode, charCode. You can use if(window.KeyEvent) to detect support for the KeyEvents module. Browsers that expect you to use UIEvents do not have a specific initialisation function for key events, so some properties must be added manually:

var fireOnThis = document.getElementById('someID');
if( window.KeyEvent ) {
var evObj = document.createEvent('KeyEvents');
evObj.initKeyEvent( 'keyup', true, true, window, false, false, false, false, 13, 0 );
} else {
var evObj = document.createEvent('UIEvents');
evObj.initUIEvent( 'keyup', true, true, window, 1 );
evObj.keyCode = 13;
}
fireOnThis.dispatchEvent(evObj);



jeremy.king
Dec 20, 2006, 03:18 PM
Oats, your posts always catch my attention.

Why exactly do you need to emulate a keystroke?

Sometimes knowing what you are trying to do could yield a more elegant and simple solution.

Oats
Dec 22, 2006, 01:57 PM
Oats, your posts always catch my attention.
thats funny, how come?

Why exactly do you need to emulate a keystroke?

Sometimes knowing what you are trying to do could yield a more elegant and simple solution.

Thanks for offering to help! I am developing a widget which uses a TEXTAREA element to use as a system console of sorts... I want users to be able to enter text at a prompt, and hit enter and receive a text response, in the same textarea. I have all this working fine in Firefox, the one problem I am having currently is when I insert text beyond the limits of the visible TEXTAREA, I want the TEXTAREA to scroll to the bottom automatically. It is quite frustrating without this, since the user will hit "return" to get a response, but the response is hidden until the user starts typing again.

My original thought (which works in firefox) was to set the scroll position of the TEXTAREA element, but this does not seem to be supported in Safari. (See related thread: http://forums.macrumors.com/showthread.php?t=260058)

My next thought was to create a "pagedown" or "end" keydown event, which would also scroll to the end of the TEXTAREA. Unfortunately, this doesn't seem to be well supported in Safari either, as far as I can tell. I can find all sorts of documentation about how to do this with IE or Firefox, but nothing for Safari. In my limited experience, Safari is extremely quirky about it's javascript implementation. Another reason why I think it is a shame that Safari even exists. Apple should just junk safari, and adopt Firefox. IMHO.

rand()
Dec 27, 2006, 09:33 PM
My original thought (which works in firefox) was to set the scroll position of the TEXTAREA element, but this does not seem to be supported in Safari. (See related thread: http://forums.macrumors.com/showthread.php?t=260058)


I have a feeling you should go back to this approach; make a handler call after adding your response text to the textarea.

Only this time, instead of trying to scroll the textarea directly, simply 'select' the last character of text and replace it with itself. This should (meaning I haven't tested it) trick Safari into scrolling to wherever the final character is. That should have the desired effect without trying to trick safari into thinking there's keyboard presses going on.

Another thing you might try is a double-pane approach, where there's a bottom-half (or quarter... whatever) textarea that takes input, and a top-half that displays response texts.

Rock!

-rand()

Oats
Jan 2, 2007, 11:02 PM
that doesn't work. any other ideas?

jeremy.king
Jan 3, 2007, 10:08 AM
that doesn't work. any other ideas?

Seems your only option may be a scrollable DIV.

Oats
Jan 3, 2007, 12:08 PM
using a scrollable DIV would work for the scroll issue, but then I don't think I can accept user input like a terminal?

jeremy.king
Jan 3, 2007, 12:23 PM
using a scrollable DIV would work for the scroll issue, but then I don't think I can accept user input like a terminal?

you can, but it gets more complicated with having to dynamically use a text input box.

This just isn't going to be easy :(

Good Luck.

saisuresh
Aug 22, 2012, 08:55 AM
document.onkeypress not working after dispatching the event. Only onkeydown is working. What should is suppose to do for making both working