How to change Menu Items depending on the ModificationKey?

Mac OSX specific forum
Wolfram
Enthusiast
Enthusiast
Posts: 567
Joined: Thu May 30, 2013 4:39 pm

How to change Menu Items depending on the ModificationKey?

Post by Wolfram »

How can I change the MenuItem text while I'm pressing a ModificationKey?

The problem is, If I open the menu the event loop is blocked till I close the menu.

Code: Select all

#NSKeyDown            = 10
#NSKeyUp              = 11
#NSFlagsChanged       = 12

#NSAlphaShiftKeyMask = 1 << 16
#NSShiftKeyMask      = 1 << 17
#NSControlKeyMask    = 1 << 18
#NSAlternateKeyMask  = 1 << 19
#NSCommandKeyMask    = 1 << 20

Global sharedApplication = CocoaMessage(0, 0, "NSApplication sharedApplication")
Define currentEvent, type, modifierFlags


If OpenWindow(0, 0, 0, 320, 170, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  If CreateMenu(1, WindowID(0))
    MenuTitle("MyMenu")
      MenuItem(1, "Item")  
  EndIf
  
  Define TxMenueText.s  

  Repeat
    
    Event = WaitWindowEvent()
    currentEvent = CocoaMessage(0, sharedApplication, "currentEvent")
    If currentEvent
      type = CocoaMessage(0, currentEvent, "type")
      If type = #NSFlagsChanged
        modifierFlags = CocoaMessage(0, currentEvent, "modifierFlags")
        If TxMenueText <> ""
          SetMenuItemText(1, 1, TxMenueText)
        EndIf
        
        If modifierFlags & #NSShiftKeyMask
          
          TxMenueText =GetMenuItemText(1, 1)
          Debug TxMenueText
          SetMenuItemText(1, 1, "Item +shift")
        EndIf
    
      EndIf
    EndIf
    
  Until Event = #PB_Event_CloseWindow
EndIf
macOS Catalina 10.15.7
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to change Menu Items depending on the ModificationKe

Post by wilbert »

Something like this ?

Code: Select all

#NSAlphaShiftKeyMask = 1 << 16
#NSShiftKeyMask      = 1 << 17
#NSControlKeyMask    = 1 << 18
#NSAlternateKeyMask  = 1 << 19
#NSCommandKeyMask    = 1 << 20

ProcedureC eventTapFunction(proxy, type, event, refcon)
  Protected flags = CGEventGetFlags_(event)
  If flags & #NSShiftKeyMask
    SetMenuItemText(1, 1, "Item +shift")
  Else
    SetMenuItemText(1, 1, "Item")
  EndIf
EndProcedure


If OpenWindow(0, 0, 0, 320, 170, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  If CreateMenu(1, WindowID(0))
    MenuTitle("MyMenu")
    MenuItem(1, "Item")  
  EndIf
  
  eventTap = CGEventTapCreate_(0, 0, 1, $1000, @eventTapFunction(), 0)
  If eventTap
    CocoaMessage(0, CocoaMessage(0, 0, "NSRunLoop currentRunLoop"), "addPort:", eventTap, "forMode:$", @"kCFRunLoopCommonModes")
  EndIf
  
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
  
EndIf
Windows (x64)
Raspberry Pi OS (Arm64)
Wolfram
Enthusiast
Enthusiast
Posts: 567
Joined: Thu May 30, 2013 4:39 pm

Re: How to change Menu Items depending on the ModificationKe

Post by Wolfram »

Yes, something like this ;-)
Just for your interest, it works on PB5.42 and not on PB 5.31
macOS Catalina 10.15.7
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to change Menu Items depending on the ModificationKe

Post by wilbert »

Wolfram wrote:Yes, something like this ;-)
Just for your interest, it works on PB5.42 and not on PB 5.31
I think you will need to import CGEventGetFlags and CGEventTapCreate on 5.31 .
On 5.42 those are available without an ImportC statement by adding an underscore.
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply