2

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?

Chris
  • 26,544
  • 5
  • 58
  • 71
fox
  • 287
  • 5
  • 16

5 Answers5

4

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.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
4

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.

Chris
  • 26,544
  • 5
  • 58
  • 71
  • +1 for not using a library :) – Damien Overeem Nov 28 '12 at 10:04
  • 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
0

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).

Damien Overeem
  • 4,487
  • 4
  • 36
  • 55
  • I'm trying to achieve this in plain javascript or dojo. Is there a way – fox Nov 28 '12 at 09:59
  • 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
0

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

Community
  • 1
  • 1
Tomas Ramirez Sarduy
  • 17,294
  • 8
  • 69
  • 85
-1

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

}

});
Dineshkani
  • 2,899
  • 7
  • 31
  • 43