Kuan
Kuan

Reputation: 11389

How to check if a key is being pressed right now

All:

I wonder how can I detect a key holding in the browser. For example:

I have a DIV on the page, and when I press ctrl key, then I can drag it around on the page, right now, I have implement the drag part, but how to enable that drag only when I hold ctrl key?

Thanks

Upvotes: 0

Views: 211

Answers (1)

Luke Joshua Park
Luke Joshua Park

Reputation: 9805

You didn't specify any languages, so a general solution:

You can use a boolean value, and listen for the keyup and keydown events for the body.

In these events, toggle the value of the boolean value. This way you can always check if the key is pressed by accessing the value of the boolean.

Upvotes: 1

Related Questions