keypress event -> control characters

Discussion in 'Javascript' started by phil_gg04@treefic.com, Mar 12, 2005.

  1. Guest

    Dear Javascript Experts,

    I'm currently implementing Anyterm, a terminal emulator on a web page.
    It consists of an Apache module, some XmlHTTP and a bit of Javascript.
    The idea is to give you shell access to your server from (almost)
    anywhere; other solutions to the problem tend to require access to
    ports other than 80 and Java. The very primitive first attempt is at
    http://chezphil.org/anyterm/. A more functional version will be
    available soon.

    My "problem for the day" is how to send control characters. For
    example, if the user types Ctrl-D I need to send byte 4 to the server.
    There are two sub-problems: first, some of these actions will be
    intercepted by the browser so I need to do anything I can to avoid that
    and/or provide an alternative entry mechanism (eg press ALT for CTRL).
    Second, I need a piece of reasonably browser-independent code that
    takes a keypress event, looks at all of its properties, and returns a
    byte.

    I'm hoping that someone has already done this and can point me at some
    existing code. Otherwise, all suggestions are welcome.

    Regards,

    --Phil.
    , Mar 12, 2005
    #1
    1. Advertising

  2. Diego Vilar Guest

    Hi Phil,

    Guess it can be done, but forget about onkeypress.... you would need
    onkeydown, since onkeypress only fires for a limited set of chars....
    onkeydown lets u catch most of the keys before the browser does... ALT
    and CTRL could be detected by reading object event's specific
    properties for those keys (altKey, altLeft, ctrlKey, ctrlLeft) in the
    onkeydown event handler.. Well, I dont really do cross-browser
    scripts... I´m kinda of an IE guy cause thats the enviroment used for
    my company, but read some Javascript papers and Netscape's DOM on those
    events.. that should not be dificult.

    Anyway,
    http://msdn.microsoft.com/workshop/author/dhtml/reference/events/onkeydown.asp?frame=true

    [ ]'s
    Diego
    Diego Vilar, Mar 12, 2005
    #2
    1. Advertising

  3. Guest

    Replying to my own question...

    > I need a piece of reasonably browser-independent code that
    > takes a keypress event, looks at all of its properties, and returns a
    > byte. [for key combinations including control sequences]


    Well as Diego points out onkeypress doesn't do control keys, but on the
    other hand if I use onkeydown I have to map shift+a to capital A etc
    myself. So I'm now using the following code that uses both onkeypress
    and onkeydown; onkeypress is used in most cases but ignores evens when
    ctrl is pressed while onkeydown detects only those. This seems to work
    in Firefox1.0 and IE6, though in FF the browser default actions (e.g.
    Ctrl-A = select all) still seem to fire; can I stop that? I also need
    to worry about keys like PgUp/PgDn, arrows etc. Any improvements
    welcomed.

    function keypress(ev) {
    if (!ev) var ev=window.event;

    if (ev.ctrlKey) {
    return;
    }

    var kc;
    if (ev.keyCode) kc=ev.keyCode;
    if (ev.which) kc=ev.which;
    //if (kc==13) kc=10;
    var k=String.fromCharCode(kc);
    process_key(k);

    ev.cancelBubble=true;
    if (ev.stopPropagation) ev.stopPropagation();
    return false;
    }


    function keydown(ev) {
    if (!ev) var ev=window.event;

    if (!ev.ctrlKey || ev.keyCode==17) {
    return;
    }

    var kc=ev.keyCode;
    process_key(String.fromCharCode(kc-64));

    ev.cancelBubble=true;
    if (ev.stopPropagation) ev.stopPropagation();
    return false;
    }


    document.onkeypress=keypress;
    document.onkeydown=keydown;



    Many thanks,

    --Phil.
    , Mar 13, 2005
    #3
  4. wrote:

    > though in FF the browser default actions (e.g.
    > Ctrl-A = select all) still seem to fire; can I stop that?


    You could at least try to use the proper code for that
    if (ev.preventDefault) {
    ev.preventDefault();
    }
    in DOM Level 2:
    <http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-Event>
    Note that I haven't checked whether that prevents the text selection in
    Mozilla.




    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Mar 13, 2005
    #4
  5. Guest

    >> the browser default actions (e.g. Ctrl-A = select all) still seem to
    fire
    > ev.preventDefault();


    You're right, I should do that. But unfortunately it doesn't seem to
    help with ctrl-A.

    --Phil.
    , Mar 13, 2005
    #5
    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?bWc=?=

    keypress event of TextBox - WebForm

    =?Utf-8?B?bWc=?=, Feb 8, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    12,221
    Marshal Antony
    Feb 8, 2004
  2. =?Utf-8?B?QXNzYW5LaGFuIElzbWFpbA==?=

    How to invoke keyPress event in ASP.Net pages

    =?Utf-8?B?QXNzYW5LaGFuIElzbWFpbA==?=, Oct 17, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    6,126
    Gary K
    Oct 17, 2005
  3. =?Utf-8?B?RHVzdHkgSGFja25leQ==?=

    KeyPress Event

    =?Utf-8?B?RHVzdHkgSGFja25leQ==?=, Feb 2, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    598
    Noldis Chumacero
    Feb 2, 2006
  4. Alper OZGUR

    KeyPress Event

    Alper OZGUR, Feb 14, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    483
    Alper OZGUR
    Feb 14, 2006
  5. Replies:
    2
    Views:
    699
    Patrice
    Dec 19, 2007
Loading...

Share This Page