Path: csiph.com!feeder.erje.net!1.eu.feeder.erje.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Interrogation of Shift-Key state Date: Thu, 18 Aug 2016 20:29:10 +0200 Organization: PointedEars Software (PES) Lines: 87 Message-ID: <2365735.mvXUDI8C0e@PointedEars.de> References: <9a42c75f-3239-4a70-9ce3-7434db0edd00@googlegroups.com> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1471544951 19703 eJwNxcEBwCAMAsCVxAgx46S07j+Cvc8xBDmXqMXDM63aaAnNjrd6jkS4kPCfB2MTtWp8Rvm5BDAQPA== (18 Aug 2016 18:29:11 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Thu, 18 Aug 2016 18:29:11 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwFwYkBAzEIA7CVMJgn4zSm2X+EkzIKpWZlMV++3V773wP/sSbE3IHvPHcj8wbA3lD1zHFAUJjfpOkoBh86ERQx X-NNTP-Posting-Host: eJwFwYERwAAEA8CVuEowjqrsP0L/8dC5GQQDgjjuLKNlZhR9RNTnN8Yd6xaucPkK6LatH/7UEF0= Cancel-Lock: sha1:ddqUI02u5zt1aqxuswOKlAWUoLA= Xref: csiph.com comp.lang.javascript:31112 [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:" > > 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(", ") + "
\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.] > 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: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.