Вопрос

I know there are ways to position the cursor in an input field. Is there a way to detect a cursor move or preferably disable cursor movement via the arrow keys? I'm implementing an onscreen keyboard and the cursor moves left and right as the user moves left and right over the keyboard. I would like to keep the input field focused the whole time.

Это было полезно?

Решение

You can attach a keydown or keyup event listener for that input, and invoke the preventDefault() method on the event object if the keyCode corresponds to cursor left and cursor right buttons. This will disable the caret movement.

yourInputElement.addEventListener('keydown', function(e){
    if(e.keyCode == 37 || e.keyCode == 39)
        e.preventDefault();
});

Другие советы

Here's a jQuery solution--blocks left/right arrow keys.

$([your input identifier]).keydown(function(event){
    if(event.which == 39 || event.which == 37){
        return false;
    }else{
        return true;
    }
});
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top