Solved

Does javascript provide any way of specifying the keycode without writing the number

Posted on 2016-11-06
2
77 Views
Last Modified: 2016-11-15
HI,
Currently in my javascript code inside keydown evenListener i am using code :
e.keyCode == 38
38 is for up button.. Is there a better way to write the same by getting rid of 38 magic number something inbuilt in javascript ?

Thanks
0
Comment
Question by:Rohit Bajaj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Assisted Solution

by:Paweł
Paweł earned 200 total points
ID: 41876096
in short no

you can use this site to figure them out

http://keycode.info/

if you really want you could create a javascript object that could store them, sort of like an enum in c# with friendly names
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 300 total points
ID: 41876205
@Pawel is right, nothing inbuilt in javascript
Example of object below :
Test page : https://jsfiddle.net/86hnL05s/

	var KEY = {
		UP: 38,
		DOWN: 40,
		DEL: 46,
		TAB: 9,
		RETURN: 13,
		ESC: 27,
		COMMA: 188,
		PAGEUP: 33,
		PAGEDOWN: 34,
		BACKSPACE: 8
	};

document.onkeydown = function(e) {
    switch(e.keyCode) {
        case KEY.UP: write("you pressed up key");break;
        case KEY.DOWN: write("you pressed down key");break;
        default: break;
   }
}

var write = function(what) {
   document.getElementsByTagName("div")[0].innerHTML = what;
}

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
web page freezes after ajax post 7 99
JSON help 8 46
Javascript Error 10 24
CSS/jQuery scaling images and detecting height and width 8 6
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question