Page 1 of 1
How to do opt/alt menu selection?
Posted: Wed Oct 14, 2020 6:27 pm
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
Re: How to do opt/alt menu selection?
Posted: Thu Oct 15, 2020 10:37 am
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
Re: How to do opt/alt menu selection?
Posted: Thu Oct 15, 2020 11:00 am
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
Re: How to do opt/alt menu selection?
Posted: Thu Oct 15, 2020 4:53 pm
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.

Re: How to do opt/alt menu selection?
Posted: Fri Oct 16, 2020 11:30 am
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
Re: How to do opt/alt menu selection?
Posted: Fri Oct 16, 2020 5:08 pm
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
Re: How to do opt/alt menu selection?
Posted: Fri Oct 16, 2020 6:38 pm
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