Platforms to show: All Mac Windows Linux Cross-Platform
/MacCF/Carbon Events/Carbon Events Test
Required plugins for this example: MBS Util Plugin, MBS MacCF Plugin, MBS MacClassic Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCF/Carbon Events/Carbon Events Test
This example is the version from Tue, 21th Jan 2019.
Project "Carbon Events Test.xojo_binary_project"
Class MainWindow Inherits Window
Control List Inherits ListBox
ControlInstance List Inherits ListBox
End Control
Control EditField1 Inherits TextField
ControlInstance EditField1 Inherits TextField
End Control
Control CheckBox1 Inherits CheckBox
ControlInstance CheckBox1 Inherits CheckBox
End Control
Control CheckMouse Inherits CheckBox
ControlInstance CheckMouse Inherits CheckBox
End Control
Control CheckMonitor Inherits CheckBox
ControlInstance CheckMonitor Inherits CheckBox
End Control
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action()
quit
End EventHandler
End Control
EventHandler Sub Open()
aevents=new MyCarbonApplicationEventsMBS
wevents=new MyCarbonWindowsEventsMBS
sevents=new MyCarbonSystemEventsMBS
mevents=new MyCarbonMonitorEventsMBS
mevents.List=list
sevents.List=list
wevents.List=list
aevents.List=list
List.InsertRow 0,"Listening for events..."
aevents.Listen
wevents.Listen self
sevents.Listen
mevents.Listen
// self needed for bug in RB 2006r3
self.HasToolbarButtonMBS=true
End EventHandler
Property Protected aevents As mycarbonapplicationEventsMBS
Property mevents As mycarbonmonitorEventsMBS
Property Protected sevents As mycarbonsystemEventsMBS
Property toolbar As boolean
Property Protected wevents As mycarbonwindowsEventsMBS
End Class
MenuBar Menu
MenuItem UntitledMenu3 = ""
MenuItem UntitledMenu2 = "File"
MenuItem FileClose = "Close"
MenuItem FileQuit = "Quit"
MenuItem UntitledMenu0 = "Edit"
MenuItem EditUndo = "Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cut"
MenuItem EditCopy = "Copy"
MenuItem EditPaste = "Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class MyCarbonApplicationEventsMBS Inherits CarbonApplicationEventsMBS
EventHandler Sub ApplicationActivated()
' This even works in built applications.
write "Application was activated."
End EventHandler
EventHandler Sub ApplicationDeactivated()
write "Application was deactivated."
End EventHandler
EventHandler Function ApplicationGetDockTileMenu() As integer
#if TargetCarbon then
write "Create Dock Tile Menu"
dim m as new MenuMBS
if m.NewMenu(1000,"Dockmenu") then
m.AppendItem "Just a little test"
m.AppendItem "Item 1"
m.AppendItem "Item 2"
m.AppendItem "Item 3"
m.AppendItem "Item 4"
m.CommandID(1)=OSTypeFromStringMBS("Test") // codes must have minimum one big letter
m.CommandID(2)=OSTypeFromStringMBS("Tes1") // all Lowercase commands are Apple's
m.CommandID(3)=OSTypeFromStringMBS("Tes2")
m.CommandID(4)=OSTypeFromStringMBS("Tes3")
m.CommandID(5)=OSTypeFromStringMBS("Tes4")
m.SetIconSelector 2,"FNDR"
dockmenu=m // save for later
Return m.Handle
end if
#endif
End EventHandler
EventHandler Sub ApplicationHidden()
write "Application hidden."
End EventHandler
EventHandler Sub ApplicationLaunched(ProcessSerial as memoryblock)
dim s as String
dim p as ProcessMBS
if ProcessSerial<>nil then
p=new ProcessMBS
p.GetProcess(ProcessSerial)
s=p.Name
end if
write "Application "+s+" launched."
End EventHandler
EventHandler Sub ApplicationQuit()
write "Application quit."
End EventHandler
EventHandler Sub ApplicationShown()
write "Application shown."
End EventHandler
EventHandler Sub ApplicationSwitched(ProcessSerial as memoryblock)
' This even works in built applications.
dim s as String
dim p as ProcessMBS
if ProcessSerial<>nil then
p=new ProcessMBS
p.GetProcess(ProcessSerial)
p.Update
s=p.Name
end if
write "Application switched to "+s+"."
End EventHandler
EventHandler Sub ApplicationTerminated(ProcessSerial as memoryblock)
if ProcessSerial<>nil then
// We can't look the serial up because the app is no longer in the list.
// You can make yourself a list of running applications and update it here.
end if
write "Application terminated."
End EventHandler
EventHandler Function GestureEnded(GlobalMouseX as double, GlobalMouseY as double, WindowHandle as integer, WindowMouseX as double, WindowMouseY as double, WindowPartCode as integer, KeyModifiers as integer) As boolean
write "GestureEnded"
End EventHandler
EventHandler Function GestureMagnify(GlobalMouseX as double, GlobalMouseY as double, WindowHandle as integer, WindowMouseX as double, WindowMouseY as double, WindowPartCode as integer, KeyModifiers as integer, MagnificationAmount as double) As boolean
write "GestureMagnify "+str(MagnificationAmount)
End EventHandler
EventHandler Function GestureRotate(GlobalMouseX as double, GlobalMouseY as double, WindowHandle as integer, WindowMouseX as double, WindowMouseY as double, WindowPartCode as integer, KeyModifiers as integer, RotationAmount as double) As boolean
write "GestureRotate "+str(RotationAmount)
End EventHandler
EventHandler Function GestureStarted(GlobalMouseX as double, GlobalMouseY as double, WindowHandle as integer, WindowMouseX as double, WindowMouseY as double, WindowPartCode as integer, KeyModifiers as integer) As boolean
write "GestureStarted "+str(GlobalMouseX)+"/"+str(GlobalMouseY)+" "+hex(WindowHandle)+" "+str(WindowMouseX)+"/"+str(WindowMouseY)+" "+str(WindowPartCode)+" "+str(KeyModifiers)
End EventHandler
EventHandler Function GestureSwipe(GlobalMouseX as double, GlobalMouseY as double, WindowHandle as integer, WindowMouseX as double, WindowMouseY as double, WindowPartCode as integer, KeyModifiers as integer, SwipeDirectionX as double, SwipeDirectionY as double) As boolean
write "GestureSwipe "+str(SwipeDirectionX)+"/"+str(SwipeDirectionY)
End EventHandler
EventHandler Function KeyboardRawKeyDown(maccharcode as integer, keycode as integer, modifiers as integer, keyboardtype as integer) As boolean
write "Keyboard raw key down "+str(keycode)
Return MainWindow.CheckBox1.Value
End EventHandler
EventHandler Function KeyboardRawKeyModifiersChanged(modifierkeys as integer) As boolean
write "Keyboard key modifiers changed "+str(modifierkeys)
End EventHandler
EventHandler Function KeyboardRawKeyRepeat(maccharcode as integer, keycode as integer, modifiers as integer, keyboardtype as integer) As boolean
write "Keyboard raw key repeat "+str(keycode)
Return MainWindow.CheckBox1.Value
End EventHandler
EventHandler Function KeyboardRawKeyUp(maccharcode as integer, keycode as integer, modifiers as integer, keyboardtype as integer) As boolean
write "Keyboard raw key up "+str(keycode)
Return MainWindow.CheckBox1.Value
End EventHandler
EventHandler Function MouseWheelMoved(modifierKeys as integer, axis as integer, delta as integer) As boolean
dim d as Integer
dim a as string
const cmdKey=256
const shiftKey=512
const alphaLock=1024
const optionKey=2048
const controlKey=4096
const rightShiftKey=8192
const rightOptionKey=16384
const rightControlKey=32768
const kEventMouseWheelAxisY=1
const kEventMouseWheelAxisX=0
if axis=kEventMouseWheelAxisY then
d=delta
if BitwiseAnd(modifierKeys,optionkey)<>0 then
d=d*4 // scroll faster with option
end if
List.ScrollPosition=List.ScrollPosition-d
a="Y"
elseif axis=kEventMouseWheelAxisX then
a="X"
else
a="?"
end if
write "MouseWheelMoved "+a+" "+str(delta)
End EventHandler
EventHandler Function ServiceCopy(Scrap as CarbonEventsScrapMBS) As boolean
'Scrap.AddText "Hi, this is REALbasic!"
Scrap.Adddata "TEXT","Hello"
write "ServiceCopy"
End EventHandler
EventHandler Function ServiceGetTypes(copytypes as CFMutableArrayMBS, pastetypes as CFMutableArrayMBS) As boolean
pastetypes.Append me.CreateTypeStringWithOSType("TEXT") // Speak text
pastetypes.Append me.CreateTypeStringWithOSType("TIFF") // for Grab
copytypes.Append me.CreateTypeStringWithOSType("TEXT")
write "ServiceGettypes "+str(pastetypes.Count)
End EventHandler
EventHandler Function ServicePaste(Scrap as CarbonEventsScrapMBS) As boolean
dim s as String
dim p as Picture
dim pw as PictureWindow
dim tw as TextWindow
write "ServicePaste"
if Scrap.DataAvailable("TIFF") then // is there a picture?
s=scrap.GetData("TIFF")
if s<>"" then // yes?
p = picture.FromData(s)
if p <> Nil then
pw=new PictureWindow
pw.backdrop=p
end if
end if
end if
if scrap.TextAvailable then // maybe ASCII Text?
s = scrap.GetText
if s<>"" then
tw=new TextWindow
tw.EditField1.text=s
end if
end if
End EventHandler
EventHandler Function ServicePerform(Scrap as CarbonEventsScrapMBS, MessageName as CFStringMBS, UserData as CFStringMBS) As boolean
write "ServicePerform"
End EventHandler
EventHandler Sub VolumeMounted(VolumeRefNum as integer, VolumeRoot as FolderItem)
dim s as String
dim f as FolderItem
f=NewVolumeFolderitemMBS(VolumeRefNum)
if f<>nil then
s=f.DisplayName
end if
write "A volume was mounted: "+s
End EventHandler
EventHandler Sub VolumeUnmounted(VolumeRefNum as integer)
// If you keep a list you can identify the volume...
write "A volume was unmounted."
End EventHandler
Property list As listbox
End Class
Class MyCarbonWindowsEventsMBS Inherits CarbonWindowsEventsMBS
EventHandler Function MouseDown(x as single, y as single, modifierKeys as integer, button as integer, clickcount as integer, MouseChord as integer) As boolean
if MainWindow.CheckMouse.Value then
Write "MouseDown @ "+str(x)+" / "+str(y)
end if
End EventHandler
EventHandler Function MouseDragged(x as single, y as single, modifierKeys as integer, deltax as single, deltay as single, button as integer, clickcount as integer, MouseChord as integer) As boolean
if MainWindow.CheckMouse.Value then
Write "MouseDragged @ "+str(x)+" / "+str(y)
end if
End EventHandler
EventHandler Function MouseMoved(x as single, y as single, modifierKeys as integer, deltax as single, deltay as single) As boolean
if MainWindow.CheckMouse.Value then
Write "MouseMoved @ "+str(x)+" / "+str(y)
end if
End EventHandler
EventHandler Function MouseUp(x as single, y as single, modifierKeys as integer, button as integer, clickcount as integer, MouseChord as integer) As boolean
if MainWindow.CheckMouse.Value then
Write "MouseUp @ "+str(x)+" / "+str(y)
end if
End EventHandler
EventHandler Function MouseWheelMoved(modifierKeys as integer, axis as integer, delta as integer) As boolean
// We prefer the one from the application's events
End EventHandler
EventHandler Sub WindowBoundsChanging(original as object, previous as object, current as object, flags as integer)
dim c as IntegerRectMBS
c=integerrectmbs(current)
List.Width=c.Width+2
List.Height=c.Height-13
List.Refresh
MainWindow.UpdateNow
End EventHandler
EventHandler Function WindowClose() As boolean
' Never seen that this event is fired
write "WindowClose"
End EventHandler
EventHandler Function WindowCloseAll() As boolean
' Never seen that this event is fired
speakandwrite "WindowCloseAll"
End EventHandler
EventHandler Function WindowCollapse() As boolean
' This even works in built applications.
speakandwrite "WindowCollapse"
End EventHandler
EventHandler Function WindowCollapseAll() As boolean
' This even works in built applications.
speakandwrite "WindowCollapseAll"
End EventHandler
EventHandler Function WindowCollapsed() As boolean
' This even works in built applications.
speakandwrite "WindowCollapsed"
End EventHandler
EventHandler Function WindowCollapsing() As boolean
' This even works in built applications.
speakandwrite "WindowCollapsing"
End EventHandler
EventHandler Function WindowExpand() As boolean
' Never seen that this event is fired
speakandwrite "WindowExpand"
End EventHandler
EventHandler Function WindowExpandAll() As boolean
' Never seen that this event is fired
speakandwrite "WindowExpandAll"
End EventHandler
EventHandler Function WindowExpanded() As boolean
' This even works in built applications.
speakandwrite "WindowExpanded"
End EventHandler
EventHandler Function WindowExpanding() As boolean
' This even works in built applications.
speakandwrite "WindowExpanding"
End EventHandler
EventHandler Function WindowHidden() As boolean
' This even works in built applications.
speakandwrite "WindowHidden"
End EventHandler
EventHandler Function WindowHiding() As boolean
' This even works in built applications.
speakandwrite "WindowHiding"
End EventHandler
EventHandler Function WindowRestoreFromDock() As boolean
if rnd>0.5 then
speakandwrite "Let us block the dock"
Return true
else
speakandwrite "Let us not block the dock"
Return false
end if
End EventHandler
EventHandler Function WindowShowing() As boolean
' This even works in built applications.
speakandwrite "WindowShowing"
End EventHandler
EventHandler Function WindowShown() As boolean
' This even works in built applications.
speakandwrite "WindowShown"
End EventHandler
EventHandler Function WindowToolbarButtonClicked() As boolean
' This even works in built applications.
MainWindow.toolbar=not MainWindow.toolbar
if MainWindow.toolbar then
speakandwrite "Switching Toolbar on"
MainWindow.Title="Carbon Events - Toolbar on"
else
speakandwrite "Switching Toolbar off"
MainWindow.Title="Carbon Events - Toolbar off"
end if
End EventHandler
EventHandler Function WindowZoom() As boolean
' Never seen that this event is fired
speakandwrite "WindowZoom"
End EventHandler
EventHandler Function WindowZoomAll() As boolean
' Never seen that this event is fired
speakandwrite "WindowZoomAll"
End EventHandler
EventHandler Function WindowZoomed() As boolean
' Never seen that this event is fired
speakandwrite "WindowZoomed"
End EventHandler
Property list As listbox
End Class
Class PictureWindow Inherits Window
EventHandler Sub EnableMenuItems()
FileClose.Enable
End EventHandler
Function FileClose() As Boolean
close
Return true
End Function
End Class
Class TextWindow Inherits Window
Control EditField1 Inherits TextArea
ControlInstance EditField1 Inherits TextArea
End Control
End Class
Class App Inherits Application
EventHandler Sub Close()
Write "App.Close"
DelayMBS 0.5
End EventHandler
EventHandler Function HandleAppleEvent(theEvent As AppleEvent, eventClass As String, eventID As String) As Boolean
Write "HandleAppleEvent: "+eventClass+" "+eventid
End EventHandler
End Class
Module Util
Sub Write(s as string)
dim i,c as integer
dim w as window
dim m as MainWindow
c=WindowCount-1
for i=0 to c
w=Window(i)
if w isa MainWindow then
m=MainWindow(w)
m.List.InsertRow 0,s
end if
next
System.DebugLog s
End Sub
Sub speakandwrite(s as string)
' crashes sometimes
'dim i as Integer
'
'Declare Function MySpeakString lib "CarbonLib" Alias "SpeakString" (SpeakString as pstring) as Integer
'
'i=MySpeakString(s)
write s
End Sub
End Module
Class MyCarbonSystemEventsMBS Inherits CarbonSystemEventsMBS
EventHandler Sub TimeDateChanged()
' This even works in built applications.
List.InsertRow 0,"Time date changed."
End EventHandler
EventHandler Sub UserSessionActivated()
' This even works in built applications.
List.InsertRow 0,"User session activated."
End EventHandler
EventHandler Sub UserSessionDeactivated()
' This even works in built applications.
List.InsertRow 0,"User session deactivated."
End EventHandler
Property list As listbox
End Class
Class MyCarbonMonitorEventsMBS Inherits CarbonMonitorEventsMBS
EventHandler Function KeyboardRawKeyDown(maccharcode as integer, keycode as integer, modifiers as integer, keyboardtype as integer) As boolean
if MainWindow.CheckMonitor.Value then
List.InsertRow 0,"Monitor: Keyboard raw key down "+str(keycode)
end if
Return true
End EventHandler
EventHandler Function KeyboardRawKeyModifiersChanged(modifierkeys as integer) As boolean
if MainWindow.CheckMonitor.Value then
List.InsertRow 0,"Monitor: Keyboard key modifiers changed "+str(modifierkeys)
end if
Return true
End EventHandler
EventHandler Function KeyboardRawKeyRepeat(maccharcode as integer, keycode as integer, modifiers as integer, keyboardtype as integer) As boolean
if MainWindow.CheckMonitor.Value then
List.InsertRow 0,"Monitor: Keyboard raw key repeat "+str(keycode)
end if
Return true
End EventHandler
EventHandler Function KeyboardRawKeyUp(maccharcode as integer, keycode as integer, modifiers as integer, keyboardtype as integer) As boolean
if MainWindow.CheckMonitor.Value then
List.InsertRow 0,"Monitor: Keyboard raw key up "+str(keycode)
end if
Return true
End EventHandler
EventHandler Function MouseDown(x as single, y as single, modifierKeys as integer, button as integer, clickcount as integer, MouseChord as integer) As boolean
if MainWindow.CheckMonitor.Value then
List.InsertRow 0,"Monitor: Mouse down "+str(x)+"/"+str(y)
end if
Return true
End EventHandler
EventHandler Function MouseDragged(x as single, y as single, modifierKeys as integer, deltax as single, deltay as single, button as integer, clickcount as integer, MouseChord as integer) As boolean
if MainWindow.CheckMonitor.Value then
List.InsertRow 0,"Monitor: Mouse dragged "+str(x)+"/"+str(y)
end if
Return true
End EventHandler
EventHandler Function MouseMoved(x as single, y as single, modifierKeys as integer, deltax as single, deltay as single) As boolean
if MainWindow.CheckMonitor.Value then
List.InsertRow 0,"Monitor: Mouse moved "+str(x)+"/"+str(y)
end if
Return true
End EventHandler
EventHandler Function MouseUp(x as single, y as single, modifierKeys as integer, button as integer, clickcount as integer, MouseChord as integer) As boolean
if MainWindow.CheckMonitor.Value then
List.InsertRow 0,"Monitor: Mouse up "+str(x)+"/"+str(y)
end if
Return true
End EventHandler
EventHandler Function MouseWheelMoved(modifierKeys as integer, axis as integer, delta as integer) As boolean
if MainWindow.CheckMonitor.Value then
List.InsertRow 0,"Monitor: Mouse wheel moved "+str(delta)
end if
Return true
End EventHandler
Property list As listbox
End Class
End Project
See also:
The items on this page are in the following plugins: MBS MacCF Plugin.