Page 1 of 1

Detecting keyPresses outside of LC

Posted: Fri Oct 12, 2018 3:33 pm
by dunbarx
This derives from a thread in this pane "Detect key events in Background", that speaks about detecting "ordinary" key presses, that is, not control keys like the optionKey or capsLock key, when LC is not in front.

It seems useful to open another thread, just so more people might see it and add to their toolbox.

In a stack with a button and two fields, in the button script:

Code: Select all

on mouseUp
   put "" into fld 2
   keyDetector
end mouseUp

on keyDetector
   if the commandKey is down then exit keyDetector --manually exit
   put the keysDown into fld 1
   if the keysDown = 120 then --detects "X"
      put "X detected!" into fld 2 -- your routine here
      exit keyDetector
   end if
   send "keyDetector" to me in 1
end keyDetector
Handlers to detect control keys are sprinkled throughout the forum, and examples can be found in the abovementioned thread.

Craig Newman

Re: Detecting keyPresses outside of LC

Posted: Fri Oct 12, 2018 4:08 pm
by bogs
Was going to say "Looks good Craig!" in the other thread, but since you started a new thread just for this, I'll put it here.

Looks good, Craig!

:D

Re: Detecting keyPresses outside of LC

Posted: Fri Oct 12, 2018 6:23 pm
by [-hh]
Craig.
That's not the whole story.

The keysDown detects multiple (simultaneous) pressed keys, I think up to 5 plus modifiers.
So if you need to detect any keypress of x = charToNum(120) you could write

Code: Select all

if 120 is among the items of the keysDown then ...
That's why I spoke of multiple triggers in the closing paragraph of
http://forums.livecode.com/viewtopic.ph ... 80#p172180
(where the keysDown is mentioned).

Re: Detecting keyPresses outside of LC

Posted: Fri Oct 12, 2018 6:55 pm
by dunbarx
Hermann.

You certainly did. I missed it in the other thread.

I hope the OP responds with his thoughts.

Craig

Re: Detecting keyPresses outside of LC

Posted: Fri Oct 12, 2018 8:21 pm
by [-hh]
Craig.

Moreover interesting with the keysDown is the fact that you have the keys recorded even if you catch all other (keyDown, keyUp, rawkeyDown, rawkeyUp, arrowKey, <special keys>) and don't pass them.

Re: Detecting keyPresses outside of LC

Posted: Fri Oct 12, 2018 9:03 pm
by FourthWorld
dunbarx wrote:
Fri Oct 12, 2018 6:55 pm
I missed it in the other thread.
Happens a lot with topic sprawl, which is why it's really great to keep related posts together.

Should I merge this thread with that one?

Re: Detecting keyPresses outside of LC

Posted: Fri Oct 12, 2018 9:28 pm
by dunbarx
Richard.

There are reasons to do so, but I started the new thread just because the subject would have been close up for anyone interested in what the title said. Merging makes sense, but then we have a long thread where the current state of the discussion would be well down the line.

in other words, do what you think best. :D

We are still waiting for the OP to congratulate us. Something in all this should solve his issue. :wink:

Craig