Detect Key events in background

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

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller, kevinmiller

Post Reply
per_bodelius
Posts: 7
Joined: Mon Mar 19, 2018 11:25 pm

Detect Key events in background

Post by per_bodelius » Thu Oct 11, 2018 10:21 am

Hi,
I´m trying do develop an application that saves my screen while pressing a specific key on keyboard.
There are no problem to do this as long as my application is on top. But if I try to hide my app or try to run in background it doesn´t work with the key.
I have also tried with an automatic save loop like this:

Code: Select all

 
  repeat with m=1 to 4
      export snapshot from rect "0,0,800,800" to file "Bild" & m &".png" as PNG
      wait 4 seconds
      end repeat
 

and it works fine while running in background. So the problem is to detect the key in this "mode". But how?
All suggestions are appreciated!
( sorry, forgot to mention it is on Windows10 I´ve tested... )

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

Re: Detect Key events in background

Post by dunbarx » Thu Oct 11, 2018 3:29 pm

Hi.

Though LC can run your loop just fine in the background, I do not think it will notice mouse or key events when not on top.

But if it is automatically doing its thing all by itself, why do you need to "tell" it to do it again with a keystroke?

Craig Newman

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

Re: Detect Key events in background

Post by [-hh] » Thu Oct 11, 2018 7:51 pm

@Craig: One of the very rare cases where you are wrong.

Recently I did something similar.

Here is a complete example code. Say your trigger is the capslockKey.
Make a stack with one locked field for testing and save as standalone.

Code: Select all

on openstack
  loopKey
end openstack

on loopKey
  put the internet date &cr&the capslockKey into fld 1
  send "loopKey" to me in \
      1000 - (the millisecs mod 1000) millisecs
end loopKey

on closeStack
  repeat 2
    repeat for each line L in the pendingMessages
      if "loopkey" is item 3 of L then cancel item 1 of L
    end repeat
  end repeat
end closeStack
This reports, CPU-friendly, every full second the state of your capslockKey (down or up).
This works also if your app is hidden or in background.
(If the window is not hidden you can see the digital clock working ...)

For a different key (or multiple keys) hold this key (or these keys) down long enough so that the seconds cycle can catch it.
Use above "the keysDown" instead of "the capslockKey" to detect the keyCode(s) of any trigger(s).
shiftLock happens

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

Re: Detect Key events in background

Post by bogs » Thu Oct 11, 2018 8:37 pm

Now that is pretty cool!
Image

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

Re: Detect Key events in background

Post by dunbarx » Fri Oct 12, 2018 12:00 am

Hello, Hermann.

You are so right, as usual.
And all you really need is this:

Code: Select all

on openstack
  loopKey
end openstack

on loopKey
   put the internet date &cr&the optionKey into fld 1
   send "loopKey" to me in 1 second
end loopKey
Craig

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

Re: Detect Key events in background

Post by [-hh] » Fri Oct 12, 2018 3:50 am

send "loopKey" to me in 1 second.
Craig.
Hold (at least on MacOS) the menu of any app open for a short time and your digit clock will be out of seconds-sync with the system time.
shiftLock happens

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

Re: Detect Key events in background

Post by dunbarx » Fri Oct 12, 2018 4:47 am

Hermann.

You know, I did not have my brain in gear.

I and others have made handlers in the past that react to control key events outside LC (or a key like capsLock that has its own function) but never an "ordinary" key, perhaps, the "X" key. The OP indicated he wanted to detect a "specific" key.

We have all written about things like this:

Code: Select all

on mouseUp
   getGoing
end mouseUp

on getGoing
   if the commandKey is down then exit getGoing
   if the optionKey is down then  put random(99) into fld 1
   send "getGoing" to me in 10
end getGoing
Similar to the one I posted

Anyway, I had made a small test stack, to try to use the "rawKeyDown" message somehow to detect "ordinary" keypresses outside of LC. I did not work it exhaustively, but failed. Maybe the idea itself, to use a rawKeyDown-type of handler is flawed. But what else might do?

I assumed that your pendingMessages gadget somehow did the "X" thing. I did not test it earlier. How would you detect "X" as opposed to the optionKey, or something similar.

I could not figure out a way to do it, which is why I posted as I did. It would be a treat to figure out how to simulate the "rawKeyWhatever" message from within a running handler. Or some other method to detect ordinary keypresses from outside.

Common, Herman. You can do this. 8)

Craig :wink:

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

Re: Detect Key events in background

Post by dunbarx » Fri Oct 12, 2018 6:03 am

So I says to myself, "Self? What did Hermann (and you) say about the capsLockKey? That it had its own function?

Hmmm. So I says to myself, "Self?" How about this:

Code: Select all

on mouseUp
   getGoing
end mouseUp

on getGoing
   if the commandKey is down then exit getGoing
   put the keysDown into fld 1
  if fld 1 = 120 then answer "you did it!" --put random(999) into line 2 of fld 1
   send "getGoing" to me in 11
end getGoing
This works everywhere.

Oh, and it just confirms that, as Hermann mentioned, I was wrong in my earlier post. 8)

Craig

per_bodelius
Posts: 7
Joined: Mon Mar 19, 2018 11:25 pm

Re: Detect Key events in background

Post by per_bodelius » Sun Oct 14, 2018 4:13 pm

Thanks everybody for your engagement in my question :D ! Dunbarx, I´ve tested your code and it works fine for me. Some extra :D :D for you. Thanks!

Post Reply

Return to “Talking LiveCode”