From 8079a2f9058d55c85484f04c7dc1de3d73aa2e93 Mon Sep 17 00:00:00 2001 From: Michael Sanders Date: Fri, 30 Dec 2016 22:21:03 -0800 Subject: [PATCH] Add support for CGEvent::new & CGEventGetLocation --- src/event.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/event.rs b/src/event.rs index 6f4ed71..b193426 100644 --- a/src/event.rs +++ b/src/event.rs @@ -149,6 +149,17 @@ impl TCFType for CGEvent { } impl CGEvent { + pub fn new(source: CGEventSource) -> Result { + unsafe { + let event_ref = CGEventCreate(source.as_concrete_TypeRef()); + if event_ref != ptr::null() { + Ok(TCFType::wrap_under_create_rule(event_ref)) + } else { + Err(()) + } + } + } + pub fn new_keyboard_event( source: CGEventSource, keycode: CGKeyCode, @@ -187,6 +198,12 @@ impl CGEvent { } } + pub fn location(&self) -> CGPoint { + unsafe { + CGEventGetLocation(self.as_concrete_TypeRef()) + } + } + pub fn post_to_pid(&self, pid: libc::pid_t) { unsafe { CGEventPostToPid(pid, self.as_concrete_TypeRef()); @@ -211,6 +228,10 @@ extern { /// Return the type identifier for the opaque type `CGEventRef'. fn CGEventGetTypeID() -> CFTypeID; + /// Return a new event using the event source `source'. If `source' is NULL, + /// the default source is used. + fn CGEventCreate(source: CGEventSourceRef) -> CGEventRef; + /// Return a new keyboard event. /// /// The event source may be taken from another event, or may be NULL. Based @@ -255,4 +276,8 @@ extern { /// Return the event flags of an event. fn CGEventGetFlags(event: CGEventRef) -> CGEventFlags; + + /// Return the location of an event in global display coordinates. + /// CGPointZero is returned if event is not a valid CGEventRef. + fn CGEventGetLocation(event: CGEventRef) -> CGPoint; }