{"id":1477,"date":"2008-12-08T12:45:56","date_gmt":"2008-12-08T15:45:56","guid":{"rendered":"http:\/\/brockerhoff.net\/bb\/viewtopic.php?p=2600"},"modified":"2010-05-08T12:20:48","modified_gmt":"2010-05-08T15:20:48","slug":"re-cocoa-musings-pt-1-2","status":"publish","type":"post","link":"https:\/\/brockerhoff.net\/blog\/2008\/12\/08\/re-cocoa-musings-pt-1-2\/","title":{"rendered":"Re: Cocoa musings pt.1"},"content":{"rendered":"<p>Just saw <a href=\"http:\/\/menumachine.com\/blog\/2008\/11\/wrapster-for-coda\/\">Rob Keniger&#8217;s Wrapster plug-in for Coda<\/a>, an interesting use of event taps; it&#8217;s a plug-in that intercepts key events for its master application.<\/p>\n<p>The version I looked at (1.2) uses a global tap and checks if its master application is active:<\/p>\n<pre><code>\u00a0 \u00a0if(![NSApp isActive]) {\r\n\u00a0 \u00a0\u00a0 \u00a0return event;\r\n\u00a0 \u00a0}\r\n<\/code><\/pre>\n<p>Of course, it would be conceptually more reliable to obtain the event&#8217;s destination process PSN and test against that; but the best way would be to tap the current process like this:<\/p>\n<pre><code>\u00a0 \u00a0ProcessSerialNumber psn = {kNoProcess, kCurrentProcess};\r\n\u00a0 \u00a0CFMachPortRef tapg = CGEventTapCreateForPSN(&amp;psn, kCGTailAppendEventTap, kCGEventTapOptionDefault, CGEventMaskBit(kCGEventKeyDown), ProcessEvent, NULL);\r\n<\/code><\/pre>\n<p>Global event taps are a great tool, but they&#8217;re also easy to abuse, and I suppose that in a year or two we might have dozens of utilities or plug-ins tapping events; the actual results might be dependent on the order they run or load, for instance, and overall responsiveness might suffer. Therefore, please make absolutely sure that you need a global tap to do whatever you want to do, that it takes as little time as possible, and that it does careful parameter checking.<\/p>\n<p>One great application to test event taps (and to check their presence) is <a href=\"http:\/\/prefabsoftware.com\/eventtapstestbench\/\">Event Tap Testbench<\/a>. Its author, Bill Cheeseman, has helped me a lot with subtle details of both event taps and the Accessibility APIs; thanks Bill! Do also check out their other products.<\/p>\n<p>Rob tests if Accessibility is enabled by trying to create a tap and seeing if it succeeds. While this works, I think it&#8217;s easier to do it like this:<\/p>\n<pre><code>\u00a0 \u00a0if (!AXAPIEnabled()&amp;&amp;!AXIsProcessTrusted()) {\r\n\u00a0 \u00a0\u00a0 \u00a0\/\/ error dialog here\r\n\u00a0 \u00a0}\r\n<\/code><\/pre>\n<p>The first call checks if Accessibility is enabled in System Preferences; the second checks if your process is allowed to set a key tap even with Accessibility disabled. To achieve this, you must (from a process running as root) call AXMakeProcessTrusted() on the tapping application&#8217;s executable &#8211; this will take effect on its next run.<\/p>\n<p>In the case of <a href=\"\/klicko\">Klicko<\/a>, it taps only mouse button events, for which Accessibility doesn&#8217;t need to be on; however, it uses the Accessibility APIs to check out details about the clicked-on windows. Some people dislike turning Accessibility on in System Preferences, so a future version of Klicko might use AXMakeProcessTrusted() to avoid this; this means asking the user for an Administrator password and running a separate tool, like <a href=\"\/quay\">Quay<\/a> does. I&#8217;m still evaluating the trade-offs for that: it precludes running Klicko from ~\/Applications or from the disk image, adds to the application size, and the user has to navigate an extra dialog on first run.<\/p>\n<p><strong>Update:<\/strong>Klicko now uses AXMakeProcessTrusted().<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just saw Rob Keniger&#8217;s Wrapster plug-in for Coda, an interesting use of event taps; it&#8217;s a plug-in that intercepts key events for its master application. The version I looked at (1.2) uses a global tap and checks if its master application is active: \u00a0 \u00a0if(![NSApp isActive]) { \u00a0 \u00a0\u00a0 \u00a0return event; \u00a0 \u00a0} Of course, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4,19],"tags":[26,28],"class_list":["post-1477","post","type-post","status-publish","format-standard","hentry","category-dev","category-software","tag-cocoa","tag-klicko"],"featured_image_src":null,"author_info":{"display_name":"Rainer Brockerhoff","author_link":"https:\/\/brockerhoff.net\/blog\/author\/rbrockerhoff\/"},"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q3Zc-nP","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts\/1477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/comments?post=1477"}],"version-history":[{"count":0,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts\/1477\/revisions"}],"wp:attachment":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/media?parent=1477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/categories?post=1477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/tags?post=1477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}