Testing for shift or ctrl within a click event.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jhcorey@yahoo.com

    #1

    Testing for shift or ctrl within a click event.

    Right now I have code like this which works in IE (this fires on an
    onclick):

    function f_Click(x)
    {

    if (window.event.s hiftKey)
    {

    I'd like to find the simplest way to make this work cross-browser. In
    another post I found:

    document.onkeyd own = function (e) {
    evt = e || event;
    if (evt.shiftKey) alert('shift');
    }

    But
    1) I haven't figured out how to return the result into a variable.
    2) I notice that once this code executes, it will execute again just by
    hitting the shift button, without clicking.

    Thanks

  • RobB

    #2
    Re: Testing for shift or ctrl within a click event.

    jhco...@yahoo.c om wrote:[color=blue]
    > Right now I have code like this which works in IE (this fires on an
    > onclick):
    >
    > function f_Click(x)
    > {
    >
    > if (window.event.s hiftKey)
    > {
    >
    > I'd like to find the simplest way to make this work cross-browser.[/color]
    In[color=blue]
    > another post I found:
    >
    > document.onkeyd own = function (e) {
    > evt = e || event;
    > if (evt.shiftKey) alert('shift');
    > }
    >
    > But
    > 1) I haven't figured out how to return the result into a variable.
    > 2) I notice that once this code executes, it will execute again just[/color]
    by[color=blue]
    > hitting the shift button, without clicking.
    >
    > Thanks[/color]

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">

    <html>
    <head>
    <title>untitled </title>
    <script type="text/javascript">

    document.onclic k = function(e)
    {
    e = e || window.event;
    if (e.shiftKey)
    {
    d = document;;
    d.getElementByI d('r').appendCh ild(d.createTex tNode('!'));
    }
    }

    </script>
    </head>
    <body>
    <span id="r" style="font:64p x arial;"></span>
    </body>
    </html>

    Comment

    • jhcorey@yahoo.com

      #3
      Re: Testing for shift or ctrl within a click event.

      Thanks, but I have the possibility of clicking in other part of the
      page (this is a tree control), and enabling an event handler here
      doesn't work for me, since whatever it does may happen when I click
      something else.

      I also tried putting the handler somewhere else, but it executes after
      my f_click function, so it doesn't work for me.

      The IE code seems to simply be a property. I would like something that
      simple, but my next thought is to write something using onkeypress and
      onkeyup to set a variable indicating if one of these keys is down.
      Thanks,
      Jim

      Comment

      Working...