Page 1 of 1
Detect Key events in background
Posted: Thu Oct 11, 2018 10:21 am
by per_bodelius
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... )
Re: Detect Key events in background
Posted: Thu Oct 11, 2018 3:29 pm
by dunbarx
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
Re: Detect Key events in background
Posted: Thu Oct 11, 2018 7:51 pm
by [-hh]
@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).
Re: Detect Key events in background
Posted: Thu Oct 11, 2018 8:37 pm
by bogs
Now that is pretty cool!
Re: Detect Key events in background
Posted: Fri Oct 12, 2018 12:00 am
by dunbarx
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
Re: Detect Key events in background
Posted: Fri Oct 12, 2018 3:50 am
by [-hh]
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.
Re: Detect Key events in background
Posted: Fri Oct 12, 2018 4:47 am
by dunbarx
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.
Craig

Re: Detect Key events in background
Posted: Fri Oct 12, 2018 6:03 am
by dunbarx
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.
Craig
Re: Detect Key events in background
Posted: Sun Oct 14, 2018 4:13 pm
by per_bodelius
Thanks everybody for your engagement in my question

! Dunbarx, I´ve tested your code and it works fine for me. Some extra

for you. Thanks!