Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #31112

Re: Interrogation of Shift-Key state

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Interrogation of Shift-Key state
Date 2016-08-18 20:29 +0200
Organization PointedEars Software (PES)
Message-ID <2365735.mvXUDI8C0e@PointedEars.de> (permalink)
References <nklv6p$eap$1@gioia.aioe.org> <9a42c75f-3239-4a70-9ce3-7434db0edd00@googlegroups.com> <nkmg47$1b8k$2@gioia.aioe.org> <d052c5aa-94b2-49f9-a493-ea81296acba9@googlegroups.com>

Show all headers | View raw


[Catching up on some old threads, with updated scorefile, in a break.]

Michael Haufe (TNO) wrote:

> On Saturday, June 25, 2016 at 12:47:58 PM UTC-5, Janis wrote:
>> If it "can be so simple", would it be possible to just post the way to
>> go, please?
> 
> The point is that it depends. From your question:
> 
> "I want to interrogate the boolean state of the Shift-Key (pressed or
> not)."
> 
> But from the reference I provided you can see that:
> 
> "When a key is pressed and held down, it begins to auto-repeat. This
> results in a sequence of events similar to the following being
> dispatched:"
> 
> <https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Auto-repeat_handling>

Auto-repeat is not supposed to happen for modifier keys like the Shift key.  
(Because) there is no logic in that: auto-repeat is a keyboard driver 
feature that is intended to make typing on a computer keyboard like on a 
typewriter easier.  For example, if you need to type a separator line of 
dashes or equals signs in a plain-text document, or quickly remove a lot of 
characters with Backspace/Delete; also, it is intended to make scrolling 
with the arrow keys or the Page Up/Down keys easier.

Some (IMHO borken) keyboard drivers (for Windows) do it anyway.  But even 
with auto-repeat, while there are several “keydown” and, for non-character 
keys, “keypress”, events for the same key code, there is only one “keyup” 
event when the key is released.  Keyboard events in Web user agents 
certainly do not range among the best pieces of software design, but they 
are not *that* ill-designed either.

This can be easily tested, for example with

  document.onkeydown = document.onkeyup = document.onkeypress =
    function (e) {
      console.log(e.type, e.keyCode, e.charCode);
    };

or, in older user agents:
  
  document.body.innerHTML = "";
  document.onkeydown = document.onkeyup = document.onkeypress =
    function (e) {
      if (!e) e = window.event;
      document.body.innerHTML +=
        ([e.type, e.keyCode, e.charCode].join(", ") + "<br>\n");
    };

> So depending on your platform targets, there is a good chance you're going
> to be getting some contradictory information in some cases (keydown
> followed by a keypress and possibly by keyup somewhere in there in
> repetition)

Name one.
 
> Which means you are now looking at the keypress event. Luckily, the event
> that is raised has a property called "shiftKey" which you can interrogate.

The *proprietary/legacy* “keypress” event is not fired for non-character 
keys; never has been.  This is how you can tell apart, for example, the 
right arrow key ({keyCode: 39, charCode: 0}) and the apostrophe key 
({keyCode: 39, charCode: 39}}; on my laptop with Swiss German keyboard 
layout, YMMV).

[The MSHTML DOM event model up to including IE 8 (and IE 9 in Compatibility 
Mode) is a “conflated” one (see “Legacy key models” in the specification 
below) that does not have the “charCode” property.  Just confirmed again in 
resurrected IE 6 to 8, thanks to ievms.]

<https://www.w3.org/TR/uievents/>

> So now your left with the problem of telling the difference between
> someone holding down the shift key and someone mashing on the keyboard in
> succession.

Most certainly not.
 
-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Interrogation of Shift-Key state Janis Papanagnou <janis_papanagnou@hotmail.com> - 2016-06-25 14:59 +0200
  Re: Interrogation of Shift-Key state Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-06-25 17:18 +0200
    Re: Interrogation of Shift-Key state Janis Papanagnou <janis_papanagnou@hotmail.com> - 2016-06-25 19:47 +0200
      Re: Interrogation of Shift-Key state Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-06-25 20:07 +0200
  Re: Interrogation of Shift-Key state "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-06-25 08:51 -0700
    Re: Interrogation of Shift-Key state Janis Papanagnou <janis_papanagnou@hotmail.com> - 2016-06-25 19:47 +0200
      Re: Interrogation of Shift-Key state Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-06-25 20:17 +0200
        Re: Interrogation of Shift-Key state Janis Papanagnou <janis_papanagnou@hotmail.com> - 2016-06-25 21:16 +0200
          Re: Interrogation of Shift-Key state Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-06-25 22:33 +0200
            Re: Interrogation of Shift-Key state Janis Papanagnou <janis_papanagnou@hotmail.com> - 2016-06-26 00:49 +0200
              Re: Interrogation of Shift-Key state Tim Streater <timstreater@greenbee.net> - 2016-06-26 00:06 +0100
              Re: Interrogation of Shift-Key state Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-06-26 11:05 +0200
      Re: Interrogation of Shift-Key state "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-06-25 12:17 -0700
        Re: Interrogation of Shift-Key state "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-06-25 12:23 -0700
        Re: Interrogation of Shift-Key state Janis Papanagnou <janis_papanagnou@hotmail.com> - 2016-06-26 00:34 +0200
        Re: Interrogation of Shift-Key state Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-08-18 20:29 +0200
          Re: Interrogation of Shift-Key state "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-08-18 15:17 -0700
            Re: Interrogation of Shift-Key state Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-08-20 07:23 +0200
              Re: Interrogation of Shift-Key state "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-08-20 00:03 -0700
                Re: Interrogation of Shift-Key state Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-08-20 11:48 +0200
                Re: Interrogation of Shift-Key state "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-08-25 13:39 -0700
      Re: Interrogation of Shift-Key state "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-06-25 12:20 -0700
      Re: Interrogation of Shift-Key state Tim Streater <timstreater@greenbee.net> - 2016-06-25 22:33 +0100
        Re: Interrogation of Shift-Key state Janis Papanagnou <janis_papanagnou@hotmail.com> - 2016-06-26 01:18 +0200
          Re: Interrogation of Shift-Key state Tim Streater <timstreater@greenbee.net> - 2016-06-26 00:23 +0100
            Re: Interrogation of Shift-Key state Janis Papanagnou <janis_papanagnou@hotmail.com> - 2016-06-26 01:37 +0200
              Re: Interrogation of Shift-Key state Tim Streater <timstreater@greenbee.net> - 2016-06-26 09:41 +0100
  Re: Interrogation of Shift-Key state - workaround Janis Papanagnou <janis_papanagnou@hotmail.com> - 2016-06-26 00:33 +0200
    Re: Interrogation of Shift-Key state - workaround "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-06-26 00:53 +0200
      Re: Interrogation of Shift-Key state - workaround Janis Papanagnou <janis_papanagnou@hotmail.com> - 2016-06-26 01:31 +0200
        Re: Interrogation of Shift-Key state - workaround "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-06-25 16:52 -0700
        Re: Interrogation of Shift-Key state - workaround "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-06-26 01:56 +0200
    Re: Interrogation of Shift-Key state - workaround "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-06-25 16:50 -0700
  Re: Interrogation of Shift-Key state Joao Rodrigues <jr@none.com> - 2016-06-25 20:34 -0300

csiph-web