Show context menu for active gadget?

Windows specific forum
BarryG
Addict
Addict
Posts: 4168
Joined: Thu Apr 18, 2019 8:17 am

Show context menu for active gadget?

Post by BarryG »

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.
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Show context menu for active gadget?

Post by ChrisR »

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
BarryG
Addict
Addict
Posts: 4168
Joined: Thu Apr 18, 2019 8:17 am

Re: Show context menu for active gadget?

Post by BarryG »

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.
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Show context menu for active gadget?

Post by ChrisR »

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
BarryG
Addict
Addict
Posts: 4168
Joined: Thu Apr 18, 2019 8:17 am

Re: Show context menu for active gadget?

Post by BarryG »

Code: Select all

PostEvent(#PB_Event_RightClick, 0, GetActiveGadget())
No, 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.
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Show context menu for active gadget?

Post by ChrisR »

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.
BarryG
Addict
Addict
Posts: 4168
Joined: Thu Apr 18, 2019 8:17 am

Re: Show context menu for active gadget?

Post by BarryG »

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.
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Show context menu for active gadget?

Post by Quin »

BarryG wrote: Thu Oct 10, 2024 8:17 am

Code: Select all

PostEvent(#PB_Event_RightClick, 0, GetActiveGadget())
No, 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.
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.
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).
BarryG
Addict
Addict
Posts: 4168
Joined: Thu Apr 18, 2019 8:17 am

Re: Show context menu for active gadget?

Post by BarryG »

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. :(
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Show context menu for active gadget?

Post by Quin »

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. :(
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

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
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: Show context menu for active gadget?

Post by breeze4me »

Quin 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()?
Replace the line below

Code: Select all

PostEvent(#PB_Event_RightClick, #Window_Main, GetActiveGadget())
with this.

Code: Select all

PostEvent(#PB_Event_Gadget, #Window_Main, GetActiveGadget(), #PB_EventType_RightClick)
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Show context menu for active gadget?

Post by Quin »

breeze4me wrote: Sat Dec 07, 2024 12:27 pm
Quin 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()?
Replace the line below

Code: Select all

PostEvent(#PB_Event_RightClick, #Window_Main, GetActiveGadget())
with this.

Code: Select all

PostEvent(#PB_Event_Gadget, #Window_Main, GetActiveGadget(), #PB_EventType_RightClick)
It worked! Thank you so much! :)
Post Reply