Dennis Thrysøe
Dennis Thrysøe

Reputation: 1911

jQuery change keypress to text field

I am looking for a cross-browser (can be based on jquery) way to change the keypress done by a user.

Capturing it is no problem. Changing behaviour in a cross-browser compatible manner, seems to be a problem.

Essentially I need to map some extra keys to special characters. Like ctrl+alt+p should enter a special character in a text field or textarea.

Thanks,

-dennis

Upvotes: 1

Views: 2307

Answers (3)

Tim Down
Tim Down

Reputation: 324597

I've covered a cross-browser technique for doing this before on SO: Can I conditionally change the character entered into an input on keypress?

You can adapt this for your own mappings. Also, you should use the keydown rather than keypress event if you're detecting keystrokes involving Ctrl or Alt, since not all browser will fire keypress events when no character is typed.

Upvotes: 0

Tramov
Tramov

Reputation: 1186

You can capture a key press, look at the event to see if CTRL or ALT has been pressed. I assume this is pretty much what you are doing?

$(document).ready(function() 
{
    $("#target").keydown(function(event) 
    {
        if (event.ctrlKey && event.altKey && event.keyCode == '80') {
            alert("Ctrl-Alt-P combination");
        }
    }
});

Upvotes: 2

Related Questions