It is currently Sun Oct 25, 2020 3:05 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: How to do opt/alt menu selection?
PostPosted: Wed Oct 14, 2020 6:27 pm 
Offline
Addict
Addict

Joined: Mon Aug 04, 2008 10:56 pm
Posts: 1135
Location: Seattle, USA
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:
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

_________________
MacBook Pro-r, OSX 10.14.6 , PB 5.70LTS


Last edited by WilliamL on Thu Oct 15, 2020 11:52 pm, edited 2 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: How to do opt/alt menu selection?
PostPosted: Thu Oct 15, 2020 10:37 am 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 1770
Location: Germany
Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: How to do opt/alt menu selection?
PostPosted: Thu Oct 15, 2020 11:00 am 
Offline
Enthusiast
Enthusiast

Joined: Thu May 30, 2013 4:39 pm
Posts: 523
Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: How to do opt/alt menu selection?
PostPosted: Thu Oct 15, 2020 4:53 pm 
Offline
Addict
Addict

Joined: Mon Aug 04, 2008 10:56 pm
Posts: 1135
Location: Seattle, USA
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-r, OSX 10.14.6 , PB 5.70LTS


Top
 Profile  
Reply with quote  
 Post subject: Re: How to do opt/alt menu selection?
PostPosted: Fri Oct 16, 2020 11:30 am 
Offline
Enthusiast
Enthusiast

Joined: Thu May 30, 2013 4:39 pm
Posts: 523
Something like this...?
Code:
#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


Top
 Profile  
Reply with quote  
 Post subject: Re: How to do opt/alt menu selection?
PostPosted: Fri Oct 16, 2020 5:08 pm 
Offline
Addict
Addict

Joined: Mon Aug 04, 2008 10:56 pm
Posts: 1135
Location: Seattle, USA
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:
#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-r, OSX 10.14.6 , PB 5.70LTS


Top
 Profile  
Reply with quote  
 Post subject: Re: How to do opt/alt menu selection?
PostPosted: Fri Oct 16, 2020 6:38 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu May 30, 2013 4:39 pm
Posts: 523
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:
#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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye