Detecting keyPresses outside of LC

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Detecting keyPresses outside of LC

Post by dunbarx » Fri Oct 12, 2018 3:33 pm

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

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Detecting keyPresses outside of LC

Post by bogs » Fri Oct 12, 2018 4:08 pm

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
Image

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Detecting keyPresses outside of LC

Post by [-hh] » Fri Oct 12, 2018 6:23 pm

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).
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Detecting keyPresses outside of LC

Post by dunbarx » Fri Oct 12, 2018 6:55 pm

Hermann.

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

I hope the OP responds with his thoughts.

Craig

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Detecting keyPresses outside of LC

Post by [-hh] » Fri Oct 12, 2018 8:21 pm

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.
shiftLock happens

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Detecting keyPresses outside of LC

Post by FourthWorld » Fri Oct 12, 2018 9:03 pm

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?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Detecting keyPresses outside of LC

Post by dunbarx » Fri Oct 12, 2018 9:28 pm

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

Post Reply

Return to “Talking LiveCode”