How to do opt/alt menu selection?

Mac OSX specific forum
WilliamL
Addict
Addict
Posts: 1215
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

How to do opt/alt menu selection?

Post by WilliamL »

There are several good examples of intercepting the modifier keys but I want to be able to intercept a menu selection with a modifier key. I hope it isn't as simple as making a menu selection then scanning for a modifier key.

(clarify from post below)
I want the menu selection to go to one procedure and the opt+menu selection to go to another procedure (or even give a different value when it goes to the procedure)

Maybe put into the code below?

Code: Select all

If OpenWindow(0, 100, 150, 195, 260, "PureBasic - Menu")

  If CreateMenu(0, WindowID(0))
    MenuTitle("File")
        MenuItem( 1, "Load...")
        ;how about an opt+load?
      MenuItem( 2, "Save")
      MenuItem( 3, "Save As...")
  EndIf
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_Menu
          MessageRequester("Info", "MenuItem: "+Str(EventMenu()), 0)
      Case #PB_Event_CloseWindow
        End
    EndSelect
  ForEver
EndIf
Last edited by WilliamL on Thu Oct 15, 2020 11:52 pm, edited 2 times in total.
MacBook Pro-M1 (2021), Sonoma 14.3.1 (CLT 15.3), PB 6.10b7 M1
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: How to do opt/alt menu selection?

Post by Shardik »

Code: Select all

If OpenWindow(0, 100, 150, 195, 260, "PureBasic - Menu")

  If CreateMenu(0, WindowID(0))
    MenuTitle("File")
      ; How about an opt+load?
      MenuItem = MenuItem( 1, "Load" + Chr(9) + "Alt")
      CocoaMessage(0, MenuItem, "setKeyEquivalent:$", @"l")
      MenuItem( 2, "Save")
      MenuItem( 3, "Save As...")
  EndIf
 
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_Menu
        MessageRequester("Info", "MenuItem: "+Str(EventMenu()), 0)
      Case #PB_Event_CloseWindow
        End
    EndSelect
  ForEver
EndIf
Wolfram
Enthusiast
Enthusiast
Posts: 567
Joined: Thu May 30, 2013 4:39 pm

Re: How to do opt/alt menu selection?

Post by Wolfram »

Code: Select all

If OpenWindow(0, 100, 150, 195, 260, "PureBasic - Menu")

  If CreateMenu(0, WindowID(0))
    MenuTitle("File")
      ; How about an opt+load?
      MenuItem = MenuItem( 1, "Load" + #TAB$ + "Alt+l")
      MenuItem( 2, "Save")
      MenuItem( 3, "Save As...")
  EndIf
 
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_Menu
        MessageRequester("Info", "MenuItem: "+Str(EventMenu()), 0)
      Case #PB_Event_CloseWindow
        End
    EndSelect
  ForEver
EndIf
macOS Catalina 10.15.7
WilliamL
Addict
Addict
Posts: 1215
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: How to do opt/alt menu selection?

Post by WilliamL »

Thanks guys but that is not what I am looking for.

I want the menu selection to go to one procedure and the opt+menu selection to go to another procedure (or even give a different value when it goes to the procedure)

I can see how I didn't make that clear. :oops:
MacBook Pro-M1 (2021), Sonoma 14.3.1 (CLT 15.3), PB 6.10b7 M1
Wolfram
Enthusiast
Enthusiast
Posts: 567
Joined: Thu May 30, 2013 4:39 pm

Re: How to do opt/alt menu selection?

Post by Wolfram »

Something like this...?

Code: Select all

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

Global modifier

ProcedureC eventTapFunction(proxy, type, event, refcon)
  Protected flags = CGEventGetFlags_(event)
  If flags & #NSAlternateKeyMask
    SetMenuItemText(0, 1, "Load..." +#TAB$ +"Alt+l")
    
  Else
    SetMenuItemText(0, 1, "Load")
  EndIf
  
  modifier = flags
  
EndProcedure


If OpenWindow(0, 0, 0, 320, 170, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  If CreateMenu(0, WindowID(0))
    MenuTitle("File")
    ; How about an opt+load?
    MenuItem = MenuItem( 1, "Load")
    MenuItem( 2, "Save")
    MenuItem( 3, "Save As...")
  EndIf
  
  eventTap = CGEventTapCreate_(0, 0, 1, $1000, @eventTapFunction(), 0)
  If eventTap
    CocoaMessage(0, CocoaMessage(0, 0, "NSRunLoop currentRunLoop"), "addPort:", eventTap, "forMode:$", @"kCFRunLoopCommonModes")
  EndIf
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_Menu
        If modifier & #NSAlternateKeyMask
          MessageRequester("Info", "Menu with Alt")
        Else
          MessageRequester("Info", "Menu without Alt")
        EndIf
        
      Case #PB_Event_CloseWindow
        End
    EndSelect
  ForEver
  
EndIf
macOS Catalina 10.15.7
WilliamL
Addict
Addict
Posts: 1215
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: How to do opt/alt menu selection?

Post by WilliamL »

Thanks Wolfram!

That's exactly what I wanted! I see you've included the other possible masks that only have to be changed in two locations. I've added you code again below with the separate menu choices in case someone is confused that only the Load menu works. I can't say I understand how the code works but it is simple enough to use. :)

Code: Select all

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

Global modifier

ProcedureC eventTapFunction(proxy, type, event, refcon)
  Protected flags = CGEventGetFlags_(event)
  If flags & #NSAlternateKeyMask
    SetMenuItemText(0, 1, "Load..." +#TAB$ +"Alt+l")
    
  Else
    SetMenuItemText(0, 1, "Load")
  EndIf
  
  modifier = flags
  
EndProcedure


If OpenWindow(0, 0, 0, 320, 170, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  If CreateMenu(0, WindowID(0))
    MenuTitle("File")
    ; How about an opt+load?
    MenuItem = MenuItem( 1, "Load")
    MenuItem( 2, "Save")
    MenuItem( 3, "Save As...")
  EndIf
  
  eventTap = CGEventTapCreate_(0, 0, 1, $1000, @eventTapFunction(), 0)
  If eventTap
    CocoaMessage(0, CocoaMessage(0, 0, "NSRunLoop currentRunLoop"), "addPort:", eventTap, "forMode:$", @"kCFRunLoopCommonModes")
  EndIf
  
  Repeat
    Select WaitWindowEvent()
    Case #PB_Event_Menu
        Select EventMenu()
          Case 1
            If modifier & #NSAlternateKeyMask
              MessageRequester("Info", "Menu with Alt")
            Else
              MessageRequester("Info", "Menu without Alt")
            EndIf
          Case 2
            MessageRequester("Info", "save")
        EndSelect
    Case #PB_Event_CloseWindow
        End
    EndSelect
  ForEver
EndIf
MacBook Pro-M1 (2021), Sonoma 14.3.1 (CLT 15.3), PB 6.10b7 M1
Wolfram
Enthusiast
Enthusiast
Posts: 567
Joined: Thu May 30, 2013 4:39 pm

Re: How to do opt/alt menu selection?

Post by Wolfram »

I hope these let you more understand how to filter the events.
In these example you have a normal menu shortcut CMD+S for "save" and CMD+ALT+S for "save as..."

Code: Select all

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

Global modifier

ProcedureC eventTapFunction(proxy, type, event, refcon)
  Protected flags = CGEventGetFlags_(event)
  
  ;//Menu item text with ALT key
  If flags & #NSAlternateKeyMask
    SetMenuItemText(0, 1, "Load..." +#TAB$ +"Alt+CMD+l")
    SetMenuItemText(0, 2, "Save As..." +#TAB$ +"Alt+CMD+S")
    
    ;//Menu item text normal - without ALT key
  Else
    SetMenuItemText(0, 1, "Load" +#TAB$ +"CMD+l")
    SetMenuItemText(0, 2, "Save" +#TAB$ +"CMD+s")
  EndIf
  
  modifier = flags
  
EndProcedure


If OpenWindow(0, 0, 0, 320, 170, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  If CreateMenu(0, WindowID(0))
    MenuTitle("File")
    ; How about an opt+load?
    MenuItem(1, "Load" +#TAB$ +"CMD+l")
    MenuItem(2, "Save" +#TAB$ +"CMD+s")
  EndIf
  
  eventTap = CGEventTapCreate_(0, 0, 1, $1000, @eventTapFunction(), 0)
  If eventTap
    CocoaMessage(0, CocoaMessage(0, 0, "NSRunLoop currentRunLoop"), "addPort:", eventTap, "forMode:$", @"kCFRunLoopCommonModes")
  EndIf
  
  
  Repeat
    Select WaitWindowEvent()
    Case #PB_Event_Menu
      
      ;//Menu behavior with ALT key
      If modifier & #NSAlternateKeyMask
        
        Select EventMenu()
          Case 1
            MessageRequester("Info", "Menu with Alt")
          Case 2
            MessageRequester("Info", "save As...")
        EndSelect
        
        ;//Menu behavior without ALT key
      Else
        Select EventMenu()
          Case 1
            MessageRequester("Info", "Menu without Alt")
          Case 2
            MessageRequester("Info", "save")
        EndSelect
      EndIf
      
      
    Case #PB_Event_CloseWindow
        End
    EndSelect
  ForEver
EndIf
macOS Catalina 10.15.7
Post Reply