U
U
Umid2015-10-20 14:03:53
JavaScript
Umid, 2015-10-20 14:03:53

How to set up an event when a button is pressed on the keyboard??

Hello, I am writing a presentation in the form of a slider in full screen and decided to set up an event to change the slide to arrows - left and right.
How can this be done in javascript and jquery?

And there is 1 more question, what plugin can I install for Sublime(2) to highlight js and jquery syntax??

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Denis Ineshin, 2015-10-20
@DarCKoder

Like this: jsfiddle.net/7o9maLLs/1
+ you can add more buttons besides the arrows

$(document).on('keyup', function (event) {
    var key = event.which;
    
    switch (key) {
        case 8:  // backspace
        case 33: // Page Up
        case 37: // <-
            console.log("Назад");
            break;
        case 32: // space
        case 34: // Page Down
        case 39: // ->
            console.log("Вперед");
            break;
    }
});

V
Vladimir Io, 2015-10-20
@vawsan

$(document).keydown(function(e) {
    switch(e.which) {
        case 37: // left
        break;

        case 38: // up
        break;

        case 39: // right
        break;

        case 40: // down
        break;

        default: return; // exit this handler for other keys
    }
    e.preventDefault(); // prevent the default action (scroll / move caret)
});

G
GreatRash, 2015-10-20
@GreatRash

Isn't it easier to use ready -made solutions?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question