Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sendEvent and NSEvent

51 views
Skip to first unread message

pepijn (aka fliebel)

unread,
Sep 26, 2009, 10:16:46 AM9/26/09
to
Hey,

I'm trying to use PyObjc to control the mouse of my Mac.
I was successfully using CGPostMouseEvent with some integers and a
tuple of floats for the CGPoint.
Since 10.6 this method became deprecated and all my events make the
mouse jump to the top left corner.

I don't expect any of you to know Python, but I do hope some of you
know how I can make this work again.
Can I make CGPostMouseEvent work again with some special trickery?
Or can I use an alternative?

I did some research, but I'm no C programmer so all this stuff
confuses me to death.
I found sendEvent which sends a NSEvent, but I can't figure out how to
create a NSEvent and send it(and if this will work).

Can someone show me an Objective C example on how to do this?

And if you really want to help me out, could you do an educated guess
on how the Python code would look?
Currently I'm using this:

bndl = objc.loadBundle('CoreGraphics', globals(), '/System/Library/
Frameworks/ApplicationServices.framework')
objc.loadBundleFunctions(bndl, globals(), [('CGPostMouseEvent', 'v
{CGPoint=ff}IIIII')])

The first line loads the CoreGraphics bundle at /System/Library/
Frameworks/ApplicationServices.framework, are sendEvent and NSEvent at
the same place?
The second line defines the Python function. The syntax is v = void,
{CGPoint=ff} = a tuple of 2 floats as CGPoint, I = integer.

Thanks,
Pepijn

Reinder Verlinde

unread,
Sep 26, 2009, 1:32:22 PM9/26/09
to
In article
<eae19b69-2b2b-4556...@l35g2000vba.googlegroups.com>,

"pepijn (aka fliebel)" <pepij...@gmail.com> wrote:

> Hey,
>
> I'm trying to use PyObjc to control the mouse of my Mac.
> I was successfully using CGPostMouseEvent with some integers and a
> tuple of floats for the CGPoint.
> Since 10.6 this method became deprecated and all my events make the
> mouse jump to the top left corner.

That does not make much sense. 'Deprecated' means "works, but is not
recommended to use, as the function may go away in a future version".
Are you sure you are passing correct arguments ('it worked in 10.5' is
not proof that the arguments you pass are correct. 10.5 could be more
lenient in accepting invalid arguments)

> I don't expect any of you to know Python, but I do hope some of you
> know how I can make this work again.
> Can I make CGPostMouseEvent work again with some special trickery?
> Or can I use an alternative?

<http://developer.apple.com/mac/library/documentation/Carbon/Reference/Qu
artzEventServicesRef/DeprecationAppendix/AppendixADeprecatedAPI.html>
states:

"This function is not recommended for general use because of
undocumented special cases and undesirable side effects. The
recommended replacement for this function is
CGEventCreateMouseEvent, which allows you to create a mouse event
and customize the event before posting it to the event system."

Have you tried that?

Reinder

pepijn (aka fliebel)

unread,
Sep 27, 2009, 9:25:33 AM9/27/09
to
On Sep 26, 7:32 pm, Reinder Verlinde <rein...@verlinde.invalid> wrote:
> In article
> <eae19b69-2b2b-4556-99e3-5b67973b2...@l35g2000vba.googlegroups.com>,

Hey,

Thanks for clearing that up. I did know that deprecated means it
should still be usable, but it does not work for me, so I'm trying the
alternative.

I have not yet tried this out... Can you tell me the difference
between the NS an the CG functions(and how far I can go with mixing
them)?
Today I've been trying to make and send a NSEvent, which did send, but
did not trigger any reaction. Are there any steps or parameters
missing in this code?
http://pastebin.com/m7e6b8ec9
I can imagine that I need to set the window and context more specific,
or that I need to call run or sync on something to make it send the
events.

As long as I don't know how these NSEvents work I'll try to make and
send a CGEvent...

Pepijn

Reinder Verlinde

unread,
Sep 27, 2009, 6:11:13 PM9/27/09
to
In article
<0bbe5f4a-859f-4d8c...@j19g2000vbp.googlegroups.com>,

"pepijn (aka fliebel)" <pepij...@gmail.com> wrote:

Mac OS X is layered
(<http://developer.apple.com/mac/library/documentation/MacOSX/Conceptual/
OSX_Technology_Overview/MacOSXOverview/MacOSXOverview.html#//apple_ref/do
c/uid/TP40001067-CH205-SW2>), with higher layers building on lower ones.
The names of functions and fields in a layer may have a common prefix to
make it easier to keep in one's head what is in which layer.

Some of the lower layers are "Core Foundation" with prefix "CF" and
"Core Graphics" with prefix "CG".

Prefix "NS" is from a higher layer called "Cocoa" (the "NS" being
historical, for "Nextstep" <http://en.wikipedia.org/wiki/NeXTSTEP>)

Normally, you can mix calls as you need them, but a good program:

- prefers to make calls to higher layers, as that typically gets
the work done in fewer lines.
- prefers not to mix calls to different layers, as that keeps the
program code more at the same level of abstraction.

That does not mean that good programs never call into lower layers or
mix layers.

To learn whether you should mix NS and CG calls in your case you should
study the documentation of these APIs.

> Today I've been trying to make and send a NSEvent, which did send, but
> did not trigger any reaction.

- I do not see a call to 'CGEventCreateMouseEvent' there.
- I do not see the expected call to 'CGEventPost' either
(both may be because Python does things differently)
- How do you know it did sent anything?
- I just tested this from Objective-C. Both CGPostMouseEvent and
CGEventCreateMouseEvent/CGEventPost appear to work on my 10.6.1
system (I generated a click at (40,20) which opened the Apple menu)

Follow-ups set to comp.sys.mac.programmer.help, as, to answer your
original question, that is a more appropriate newsgroup
(comp.lang.objective-c is about the programming language, not about the
API of the platform it currently is most popular on)

Reinder

0 new messages