Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

474
Views
Angular - How to trigger keyboard events on button click?

I have provided two separates button for Ctrl+z and Ctrl+y. I want the Ctrl+z and Ctrl+y functionality to be performed on the click of a button. Here is my function.

undoText(event:MouseEvent){
    var element =   document.getElementById("undoBt")
     if(element){
        element.dispatchEvent(new KeyboardEvent('keydown',{composed: true, bubbles:true, cancelable:true, code:"ControlLeft", ctrlKey:true, key:'Control',keyCode:17, location:1}));
        element.dispatchEvent(new KeyboardEvent('keydown',{bubbles:true,   cancelable:true, code:"KeyZ", composed:true, ctrlKey:true, key:'z', keyCode: 90}));
       }

  }

Here is my html.

<mat-icon matTooltip="Undo"  (click)="undoText($event)" *ngIf="svgInit" id="btn1" class="undo" svgIcon="undo" aria-hidden="false" aria-label="undo">
        </mat-icon>

To achieve this, I am trying to trigger Ctrl+z and Ctrl+y on button click but they are not getting triggered and I'm not able to set isTrusted property true.

What is wrong with the code?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

(keyup.enter)="keyUpFunction($event)"

Where keyUpFunction($event) is your function.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!