search

Home  >  Q&A  >  body text

javascript - How does js trigger the keyboard Ctrl+P event by clicking a button?

window.print(), this method can only be triggered after the page is completely loaded.

When the page loads slowly or when asynchronous JS is loaded, click the page print button to print. window.print() can only be triggered after the page loading is completed, so I want to notify JS to directly trigger Ctrl P implementation

过去多啦不再A梦过去多啦不再A梦2751 days ago678

reply all(1)I'll reply

  • 仅有的幸福

    仅有的幸福2017-05-19 10:31:30

    window.addEventListener('keydown', function (evt) {
      if (!evt.ctrlKey || (evt.key !== 'p' && evt.keyCode !== 80)) { return }
      evt.preventDefault()
    
      // ....
      window.print()
    
    })

    reply
    0
  • Cancelreply