Page 1 of 1

Subclassing & Messages

Posted: Wed Oct 09, 2013 12:15 pm
by fromVB
How do we intercept messages in OSX the way we do in Windows using callbacks? I see that SetWindowCallback is no longer supported for OSX and we must now use BindEvent. How to use BindEvent if the event we want to trap does not have similar PB event, like mouse move?

I saw some examples that looks like OSX handles messages differently than Windows with calls to Cocoa polling currentEvents?

Re: Subclassing & Messages

Posted: Wed Oct 09, 2013 6:08 pm
by wilbert
The easiest way is probably using an event tap function.
http://www.purebasic.fr/english/viewtop ... 25#p411225

Code: Select all

EnableExplicit

#MouseMovedMask = 1 << 5

ImportC ""
  CGEventTapCreate(tap, place, options, eventsOfInterest.q, callback, refcon)
EndImport

Define eventTap

ProcedureC eventTapFunction(proxy, type, event, refcon)
  SetGadgetText(0, Str(WindowMouseX(0)) + "," + Str(WindowMouseY(0)))
EndProcedure


If OpenWindow(0, 0, 0, 220, 70, "MouseMove tap", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(0, 10, 10, 200, 50, "")
  
  eventTap = CGEventTapCreate(0, 0, 1, #MouseMovedMask, @eventTapFunction(), 0)
  If eventTap
    CocoaMessage(0, CocoaMessage(0, 0, "NSRunLoop currentRunLoop"), "addPort:", eventTap, "forMode:$", @"kCFRunLoopDefaultMode")
  EndIf

  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: Subclassing & Messages

Posted: Thu Oct 10, 2013 4:25 pm
by fromVB
wilbert wrote:The easiest way is probably using an event tap function.
Thank you for the example. I just like to know how they differentiate from Windows messages. You sometimes use CGEventTapCreate and sometimes CGEventTapCreateForPSN. You also use CFRunLoopAddCommonMode and GetCurrentProcess. Do they do the same things? Is this the way to subclass in OSX? :?

Re: Subclassing & Messages

Posted: Thu Oct 10, 2013 5:25 pm
by wilbert
No, this isn't the way to subclass, it only taps events.
CGEventTapCreateForPSN only taps events from a specific process.

Subclassing is easy when using Objective-C (XCode) but complicated using PureBasic.
You can look at the Objective-C Runtime Reference to find information about some low level functions like objc_allocateClassPair that you would need to do so using PureBasic.

Re: Subclassing & Messages

Posted: Fri Oct 11, 2013 9:33 am
by fromVB
wilbert wrote:No, this isn't the way to subclass, it only taps events.
CGEventTapCreateForPSN only taps events from a specific process.

Subclassing is easy when using Objective-C (XCode) but complicated using PureBasic.
You can look at the Objective-C Runtime Reference to find information about some low level functions like objc_allocateClassPair that you would need to do so using PureBasic.
Sounds complicated. Taps sound like PB's new BindEvent. I think that it is easier to "tap" OSX messages than Windows ones, right?