Categories
JavaScript Answers

How to check for the subtract key press with JavaScript?

Spread the love

Sometimes, we want to check for the subtract key press with JavaScript.

In this article, we’ll look at how to check for the subtract key press with JavaScript.

How to check for the subtract key press with JavaScript?

To check for the subtract key press with JavaScript, we can listen to the keypress event and use the event key property to check which key is pressed.

For instance, we write:

window.onkeypress = (e) => {
  if (e.key === '-') {
    console.log('subtract pressed')
  }
}

to check if the subtract key is pressed with e.key === '-' in the window.

If it’s pressed, then we log 'subtract pressed'.

Conclusion

To check for the subtract key press with JavaScript, we can listen to the keypress event and use the event key property to check which key is pressed.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *