Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detect keypress during onclick event for div

Status
Not open for further replies.

nanananana

Technical User
Apr 16, 2007
2
GB
Hi

i am looking for a way to detect when a user is holding down the control key (or could be another key) while they are clicking on a div element, because i want to fire a different function if they are holding down the control key compared to when they are not.

I have googled around and havent come up with an answer yet and its kinda frustrating :) Does anyone know if this can be done, must work in FF, dont really care about IE :)

Cheers!
 
You should be able to do this really easily, so I'm surprised nothing shows up on a Google search for you... something like this:

Code:
el.onclick = keypressfunc;

function keypressfunc(eventData) {
   if (eventData.ctrlKey) {
      // Do something here...
   }
}

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
P.S... Here's a good reference to available DOM events:


Support for these may differ from browser to browser, and IE has a different way of getting event data to almost every other browser, so you'd need to modify the above code slightly if you do want IE support.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
yeah it worked fine, i use a global variable to flag whether the contorl key is being held down, i then reference this on a mouseclick event. If the user is holding down the control key i do one thing, if not i do another. thank!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top