1

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

Dennis Thrysøe
  • 1,791
  • 4
  • 19
  • 31
  • 3
    Be careful with this. Depending on the browser (and OS) you could be covering up shortcuts, that the user is expecting to work, or you won't be able to to catch them at all. Ctrl-Alt-... shortcuts, for example, are often used for system-wide functions in Linux. – RoToRa Mar 17 '11 at 11:27
  • I now. But this is a tradeoff with functionality. This is a situation where the users need to input characters that are typically not on their keyboards/keymappings. The alternative would be to use a mouse to point to a character grid next to each input field. – Dennis Thrysøe Mar 17 '11 at 12:09
  • Hmm. It seems that this might solve it: http://stackoverflow.com/questions/1064089/inserting-a-text-where-cursor-is-using-javascript-jquery – Dennis Thrysøe Mar 17 '11 at 12:09

3 Answers3

2

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");
        }
    }
});
Tramov
  • 1,166
  • 1
  • 11
  • 17
  • Yes, but I need to replace the alert() invocation with something that actually writes a character - just possibly another one than the browser would normally write. – Dennis Thrysøe Mar 17 '11 at 12:07
0

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.

Community
  • 1
  • 1
Tim Down
  • 318,141
  • 75
  • 454
  • 536