1

Possible Duplicate:
Can I do SendKeys in Javascript?

Hello is there any way to do the folowing things in javascript:

I want the user to click a link on the webpage, and when that link is clicked it would be the equivalent of them pressing CONTROL + D, except without actually pressing any keys.

Thank you

Community
  • 1
  • 1
  • 1
    Which is exactly the reason why this does not work. The user doing one thing and you making another out of it on system level will never work. – Beat Richartz Aug 20 '12 at 09:58

3 Answers3

1

If you use jQuery, you might be able to do something like this.

$("#linkId").click(function(event) {
    event.preventDefault(); // Stop the link click from doing anything.
    var ev = jQuery.Event("keypress"); // Build an event to simulate keypress.
    ev.which = 68; // Keycode for 'd' is 68
    ev.ctrlKey = true; // Control key is down.
    $(this).trigger(ev); // Fire!
});
Aesthete
  • 18,622
  • 6
  • 36
  • 45
0

You can simulate only in jQuery and track as events but you cannot do that as if the user made those key presses.

It's an security issue that can't be overwritten with javascript.

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
0

You can not make something else out of user input on system level. Assuming you want to let the user Bookmark a link (which is what Control + D does), this could get you where you want:

function addBookmark(title,url){ 
  if(window.sidebar){ 
    window.sidebar.addPanel(title, url, ""); 
  } else if(document.all){ 
    window.external.AddFavorite(url, title); 
  } else if((window.opera && window.print) || window.chrome){ 
    // Chrome & Opera do not have an add Favorite function on window
    alert('Press Control+D to bookmark (Command+D for macs) after you click Ok');
    // or something similar
  }
}

Then you bind the click handler to call the function:

$('#yourLink').on('click', function(e) {
  addBookmark($(this).attr('title'), $(this).attr('href'));
});
Beat Richartz
  • 9,474
  • 1
  • 33
  • 50