work around browsers' inconsistent handling of control keys?

Discussion in 'Javascript' started by Ben Crowell, Apr 3, 2009.

  1. Ben Crowell

    Ben Crowell Guest

    I have a js application that emulates a terminal:
    http://www.lightandmatter.com/calc/inf/
    That is, the user types a line of text, hits enter, and gets
    a response printed out. Then he can type more text, etc.

    Because I have emacs keybindings programmed into my muscle
    memory, I would like to make control-P and control-N work
    the way they do in emacs and in the bash shell, i.e.,
    they allow you to recall previously typed commands and
    move up and down through your history of commands. Since
    most browsers define control-P, for example, as a print
    command, I have to catch those keypress events if they
    occur within the text input area, and make sure they don't
    bubble up to the browser, by returning false from my event
    handler. (You could argue that this is evil, and I shouldn't
    mess up the keybindings that the user is used to, but personally
    I see this as making the user experience *more* consistent for
    me :) I've got this working in firefox and galeon, but not in
    konqueror, which still pops up a print dialog when I hit control-P.
    Can anyone suggest how to make this work in more browsers? My
    code is below.

    Thanks in advance!

    -Ben

    --------------------------------------------------------------------

    inp.onkeypress = function(e) {

    var code = 0;

    if (!e) { // IE
    e = window.event;
    }
    // IE has keyCode, Firefox has charCode
    try { code = e.charCode; } catch (foo) {}
    try { code = e.keyCode; } catch (foo) {}
    if (code==0) {try { code = e.which; } catch (foo) {}}
    // necessary for ctl keys in FF
    var enter = 13; // unicode for enter key
    var up_arrow = 38;
    var down_arrow = 40;
    var ch = String.fromCharCode(code);
    var go_up = (e.ctrlKey && ch=='p') || (code==up_arrow);
    var go_down = (e.ctrlKey && ch=='n') || (code==down_arrow);
    ...
    return (!(code==enter || go_up || go_down));
    // in these three cases, prevent the browser from doing
    // other things, e.g., printing if we do control-p
    };
     
    Ben Crowell, Apr 3, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TVIuIFVOREVSSElMTA==?=

    Inconsistent look between browsers

    =?Utf-8?B?TVIuIFVOREVSSElMTA==?=, Apr 20, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    279
    bruce barker
    Apr 20, 2004
  2. Piet
    Replies:
    0
    Views:
    547
  3. El Kabong

    Browsers, browsers! Quo vadis?

    El Kabong, May 11, 2007, in forum: HTML
    Replies:
    23
    Views:
    905
    dorayme
    May 13, 2007
  4. Network/Software Buyer
    Replies:
    0
    Views:
    420
    Network/Software Buyer
    May 23, 2010
  5. Charles Hixson
    Replies:
    9
    Views:
    215
    Rick Johnson
    Dec 28, 2011
Loading...

Share This Page