简体   繁体   中英

Is it possible to have a keyCode equal to keys?

I'm creating a more accessible version of jQuery UI slider with number keys assigned to events that move the slider to different positions. I would like the user to be able to use both sets of numbers on a keyboard.

Example "49" and "97" for the number 1.

Here is the code I'm using:

if(event.keyCode == "49")
{
$("#slider_feedback").html(mod3["rc" + clip + "_1"]);
$("#slider_feedback").show();
$(".ui-slider-handle").css("left", "0%");
}

Any suggestions?

Why you don't just put both conditions in IF statement but with OR operator:

if(event.keyCode == 49 || event.keyCode == 97)
{
$("#slider_feedback").html(mod3["rc" + clip + "_1"]);
$("#slider_feedback").show();
$(".ui-slider-handle").css("left", "0%");
}

See a list of Java script OPERATORS and study it a little bit (JavaScript Assignment Operators are directly connected to this question):
http://www.w3schools.com/js/js_operators.asp

Instead of

if(event.keyCode == "49")

has to be

if(event.keyCode == '49' || event.keyCode == '97')

OR

if(event.which== 49 || event.which== 97)

I would make an array mapping the keycodes like that:

var mapKeys = [
   49: 0,
   97: 0
];

And then, I make only ONE script to handle the keypress.

$(document).bind('keypress', function(e) {
   if(mapKeys[e.keyCode] != undefined){
      $(".ui-slider-handle").css("left", mapKeys[e.keyCode] + "%");
   }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM