Is it possible to check if a key on a keyboard was pressed when hovering a certain element (div
) without using jQuery or a similar library?
-
6Answer to your question is yes. – Kaf Nov 28 '12 at 09:52
-
1almost duplicated: http://stackoverflow.com/q/2445613/670377 – Tomas Ramirez Sarduy Nov 28 '12 at 09:56
-
Yes but I want to achieve this not using JQuery – fox Nov 28 '12 at 10:01
-
You forgot to ask your implicit question "How to do this?" ;) – Serge S. Nov 28 '12 at 10:22
-
-1 You should have included on your question "using plain javascript" – Tomas Ramirez Sarduy Nov 28 '12 at 10:22
5 Answers
Here is a pure JS implementation:
myElement = document.getElementById('mydiv');
function keyaction(e){
myElement.innerHTML+= String.fromCharCode(e.charCode);
}
myElement.addEventListener("mouseover",function() {
document.addEventListener("keypress", keyaction);
});
myElement.addEventListener("mouseout",function() {
document.removeEventListener("keypress", keyaction, false);
});
Here is a demonstration: http://jsfiddle.net/bAV6f/1/
Type into the div
when it is hovered.

- 46,193
- 6
- 90
- 139
-
-
The OP says: `a key on a keyboard was pressed` -- I believe you missed that part. – Chris Nov 28 '12 at 10:05
-
-
Oh oh oh, sorry, I misread your code :) Although binding and unbinding each time is kinda expensive, this should work. Sorry again. – Chris Nov 28 '12 at 10:09
-
@Abody97 This works perfectly fine, see the demo [here](http://jsfiddle.net/bAV6f/) – Asad Saeeduddin Nov 28 '12 at 10:09
-
Yep, it surely works. I was just pointing out that binding and unbinding (using `addEventListener` and `removeEventListener`, that is) each time the mouse goes in or out is kinda expensive. – Chris Nov 28 '12 at 10:12
Here's a plain JavaScript solution:
var yourDiv = document.getElementById("yourdiv"); //cache your div
var keyPressed = 0; //kinda-boolean flag, if it's not 0 then at least one key is pressed
document.onkeydown = function() { //when a key is "down"
keyPressed ++; //increment the counter of "down" keys
};
document.onkeyup = function() { //when a key is "released"
keyPressed --; //decrement the counter of "down" keys
keyPressed = Math.max(0, keyPressed); //make sure it doesn't go negative
};
yourDiv.onmouseover = function() { //when you hover your div
if(keyPressed !== 0) { //if a key is pressed
//do stuff
}
};
The above only fires the mouse events when the mouse enters your div
. If you want it to fire if it moves inside it, use the onmousemove
event instead:
yourDiv.onmousemove = function() { //when the mouse moves inside your div
if(keyPressed !== 0) { //if a key is pressed
//do stuff
}
};
Note that you can use addEventListener
instead of directly setting onkeydown
, onkeyup
and onmousemover
-- but the concept stays the same.

- 26,544
- 5
- 58
- 71
-
-
This doesn't register events that occur while the mouse is hovered over the div. The question is `is it possible to check if a key on a keyboard was pressed when hovering a certain element`, not `is it possible to check if the element was moused over while a key on the keyboard was pressed` – Asad Saeeduddin Nov 28 '12 at 10:18
-
@Abody97 That is still incorrect. The event isn't supposed to be triggered by mouse movement. It is supposed to be triggered by a keypress provided that the mouse is inside the element ("hovering the element", to quote the OP). – Asad Saeeduddin Nov 28 '12 at 10:29
Your answer is yes, this is possible.
You would have to add a javascript event on hover (onmouseover) over the div that sets a keydown event on the same div.
You will have to remove the keydown event when the user stops the hover again (onmouseout).

- 4,487
- 4
- 36
- 55
-
-
Yes this is possible in plain javascript, although it is different for several browsers. Read ie. http://seanmonstar.com/post/706917892/cross-browser-addevent-without-frameworks. I don't know dojo, but i can assure you that it also has an addevent option somewhere. – Damien Overeem Nov 28 '12 at 10:02
-
Actually. Here is a manual for dojo http://dojotoolkit.org/documentation/tutorials/1.6/events/ – Damien Overeem Nov 28 '12 at 10:04
Short anwser
Yes, is possible
but, how can be done if it is possible?
Well, using Jquery is pretty simple:
$("div#selector").mouseover(function(e) {
if (e.ctrlKey)
alert('You are hover selector and pressing ctrl');
if (e.altKey)
alert('You are hover selector and pressing alt');
if (evt.which == 77)
alert('You are hover selector and pressing m');
...
});
See this list of key to know the javascript keycode associated with a character
Edit: I just readed your comment about not to use jQuery which should be included in the question

- 1
- 1

- 17,294
- 8
- 69
- 85
You can use the jquery for this purpose
$("p").hover(function(){
//Your code
});
Or
$(".selector").on({
mouseenter: function () {
//stuff to do on mouseover
},
mouseleave: function () {
//stuff to do on mouseleave
}
});

- 2,899
- 7
- 31
- 43