Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get keydown events on a div in chrome?

Tags:

jquery

keydown

I'd like to get keydown events on a div. I use JQuery keydown. Pretty simple.

However, it does not work on chrome. For this to work on chrome, I have to set tabindex = 0.

If I do this, Chrome puts an ugly orange border around my div.

Is there a way to make this work on chrome without the ugly orange border?

like image 490
user380719 Avatar asked Nov 07 '11 04:11

user380719


People also ask

How do you get the value of a Keydown event?

We set the onKeyDown prop on the input element, so every time the user presses a button when the input has focus, the handleKeyDown function is invoked. The key property on the event object returns the value of the key pressed by the user.

How do you use Keydown in HTML?

The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup , but as 97 by keypress . An uppercase "A" is reported as 65 by all events.

What is difference between Keyup and Keydown?

In this article Occur in sequence when a user presses and releases a key. KeyDown occurs when the user presses a key. KeyUp occurs when the user releases a key.

What does Keydown return?

Return values: It returns whether any key is pressed or not or which key is pressed.


1 Answers

Keydown event is only sent to the HTML element that has the focus. Focusable elements vary between browsers, but elements of which tabindex property is set can always get focus in most browsers.

You have already set tabindex for div element so that it is focusable and can receive keyboard event. Your problem is the default outline of currently focused element on Google Chrome.

To change the outline (the "ugly orange border" as you mentioned), use pseudo CSS class :focus and CSS property outline. The following example will remove outline from all elements when they have focus:

*:focus
{
    outline: none;
}

Hope this help.

like image 89
ExpExc Avatar answered Oct 01 '22 02:10

ExpExc