How to detect if Ctrl key pressed on start of an app

Started by ubacher, October 02, 2017, 07:41:17 PM

Previous topic - Next topic

ubacher

For some apps (modal, those with little or no user intervention) one could modify their behaviour if CTRL (or Shift or...) is pressed when initiated.

How does one detect a pressed key - I assume one needs to do this "on loading" the html document?

(This is a different situation than detecting that a key was pressed after the html page has been loaded.)




Mario

Your app can only detect the state of keys when it has started.

The jQuery $(document).ready( event handler just tells you that all the DOM is ready and everything has been loaded. From that time on you can access the DOM (HTML) and manipulate it.
You can check for keyboard states much earlier in your <script> block. The script block itself is executed immediately, while the HTML is still loading. Just figure out which event you want to react on. Numerous resources for that on the net.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

ubacher

#2
I have found the following code to work:
$(document).ready(function () {
            document.body.onkeydown = function (e) {
               ('#info').text(String.fromCharCode(e.keyCode) + " --> " + e.keyCode)
                // 16 = shift
                // 17 = Ctrl
                // 18 = Alt
            };


However: This code is also asynchronous - it does not execute at the time it is called.
This makes in unsuitable for my purpose: I can not check if a key was pressed when I start my code.
Looking for other ways.....