Friday, December 15, 2017

#1 2015-09-15 03:03:51 am

Joseph25b
Member
Registered: 2015-08-31
Posts: 31

Prevent modifierKeys responding

Hello,

In my app, while a process which can take from a fraction of second to almost 2 minutes (depending user's choices), I use NSEvent's modifierFlags() in the loop to catch the Cmd+ Crtl keydown to cancel the action.
Well, BUT, even when I give the focus to another app, mine catches the modifiersKey strokes and react unexpectedly.

I searched a while how to fix this behaviour, but was incapable to manage it sad
Any help, please ?

With regards
Joseph

(Xcode 4.6.3 with Lion 10.7.5 on MacPro)


Joseph from France
MacPro 10.7.5 - XCode 4.6.3

Offline

 

#2 2015-09-15 11:22:36 am

Joseph25b
Member
Registered: 2015-08-31
Posts: 31

Re: Prevent modifierKeys responding

even when I give the focus to another app, mine catches the modifiersKey strokes and react unexpectedly.


Ok, I used this very Applescript way to manage my request :

Applescript:


repeat
-- in the curse of the loop
set modifierNumber to current application's NSEvent's modifierFlags()
if modifierNumber = 1310720 then -- CmdKey + Ctrl keydown = to cancel operation
tell application "System Events" to set frontApp to name of first process where frontmost is true
    if frontApp = myAppName then -- the var contains the actual name of this application
    display alert "User cancelled the process!" buttons {"OK"} default button 1
    return "Canceled" -- this result returned to its parent handler triggers the actual cancelling process
   end if
end if
-- the rest of the loop handler following

Probably exists a more "Cocoa" way, but this  does the trick…

Joseph

Model: MacPro
Browser: Safari 537.78.2
Operating System: Mac OS X (10.7)


Joseph from France
MacPro 10.7.5 - XCode 4.6.3

Offline

 

#3 2015-09-15 07:37:54 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: Prevent modifierKeys responding

Rather than a tight loop, you might be better to use a timer that runs every 0.x seconds. And you can use something like this:

Applescript:

if (current application's NSApp's isActive()) as boolean then


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#4 2015-09-16 04:02:08 am

Joseph25b
Member
Registered: 2015-08-31
Posts: 31

Re: Prevent modifierKeys responding

Hello Shane,

Shane Stanley wrote:

Rather than a tight loop, you might be better to use a timer that runs every 0.x seconds. And you can use something like this:

Applescript:

if (current application's NSApp's isActive()) as boolean then


Certainly less consuming proc resource into the loop!
I'm just puzzled  about how to manage it, but I'll try…
Must I insert this timer in a <on idle / end idle> handler?

Thank you for the advice.

Best regards
Joseph

Model: MacPro & Xcode 4.6.3
Browser: Safari 537.78.2
Operating System: Mac OS X (10.7)


Joseph from France
MacPro 10.7.5 - XCode 4.6.3

Offline

 

#5 2015-09-17 06:47:47 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: Prevent modifierKeys responding

Joseph25b wrote:

Must I insert this timer in a <on idle / end idle> handler?


No -- you just trigger it when your process starts, and invalidate it when it finishes.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#6 2015-09-21 02:53:57 pm

Joseph25b
Member
Registered: 2015-08-31
Posts: 31

Re: Prevent modifierKeys responding

Hello Shane,

Shane Stanley wrote:

Rather than a tight loop, you might be better to use a timer that runs every 0.x seconds.


With help of your «AppleScriptObjC Explored» (definitely the must-have book to jump into Xcode from vanilla AppleScripting) I could figure out how it worked.
Naively I expected that a Timer could behave as background temporiser which could insert his action at intervals through a running loop. Well, it's the loop or the timer, not them together.
So my code needs some rearrangement to pass from one way to the other, but it's not too complicated.

My question is about the timing when choosing a timer to behave like a loop. Because it seems quite unpredictable to me.
Let's figure I put 0.001 sec as delay and the process that the timer must manage takes eventually 0.025 sec and will be triggered hundreds of times?
What will happen? Any risk of CPU or memory overflowed?


Joseph from France
MacPro 10.7.5 - XCode 4.6.3

Offline

 

#7 2015-09-21 06:40:30 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: Prevent modifierKeys responding

Joseph25b wrote:

Let's figure I put 0.001 sec as delay and the process that the timer must manage takes eventually 0.025 sec and will be triggered hundreds of times?
What will happen? Any risk of CPU or memory overflowed?


You'll probably flood the event queue, resulting in a spinning cursor and worse.

You should rarely need to check anything that often -- checking for a key-press is something for which 0.1 seconds is probably more than adequate. But if you have a situation where you are worried about timers firing before a triggered handler is finished, the best approach is not to use a repeating timer. Instead, just create a new single-shot timer at the end of the handler each time it's finished its work.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#8 2015-09-22 09:07:17 am

Joseph25b
Member
Registered: 2015-08-31
Posts: 31

Re: Prevent modifierKeys responding

Shane Stanley wrote:

You should rarely need to check anything that often -- checking for a key-press is something for which 0.1 seconds is probably more than adequate. But if you have a situation where you are worried about timers firing before a triggered handler is finished, the best approach is not to use a repeating timer. Instead, just create a new single-shot timer at the end of the handler each time it's finished its work.


OK, I agree that my example was voluntarily exaggerated, but better be aware of possible issues when trying to speed-up artificially the process.


Joseph from France
MacPro 10.7.5 - XCode 4.6.3

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)