Subclassing & Messages

Mac OSX specific forum
fromVB
User
User
Posts: 82
Joined: Sun Jul 29, 2012 2:27 am

Subclassing & Messages

Post 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?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Subclassing & Messages

Post 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
Windows (x64)
Raspberry Pi OS (Arm64)
fromVB
User
User
Posts: 82
Joined: Sun Jul 29, 2012 2:27 am

Re: Subclassing & Messages

Post 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? :?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Subclassing & Messages

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
fromVB
User
User
Posts: 82
Joined: Sun Jul 29, 2012 2:27 am

Re: Subclassing & Messages

Post 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?
Post Reply