Setting up hotkeys using onKeyPress

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hasan Ammar

    Setting up hotkeys using onKeyPress

    Is it possible to set up hotkeys using onkeypress? I know it can be
    done with the usual alphanumeric keys, but what about function keys?
    or using ctrl/alt combinations? Does anybody have a tutorial/guide?
  • Evertjan.

    #2
    Re: Setting up hotkeys using onKeyPress

    Hasan Ammar wrote on 18 aug 2004 in comp.lang.javas cript:
    [color=blue]
    > Is it possible to set up hotkeys using onkeypress? I know it can be
    > done with the usual alphanumeric keys, but what about function keys?
    > or using ctrl/alt combinations? Does anybody have a tutorial/guide?[/color]

    I use this code with onkeydown(!!!), also for F5/6/7/8.

    [IE only, because the page is for my personal administrator use only]
    [The alert()s are leftovers from the debugging fase]

    onkeydown='druk ()'

    .....

    function druk(){
    x=event.keyCode
    //alert(x)
    if((x==8)||(x== 32)){ // bs or space
    doenv(ouderbnum )}
    else if (x==27){ // esc
    location.reload ()}
    else if (x==37){ // li
    doenv(tvorig)}
    else if (x==39){ // re
    doenv(tvolgend) }
    else if (x==40){ // dn
    doenv(svorig)}
    else if (x==38){ // up
    doenv(svolgend) }
    else if (x==36){ // back numb
    doenv(bnumdef)}
    else if (x==35){ // help
    helpen()}
    else if (x==45){ // toggle test
    text()}
    else if (x==116){ // F5
    doen(onder[0])}
    else if (x==117){ // F6
    doen(onder[1])}
    else if (x==118){ // F7
    doen(onder[2])}
    else if (x==119){ // F8
    doen(onder[3])}
    //else {alert(x)}
    }

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Robert

      #3
      Re: Setting up hotkeys using onKeyPress

      ammarh@gmail.co m (Hasan Ammar) wrote in message news:<e9008f67. 0408172342.57d1 4dde@posting.go ogle.com>...[color=blue]
      > Is it possible to set up hotkeys using onkeypress? I know it can be
      > done with the usual alphanumeric keys, but what about function keys?
      > or using ctrl/alt combinations?[/color]


      PointedEars provied this link to a post titled "Re: mouseless
      navigation" the link is:
      <http://www.w3.org/TR/html4/interact/forms.html#adef-accesskey>

      I copied the following from the article:

      In this example, we assign an access key to a link defined by the A
      element. Typing this access key takes the user to another document, in
      this case, a table of contents.

      <P><A accesskey="C"
      rel="contents"
      href="http://someplace.com/specification/contents.html">
      Table of Contents</A>

      The invocation of access keys depends on the underlying system. For
      instance, on machines running MS Windows, one generally has to press
      the "alt" key in addition to the access key. On Apple systems, one
      generally has to press the "cmd" key in addition to the access key.


      I wrote up a Javascript program for detecting key presses too. It has
      a Netscape path.

      The function is written so it could be invoked by setting the
      document.onkeyp ress global variable.

      Looks like you can either set the event handler via the
      statement below or in the onkeypress event handler of the
      html body statement. If you use the statement below, IE
      will not be passing the event variable to the function
      processKey and you will have to preference window to
      the event variables for the IE portion of the code.

      document.onkeyp ress = processKey;


      Robert

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
      <head>
      <title>onkeypre ss example</title>

      <SCRIPT type="text/javascript">
      function processKey(even t)
      {
      if (typeof event == "object" &&
      typeof event.which == "number")
      {
      // Netscape style event
      alert("Netscape : event.which = " + typeof event.which +
      " " + event.which + " letter is " +
      String.fromChar Code(event.whic h));

      alert("event.sh iftKey = " + event.shiftKey +
      " event.ctrlKey = " + event.ctrlKey +
      " event.altKey = " + event.altKey +
      " event.metaKey = " + event.metaKey);


      //control-y goes to yahoo
      if (event.which == 121 && event.ctrlKey == true)
      {
      location.replac e("http://www.yahoo.com") ;
      return false;
      }

      }
      else
      {
      // We assume IE
      /* ***
      // Doing an alert seems to confuse IE.
      // The event will be changed after the alert.
      alert("IE: window.event.ke yCode = " +
      window.event.ke yCode +
      " letter is " +
      String.fromChar Code(window.eve nt.keyCode) +
      "\n" +
      " window.event.sh iftKey = " + window.event.sh iftKey +
      " window.event.ct rlKey = " + window.event.ct rlKey +
      " window.event.al tKey = " + window.event.al tKey +
      "\n" +
      " ... Warning: " +
      "IE event handle is confused after this alert");
      *** */


      //control-y goes to yahoo
      if (window.event.k eyCode == 25 )
      {
      location.replac e("http://www.yahoo.com") ;
      window.event.re turnValue = false;
      return;
      }


      }

      return true;
      }

      </SCRIPT>
      </head>
      <body onkeypress='ret urn processKey(even t);'>
      <p>Lets look for a key press.</p>
      <form>
      <input type=text size=20>
      </form>
      </body>

      Comment

      Working...