4

Ok now I am aware that you can listen to keypresses (keydowns keyups etc...) but I would like to TRIGGER a keypress. Like as if the user presses CTRL+S to save.

Short question is, is it possible to use Jquery to trigger 2 key presses at the same time? I have been trying to look this up but all the stuff I found was talking about capturing key presses and not triggering them especially 2 key presses at the same time.

Any ideas how this can be accomplished? If you could lead me to the right track it would be much appreciated.

Thirumalai murugan
  • 5,698
  • 8
  • 32
  • 54
Alcatraz
  • 81
  • 1
  • 1
  • 4
  • possible duplicate of [JavaScript: capture key press without placing an input element on the page?](http://stackoverflow.com/questions/2878983/javascript-capture-key-press-without-placing-an-input-element-on-the-page) – Marc B Jul 24 '11 at 18:12
  • @Marc: You're misunderstanding the question. – SLaks Jul 24 '11 at 18:14
  • Is this even cross browser, let alone cross OS compatible? For instance Ctrl+S will work in Windows, but it most certainly will not work in Mac. Also, what happens if someone has bound their keys differently? – KyleWpppd Jul 24 '11 at 18:22

2 Answers2

8

If you want to trigger your own handler code, you should just put that code in a separate function and call the function normally.

If you want to trigger the browser's default functionality, you should give up; that is impossible for security reasons. (Although you may be interested in window.print())

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
4

I don't know if you can trigger the key presses but perhaps if you explain what you are specifically trying to achieve you might be able to find a solution?

For example, if you want to trigger ctrl-p to bring up the print dialog you can do this using window.print();

If you want to trigger actions in your own code then you can just call the code directly rather than triggering the key press.

I don't think there is any way to trigger a file | save as (ctrl-s) by using JavaScript though.

Thirumalai murugan
  • 5,698
  • 8
  • 32
  • 54
rtpHarry
  • 13,019
  • 4
  • 43
  • 64