views:

38

answers:

4

I am trying to mimic key press events, for instance Ctrl+D on a button click.

It would be great if someone can point me in the right direction on how to achieve the same.

A: 

You're not allowed to do that. Imagine all the havoc I could wreak if I could send CTRL-ALT-DEL at will.

FatherStorm
No programs, let alone a browser, can send a "real" Ctrl-Alt-Del to CSRSS.
Hello71
A: 

That would be "firing events", though I'm leaving the exercise to you to find the right code.

As the other guy said, you cannot do any kind of thing with it. It is purposefully limited.

However, let's say I have a wysiwyg editor in javascript, which supports receiving ctrl+s and saving, you should be able to fire that yourself and make it save anyway.

At the end, it's a matter of context (focus), and which sometimes fails (again, purposefully).

Christian Sciberras
A: 

The code for triggering a custom event (in this instance, Ctrl+d) is as follows:

var evt = jQuery.Event("keypress");
evt.keyCode = 100; // d
evt.ctrlKey = true;
$(document).trigger(evt);

NB that, as the other answers have said, this will be limited in its impact. You won't be able to affect normal browser functions in this way.

lonesomeday
Is there any piece of code which I can run and see the result. I tried Ctrl+S but even that didn't worked
Sandhurst
Have updated with a working example.
lonesomeday
A: 

I don't think that you can mimic key presses, however if you are trying to have the site bookmark on a button click you can use a simple script. http://www.developersnippets.com/2009/05/10/simple-bookmark-script-using-jquery/

FallingReign