Show context menu for active gadget?
Show context menu for active gadget?
Is there a way to show the context menu (when the "Apps" key is pressed) for the active gadget? I've got a blind user who wants it to open when he presses that key, but because he's not using the mouse I need it to open for the active gadget regardless of where the mouse is on the window (or even desktop). Because the mouse may be over another non-active gadget when "Apps" is pressed.
Re: Show context menu for active gadget?
You can do it by adding a keyboard shortcut for Apps:
Code: Select all
Procedure PopupMenu()
If GetActiveGadget() <> -1
If CreatePopupMenu(0)
MenuItem(1, "Gadget: " + Str(GetActiveGadget()) + " " + GetGadgetText(GetActiveGadget()))
MenuBar()
MenuItem(2, "Open")
MenuItem(3, "Quit")
DisableMenuItem(0, 1, #True)
DisplayPopupMenu(0, WindowID(0))
EndIf
EndIf
EndProcedure
If OpenWindow(0, 200, 200, 200, 150, "Apps Popup-Menu Example")
StringGadget(0, 10, 10, 180, 24, "String")
CheckBoxGadget(1, 10, 50, 180, 22, "CheckBox")
ButtonGadget(2, 10, 90, 180, 50, "Button", #PB_Button_Toggle)
SetGadgetState(2, #True) : SetActiveGadget(2)
AddKeyboardShortcut(0, #PB_Shortcut_Apps, 0)
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_RightClick
PopupMenu()
Case #PB_Event_Menu
Select EventMenu()
Case 0 : PopupMenu()
Case 2 : Debug "Menu: Open"
Case 3 : End
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
EndIf
Re: Show context menu for active gadget?
That's not the context menu; that's a custom menu. So not relevant, sorry. I want the menu that appears when you hit the Apps key, not a menu that I've created; and without hitting the Apps key to show it. Shift+F10 is the alternative to the Apps key to show it as well.
Re: Show context menu for active gadget?
Perhaps that

Code: Select all
If OpenWindow(0, 200, 200, 200, 80, "Apps Context-Menu")
StringGadget(0, 10, 10, 180, 60, "String Line 1" +#CRLF$+ "String Line 2", #ES_MULTILINE)
SetActiveGadget(0)
AddKeyboardShortcut(0, #PB_Shortcut_Apps, 0)
Repeat
Event = WaitWindowEvent()
Select Event
;Case #PB_Event_RightClick
Case #PB_Event_Menu
If EventMenu() = 0
If GetActiveGadget() <> -1
PostEvent(#PB_Event_RightClick, 0, GetActiveGadget())
EndIf
EndIf
EndSelect
Until Event = #PB_Event_CloseWindow
EndIf
Re: Show context menu for active gadget?
Code: Select all
PostEvent(#PB_Event_RightClick, 0, GetActiveGadget())
Re: Show context menu for active gadget?
BarryG, please try, retry the snippet, with the mouse outside the window or without using the mouse at all.
the Postevent is only there to simulate a RightClick when the user presses the Apps key.
the Postevent is only there to simulate a RightClick when the user presses the Apps key.
Re: Show context menu for active gadget?
Thanks, your first example actually is more like what I need. I'm trying to adapt it to my own large app but it's not working. I'll keep trying.
Re: Show context menu for active gadget?
You're misunderstanding what that code does, all it does is simulat a right click on the gadget, so it doesn't matter where the mouse is.BarryG wrote: Thu Oct 10, 2024 8:17 amNo, sorry. Can't be a right-click because the mouse might not be over the window or active gadget at the time (see my first post), or not even used at all. The user said blind people don't use a mouse.Code: Select all
PostEvent(#PB_Event_RightClick, 0, GetActiveGadget())
For whatever it's worth, you're correct in that blind people don't use mice, however it's fairly common for the mouse cursor to at least be near our keyboard focus, because a lot of screen readers automatically track this. Even if they don't, you have shortcuts to move the mouse to the current navigator object, or the navigator object to the mouse's current position, just in case there's something inaccessible that only uses mouse clicks (this happens way more often than you may think).
Re: Show context menu for active gadget?
The trouble is, that my app has to use a lot of custom right-click code because PureBasic doesn't support #PB_EventType_RightClick for StringGadgets (for one example). So it turns out that I can't use PostEvent with PB_Event_RightClick to achieve my goal. It's a massive PureBasic limitation. 

Re: Show context menu for active gadget?
Now I'm here because I'm trying to do the same thing in my app, accept the key I want to bind is shift+f10. I have it working on right click with my screen reader's mouse review commands, but not with my keyboard shortcut. Do I have to handle it some other way from BindGadgetEvent()?BarryG wrote: Thu Oct 10, 2024 11:47 am The trouble is, that my app has to use a lot of custom right-click code because PureBasic doesn't support #PB_EventType_RightClick for StringGadgets (for one example). So it turns out that I can't use PostEvent with PB_Event_RightClick to achieve my goal. It's a massive PureBasic limitation.![]()
Code: Select all
EnableExplicit
Enumeration Windows
#Window_Main
EndEnumeration
Enumeration Gadgets
#Gadget_MyList
EndEnumeration
Enumeration Menus
#Menu_MyPopup
EndEnumeration
Enumeration MenuEvents
#Menu_New
#Menu_Open
#Menu_Close
#Menu_ShiftF10
EndEnumeration
Procedure GadgetEvents()
Select EventGadget()
Case #Gadget_MyList
If EventType() = #PB_EventType_RightClick
DisplayPopupMenu(#Menu_MyPopup, WindowID(#Window_Main))
EndIf
EndSelect
EndProcedure
Procedure MenuEvents()
Select EventMenu()
Case #Menu_ShiftF10
PostEvent(#PB_Event_RightClick, #Window_Main, GetActiveGadget())
EndSelect
EndProcedure
Procedure CreateMenus()
CreatePopupMenu(#Menu_MyPopup)
MenuItem(#Menu_New, "New" + Chr($2026))
MenuItem(#Menu_Open, "Open" + Chr($2026))
MenuItem(#Menu_Close, "Close")
EndProcedure
Procedure CreateMainWindow()
Protected I
OpenWindow(#Window_Main, #PB_Ignore, #PB_Ignore, 400, 200, "Test")
CreateMenus()
ListViewGadget(#Gadget_MyList, 5, 5, 290, 100)
For I = 1 To Random(20, 4)
AddGadgetItem(#Gadget_MyList, -1, "Test")
Next
AddKeyboardShortcut(#Window_Main, #PB_Shortcut_Shift | #PB_Shortcut_F10, #Menu_ShiftF10)
BindGadgetEvent(#Gadget_MyList, @GadgetEvents())
BindEvent(#PB_Event_Menu, @MenuEvents())
SetActiveGadget(#Gadget_MyList)
EndProcedure
CreateMainWindow()
Repeat : Until WaitWindowEvent(1) = #PB_Event_CloseWindow
Re: Show context menu for active gadget?
Replace the line belowQuin wrote: Fri Dec 06, 2024 3:39 am Now I'm here because I'm trying to do the same thing in my app, accept the key I want to bind is shift+f10. I have it working on right click with my screen reader's mouse review commands, but not with my keyboard shortcut. Do I have to handle it some other way from BindGadgetEvent()?
Code: Select all
PostEvent(#PB_Event_RightClick, #Window_Main, GetActiveGadget())
Code: Select all
PostEvent(#PB_Event_Gadget, #Window_Main, GetActiveGadget(), #PB_EventType_RightClick)
Re: Show context menu for active gadget?
It worked! Thank you so much!breeze4me wrote: Sat Dec 07, 2024 12:27 pmReplace the line belowQuin wrote: Fri Dec 06, 2024 3:39 am Now I'm here because I'm trying to do the same thing in my app, accept the key I want to bind is shift+f10. I have it working on right click with my screen reader's mouse review commands, but not with my keyboard shortcut. Do I have to handle it some other way from BindGadgetEvent()?with this.Code: Select all
PostEvent(#PB_Event_RightClick, #Window_Main, GetActiveGadget())
Code: Select all
PostEvent(#PB_Event_Gadget, #Window_Main, GetActiveGadget(), #PB_EventType_RightClick)
