jQuery event.which Property

Last Updated:

jQuery event.which Property

The jQuery event.which property returns which keyboard key or mouse button was pressed for the event.

Detecting which Keyboard Key was Pressed

In the following example, we will use keydown() method along with event.which property to detect which keyboard key was pressed.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery event.which Property</h1> <input type="text" placeholder="type something"> <p>keydown: <span id="point"></span></p> <script> $(document).ready(function(){ $("input").keydown(function(e){ $("#point").text(e.which); }); }); </script> </body> </html>

Syntax

event.which;

Return Value

Value Explanation
Number Returns the Unicode character code of the pressed key.

Browser Support

Devicedesktopmobile
Browsergoogle chromesafarifirefoxinternet Exploreredgeoperagoogle chromesafarifirefoxedgeoperaandroid webviewsamsung internet
event.whichYesYesYesYesYesYesYesYesYesYesYesYesYes

Detecting which Mouse Button was Pressed

In the following example, we will use mousedown() method along with event.which property to detect which mouse button was pressed.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery event.which Property</h1> <p>Click on this paragraph any mouse button (left, right, center).</p> <p>mousedown: <span id="point"></span></p> <script> $(document).ready(function(){ $("p").mousedown(function(e){ $("#point").text(e.which); }); }); </script> </body> </html>

Share this Page

Meet the Author