Xojo Conferences
MBSSep2018MunichDE
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

/MacCocoa/Mouse Click & Keyboard Events
Function:
Required plugins for this example: MBS MacBase Plugin, MBS MacFrameworks Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCocoa/Mouse Click & Keyboard Events
This example is the version from Sun, 8th Mar 2014.
Project "Mouse Click & Keyboard Events.rbp"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Open() call Window1.AppEventMonitor.addLocalMonitorForEventsMatchingMask(NSEventMBS.NSAnyEventMask) End EventHandler
Property myEventMonitor As MyNSEventMonitor
End Class
Class Window1 Inherits Window
Control AppEventMonitor Inherits MyNSEventMonitor
ControlInstance AppEventMonitor Inherits MyNSEventMonitor
EventHandler Sub AnyLocalEvent(e As NSEventMBS) laEventInfo.Text = e.description End EventHandler
EventHandler Sub KeyDown(Key As String, KeyCode As Integer, Repeated As Boolean, Modifier As Integer, mc As ModifierKeyClass) dim s As String if Repeated then s = " Repeated " laEvent.Text = "KeyDown: " + key + " KeyCode: " + Str( KeyCode ) + s + " " + mc.ModifierString // All Properties are read only 'ModifierKeyClass.ShiftKey As Boolean '// any shift key is pressed ' 'ModifierKeyClass.ShiftLeft As Boolean '// left shift key is pressed ' 'ModifierKeyClass.ShiftRight As Boolean '// right shift key is pressed ' ' 'ModifierKeyClass.AltKey As Boolean '// any alt key is pressed ' 'ModifierKeyClass.AltLeft As Boolean '// left alt key is pressed ' 'ModifierKeyClass.AltRight As Boolean '// right alt key is pressed ' ' 'ModifierKeyClass.CmdKey As Boolean '// any cmd key is pressed ' 'ModifierKeyClass.CmdLeft As Boolean '// left cmd key is pressed ' 'ModifierKeyClass.CmdRight As Boolean '// right cmd key is pressed ' ' 'ModifierKeyClass.CapsLockKey As Boolean '// caps lock key is down ' 'ModifierKeyClass.CtrlKey As Boolean '// ctrl key is pressed ' 'ModifierKeyClass.FnKey As Boolean '// fn key is pressed 'ModifierKeyClass.ModifierString As String '// Returns a string with all pressed modifier keys End EventHandler
EventHandler Sub KeyUp(Key As String, KeyCode As Integer, Modifier As Integer, mc As ModifierKeyClass) laEvent.Text = "KeyUp: " + key + " KeyCode: " + Str( KeyCode ) + " " + mc.ModifierString // All Properties are read only 'ModifierKeyClass.ShiftKey As Boolean '// any shift key is pressed ' 'ModifierKeyClass.ShiftLeft As Boolean '// left shift key is pressed ' 'ModifierKeyClass.ShiftRight As Boolean '// right shift key is pressed ' ' 'ModifierKeyClass.AltKey As Boolean '// any alt key is pressed ' 'ModifierKeyClass.AltLeft As Boolean '// left alt key is pressed ' 'ModifierKeyClass.AltRight As Boolean '// right alt key is pressed ' ' 'ModifierKeyClass.CmdKey As Boolean '// any cmd key is pressed ' 'ModifierKeyClass.CmdLeft As Boolean '// left cmd key is pressed ' 'ModifierKeyClass.CmdRight As Boolean '// right cmd key is pressed ' ' 'ModifierKeyClass.CapsLockKey As Boolean '// caps lock key is down ' 'ModifierKeyClass.CtrlKey As Boolean '// ctrl key is pressed ' 'ModifierKeyClass.FnKey As Boolean '// fn key is pressed 'ModifierKeyClass.ModifierString As String '// Returns a string with all pressed modifier keys End EventHandler
EventHandler Sub ModifierKeyHasChanged(e As NSEventMBS, Flags As Integer, KeyCode As Integer, mc As ModifierKeyClass) laModifierEvent.Text = "KeyCode: " + str( KeyCode) + " " +mc.ModifierString // All Properties are read only 'ModifierKeyClass.ShiftKey As Boolean '// any shift key is pressed ' 'ModifierKeyClass.ShiftLeft As Boolean '// left shift key is pressed ' 'ModifierKeyClass.ShiftRight As Boolean '// right shift key is pressed ' ' 'ModifierKeyClass.AltKey As Boolean '// any alt key is pressed ' 'ModifierKeyClass.AltLeft As Boolean '// left alt key is pressed ' 'ModifierKeyClass.AltRight As Boolean '// right alt key is pressed ' ' 'ModifierKeyClass.CmdKey As Boolean '// any cmd key is pressed ' 'ModifierKeyClass.CmdLeft As Boolean '// left cmd key is pressed ' 'ModifierKeyClass.CmdRight As Boolean '// right cmd key is pressed ' ' 'ModifierKeyClass.CapsLockKey As Boolean '// caps lock key is down ' 'ModifierKeyClass.CtrlKey As Boolean '// ctrl key is pressed ' 'ModifierKeyClass.FnKey As Boolean '// fn key is pressed 'ModifierKeyClass.ModifierString As String '// Returns a string with all pressed modifier keys End EventHandler
EventHandler Sub MouseDown(RightClick As Boolean) dim s As String If RightClick then s = "RightClick Down" else s = "LeftClick Down" end if laEvent.Text = s End EventHandler
EventHandler Sub MouseUp(RightClick As Boolean) dim s As String If RightClick then s = "RightClick Up" else s = "LeftClick Up" end if laEvent.Text = s End EventHandler
End Control
Control GroupBoxs Inherits GroupBox
ControlInstance GroupBoxs(0) Inherits GroupBox
ControlInstance GroupBoxs(1) Inherits GroupBox
ControlInstance GroupBoxs(2) Inherits GroupBox
End Control
Control laEvent Inherits Label
ControlInstance laEvent Inherits Label
End Control
Control laModifierEvent Inherits Label
ControlInstance laModifierEvent Inherits Label
End Control
Control laEventInfo Inherits Label
ControlInstance laEventInfo Inherits Label
End Control
EventHandler Function KeyDown(Key As String) As Boolean Return true // just to prevent the system beep End EventHandler
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class MyNSEventMonitor Inherits NSEventMonitorMBS
Event AnyLocalEvent(e As NSEventMBS) End Event
Event KeyDown(Key As String, KeyCode As Integer, Repeated As Boolean, Modifier As Integer, mc As ModifierKeyClass) End Event
Event KeyUp(Key As String, KeyCode As Integer, Modifier As Integer, mc As ModifierKeyClass) End Event
Event ModifierKeyHasChanged(e As NSEventMBS, Flags As Integer, KeyCode As Integer, mc As ModifierKeyClass) End Event
Event MouseDown(RightClick As Boolean) End Event
Event MouseUp(RightClick As Boolean) End Event
EventHandler Sub GlobalEvent(e as NSEventMBS) // Not used in this app End EventHandler
EventHandler Function LocalEvent(e as NSEventMBS) As NSEventMBS Dim mc As ModifierKeyClass Select Case e.type Case 1, 3 // LMouseDown, RMousedown MouseDown e.type = 3 Case 2, 4 // LMouseUp, RMouseUp MouseUp e.type = 4 Case 10 // KeyDown mc = new ModifierKeyClass( e.modifierFlags ) KeyDown e.characters, e.keyCode, e.isARepeat, e.modifierFlags, mc Case 11 // KeyUp mc = new ModifierKeyClass( e.modifierFlags ) KeyUp e.characters, e.keyCode, e.modifierFlags, mc Case 12 // FlagsChanged (Modifier) mc = new ModifierKeyClass( e.modifierFlags ) ModifierKeyHasChanged e, e.modifierFlags, e.keyCode, mc End Select // AnyLocalEvet AnyLocalEvent e Finally // Return event, because else your app can't do anything Return e End EventHandler
Note "Future Code"
Select Case e.type Case 1, 3 // LMouseDown, RMousedown MouseDown e.type = 3 Case 2, 4 // LMouseUp, RMouseUp MouseUp e.type = 4 Case 5 // MouseMoved Case 6 // MouseDragged Case 7 // RMouseDragged Case 8 // MouseEntered Case 9 // MouseExited Case 10 // KeyDown KeyDown e.characters, e.keyCode, e.modifierFlags, e.isARepeat Case 11 // KeyUp KeyUp e.characters, e.keyCode, e.modifierFlags Case 12 // FlagsChanged (Modifier) if LastModifierFlags <> e.modifierFlags then ModifierKeyHasChanged e, e.modifierFlags, e.keyCode LastModifierFlags = e.modifierFlags end if Case 13 // kitdefined Case 14 // SysDefined Case 17 // CursorUpdate Case 19 // BeginGesture Case 20 // EndGesture Case 21 // ProcessNotification Case 22 // ScrollWheel Else break End Select 'Window1.Title = str( e.type ) Finally // Return event, because else your app can't do anything Return e
Note "ModifierFlags"
000000100000000000000000 Shift Key 000000100000000000000010 Left Shift 000000100000000000000100 Right Shift 000010000000000000000000 alt Key 000010000000000000100000 Left alt 000010000000000001000000 Right alt 000100000000000000000000 cmd Key 000100000000000000001000 Left cmd 000100000000000000010000 Right cmd 100000000000000000000000 fn Key 000001000000000000000001 ctrl key 000000010000000000000000 Caps Lock
Property Private LastModifierFlags As Integer = 256
End Class
Class ModifierKeyClass
ComputedProperty AltKey As Boolean
Sub Get() Return AltLeft or AltRight // any alt key is pressed End Get
End ComputedProperty
ComputedProperty CmdKey As Boolean
Sub Get() Return CmdLeft or CmdRight // any cmd key is pressed End Get
End ComputedProperty
ComputedProperty ModifierString As String
Sub Get() dim s As String if ShiftLeft then s = s + " ShiftLeft" if ShiftRight then s = s + " ShiftRight" if AltLeft then s = s + " AltLeft" if AltRight then s = s + " AltRight" if CmdLeft then s = s + " CmdLeft" if CmdRight then s = s + " CmdRight" if CtrlKey then s = s + " CtrlKey" if FnKey then s = s + " FnKey" if CapsLockKey then s = s + " CapsLockKey" Return s End Get
End ComputedProperty
ComputedProperty ShiftKey As Boolean
Sub Get() Return ShiftLeft or ShiftRight End Get
End ComputedProperty
Const Alt_Key = &b000010000000000000000000
Const Alt_Left = &b000010000000000000100000
Const Alt_Right = &b000010000000000001000000
Const Caps_Lock = &b000000010000000000000000
Const Cmd_Key = &b000100000000000000000000
Const Cmd_Left = &b000100000000000000001000
Const Cmd_Right = &b000100000000000000010000
Const Shift_Key = &b000000100000000000000000
Const Shift_Left = &b000000100000000000000010
Const Shift_Right = &b000000100000000000000100
Const ctrl_Key = &b000001000000000000000001
Const fn_Key = &b100000000000000000000000
Sub Constructor(flags As Integer) // set Properties ShiftLeft = ((Shift_Left And flags) = Shift_Left) ShiftRight = ((Shift_Right And flags) = Shift_Right) AltLeft = ((Alt_Left And flags) = Alt_Left) AltRight = ((Alt_Right And flags) = Alt_Right) CmdLeft = ((Cmd_Left And flags) = Cmd_Left) CmdRight = ((Cmd_Right And flags) = Cmd_Right) CtrlKey = ((ctrl_Key And flags) = ctrl_Key) FnKey = ((fn_Key And flags) = fn_Key) CapsLockKey = ((Caps_Lock And flags) = Caps_Lock) End Sub
Note "Info"
All Properties are read only ModifierKeyClass.ShiftKey As Boolean // any shift key is pressed ModifierKeyClass.ShiftLeft As Boolean // left shift key is pressed ModifierKeyClass.ShiftRight As Boolean // right shift key is pressed ModifierKeyClass.AltKey As Boolean // any alt key is pressed ModifierKeyClass.AltLeft As Boolean // left alt key is pressed ModifierKeyClass.AltRight As Boolean // right alt key is pressed ModifierKeyClass.CmdKey As Boolean // any cmd key is pressed ModifierKeyClass.CmdLeft As Boolean // left cmd key is pressed ModifierKeyClass.CmdRight As Boolean // right cmd key is pressed ModifierKeyClass.CapsLockKey As Boolean // caps lock key is down ModifierKeyClass.CtrlKey As Boolean // ctrl key is pressed ModifierKeyClass.FnKey As Boolean // fn key is pressed ModifierKeyClass.ModifierString As String // Returns a string with all pressed modifier keys
Property AltLeft As Boolean
Property AltRight As Boolean
Property CapsLockKey As Boolean
Property CmdLeft As Boolean
Property CmdRight As Boolean
Property CtrlKey As Boolean
Property FnKey As Boolean
Property ShiftLeft As Boolean
Property ShiftRight As Boolean
End Class
End Project

Feedback, Comments & Corrections

The items on this page are in the following plugins: MBS MacCocoa Plugin.





Links
MBS FileMaker Plugins