underline and set accelerator key shortcut

Share your advanced PureBasic knowledge/code with the community.
User avatar
idle
Always Here
Always Here
Posts: 5891
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

underline and set accelerator key shortcut

Post by idle »

Sets a Alt + shortcut key for the Accelerator key character in a gadget title
windows.
gadget are limited at 3095 gadgets 3095 for sub gadgets 255 for items
This it so you can set a shortcut for a nested gadget in a panel gadget for instance where the item is the tab and have it activated.

Code: Select all

;Adds Shortcuts for Accelerator key following & in gadget
;from topic https://www.purebasic.fr/english/viewtopic.php?t=85453
;authours  Mesa Idle 
;- MODULE ACCELERATOR
;- ====================

;- TOP
CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf
;-
;- >> DeclareModuleAccelerator
DeclareModule Accelerator
  
  Global Pv.l
  #SPI_SETKEYBOARDCUES = $100B
  
  Declare DisplayAccelerator()
  Declare RestoreAccelerator()
 
  Declare.s SC(window, shortcut.s, gad, item = 0, subGad = 0)
  
EndDeclareModule
;- >> EndDeclareModuleAccelerator

;-

;- >> Module Accelerator
Module Accelerator
  
  CompilerIf #PB_Compiler_IsMainFile
    EnableExplicit
  CompilerEndIf
  
  ;- Private
  ; ----------
  #gadmask = $FFF00000 ;4096
  #SubGadMask = $FFF00 ;4096 
  #Itemmask = $FF      ;255 
    
  Global Dim mindex.l(64000)
  
  Procedure _MenuSetActiveGadget()
    Protected gadget, item, subgadget, event
    Event = mindex(EventMenu())
    
    gadget = (Event & #Gadmask) >> 20 
    subgadget = (Event & #SubGadMask) >> 8  
    item = (event & #Itemmask) 
    SetActiveGadget(gadget)
    If item  
      SetGadgetState(gadget,item-1)
    EndIf
    If subgadget 
      SetActiveGadget(subgadget)
    EndIf 
  EndProcedure
  
  ;-
  ;- Public
  ; ----------
  Procedure DisplayAccelerator()
    SystemParametersInfo_(#SPI_GETKEYBOARDCUES, 0, @Pv, 0)                   ;Get the current status for the running system
    SystemParametersInfo_(#SPI_SETKEYBOARDCUES, 0, 1, 0)                     ;Show underlined letters for keyboard navigation and gadget focus rectangle
                                                                             ;SystemParametersInfo_(#SPI_SETKEYBOARDCUES, 0, 0, 0) Hide underlined letters for keyboard navigation and gadget focus rectangle
  EndProcedure
  
  Procedure RestoreAccelerator()
    ; To set the system to it's default
    If Pv = 0
      SystemParametersInfo_(#SPI_SETKEYBOARDCUES, 0, 0, 0)
    EndIf
  EndProcedure
  
  Procedure.s SC(window, shortcut.s, gad,item = 0,subGad = 0)
    Protected key.s, pos  
    
    Static event  
    If event < 1000 
      event = 1000 
    EndIf   
    
    Static lwindow, Bset 
    If Bset = 0 
      lwindow = -1 
      Bset=1
    EndIf   
    
    pos = FindString(ShortCut,"&") 
    If pos 
      key=UCase(Mid(ShortCut,pos+1,1)) 
      event+1 
      
      mindex(event) = ((gad << 20) | ((subGad) << 8) | item+1)  
      
      Select key
        Case "A"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_A, event)
        Case "B"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_B, event)
        Case "C"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_C, event)
        Case "D"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_D, event)
        Case "E"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_E, event)
        Case "F"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_F, event)
        Case "G"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_G, event)
        Case "H"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_H, event)
        Case "I"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_I, event)
        Case "J"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_J, event)
        Case "K"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_K, event)
        Case "L"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_L, event)
        Case "M"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_M, event)
        Case "N"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_N, event)
        Case "O"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_O, event)
        Case "P"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_P, event)
        Case "Q"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Q, event)
        Case "R"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_R, event)
        Case "S"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_S, event)
        Case "T"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_T, event)
        Case "U"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_U, event)
        Case "V"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_V, event)
        Case "W"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_W, event)
        Case "X"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_X, event)
        Case "Y"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Y, event)
        Case "Z"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Z, event)
        Case "0"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_0, event)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad0, event)
        Case "1"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_1, event)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad1, event)
        Case "2"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_2, event)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad2, event)
        Case "3"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_3, event)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad3, event)
        Case "4"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_4, event)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad4, event)
        Case "5"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_5, event)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad5, event)
        Case "6"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_6, event)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad6, event)
        Case "7"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_7, event)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad7, event)
        Case "8"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_8, event)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad8, event)
        Case "9"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_9, event)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad9, event)
      EndSelect
      
      If window <> lwindow 
        CreateMenu(window, WindowID(window))
        lwindow = window 
      EndIf   
        
      BindMenuEvent(window, event, @_MenuSetActiveGadget())
      
    EndIf
    
    ProcedureReturn shortcut
    
  EndProcedure
EndModule
;- >> EndModule Accelerator
;-


;- TEST
;- ------

CompilerIf #PB_Compiler_IsMainFile
  
  ;- OpenWindow
  UseModule Accelerator
  DisplayAccelerator();Must be before Open any window
  
  ;BEWARE Don't create Menus before gadgets
  
  Define Ev, Quit
  
  If OpenWindow(0, 300, 200, 270, 190, "Shortcuts 1", #PB_Window_SystemMenu)
    CheckBoxGadget(0, 10, 10, 250, 25, sc(0,"&CheckBox",0))
    ButtonGadget(1, 10, 50, 250, 25, sc(0,"&Button",1))
    PanelGadget(2, 10, 90, 250, 70)
    AddGadgetItem(2, -1, sc(0,"&One",2,0))
    AddGadgetItem(2, -1, sc(0,"&Two",2,1,3))   ;<- Set PanelGadget to 2nd Tab and activate Button 3 
    ButtonGadget(3, 10, 15, 80, 24,sc(0,"Button &1",3))
    ButtonGadget(4, 95, 15, 80, 24,sc(0,"Button &2",4))
    CloseGadgetList()
    
    OpenWindow(1, 600, 200, 270, 170, "Shortcuts 2", #PB_Window_SystemMenu)
    CheckBoxGadget(5, 10, 10, 250, 25, sc(1,"&CheckBox",5))
    ButtonGadget(6, 10, 50, 250, 25, sc(1,"&Button",6))
    PanelGadget(7, 10, 90, 250, 70)
    AddGadgetItem(7, -1, sc(1,"&One",7,0))
    AddGadgetItem(7, -1, sc(1,"&Two",7,1,9))   ;<- Set PanelGadget to 2nd Tab and activate Button 9 
    ButtonGadget(8, 10, 15, 80, 24,sc(1,"Button &1",8))
    ButtonGadget(9, 95, 15, 80, 24,sc(1,"Button &2",9))
    CloseGadgetList()
    SetActiveGadget(0)
    
    
    ; Create Menu here, after gadgets
    CreateMenu(0, WindowID(0))
    MenuTitle("Project")
    MenuItem(1, "Open" + Chr(9) + "Ctrl+O"):AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_O, 1)
    MenuItem(2, "Save" + Chr(9) + "Ctrl+S"):AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_S, 2)
    MenuBar()
    MenuItem(3, "Quit" + Chr(9) + "Ctrl+Q"):AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_Q, 3)
    
    ;- Loop
    Repeat
      
      Ev = WaitWindowEvent()
      
      Select Ev
        Case #PB_Event_CloseWindow
          RestoreAccelerator()
          Quit = 1
          
        Case #PB_Event_Menu
          Select EventMenu()
            Case 1 : Debug "Open"
            Case 2 : Debug "Save"
            Case 3 : End
          EndSelect
          
      EndSelect
      
    Until Quit = 1
    
  EndIf
CompilerEndIf
;-
;- END




User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: underline and set accelerator key shortcut

Post by ChrisR »

Nice :)
I don't know if it's just me, but here, when opening windows, the Accelerator key character is not automatically underlined in the gadget title until I press ALT. It's good if I add this:

Code: Select all

Case #PB_Event_ActivateWindow
        InputData.Input
        InputData\type = #INPUT_KEYBOARD
        InputData\ki\wVk = #VK_LMENU
        InputData\ki\dwFlags = 0
        SendInput_(1, @InputData, SizeOf(INPUT))
        Delay(50)
        InputData\ki\dwFlags = #KEYEVENTF_KEYUP
        SendInput_(1, @InputData, SizeOf(INPUT))
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: underline and set accelerator key shortcut

Post by BarryG »

That's a global Windows setting, ChrisR -> "Underline access keys when available" (on Win 10).
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: underline and set accelerator key shortcut

Post by ChrisR »

Thanks BarryG, I've never used it, seems to be disabled by default on win10
I've found the option now in Control Panel > Ease of Access Center > Make the Keyboard easier to use > Underline keyboard shortcuts and access keys
Or in French:
Panneau de configuration > Options d’ergonomie > Rendre le clavier plus facile à utiliser > Souligner les raccourcis clavier et les touches d'accès rapides
Little John
Addict
Addict
Posts: 4789
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: underline and set accelerator key shortcut

Post by Little John »

That's interesting. I didn't know about that setting in the Control Panel.

For immediately showing the focus rectangle and underlined letters on Windows, I always call the following macro after creation of the gadgets.
<https://stackoverflow.com/questions/464 ... -has-focus>
<https://devblogs.microsoft.com/oldnewth ... 00/?p=4343>

Code: Select all

Macro SetUIState (_hwnd_)
   ; in: _hwnd_: WindowID
   SendMessage_(_hwnd_, #WM_UPDATEUISTATE, $30002, 0)  ; $30002 = MakeDWord(#UIS_CLEAR, #UISF_HIDEFOCUS|#UISF_HIDEACCEL)
EndMacro
User avatar
idle
Always Here
Always Here
Posts: 5891
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: underline and set accelerator key shortcut

Post by idle »

The underlines don't show up until you press alt, Isn't that how it's always been?

Should it display them by default?
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: underline and set accelerator key shortcut

Post by BarryG »

idle wrote: Tue Oct 01, 2024 9:19 amThe underlines don't show up until you press alt, Isn't that how it's always been?
Yep. It's a setting in Windows you can change, though. I always enable it after installing Windows.
Mesa
Enthusiast
Enthusiast
Posts: 433
Joined: Fri Feb 24, 2012 10:19 am

Re: underline and set accelerator key shortcut

Post by Mesa »

FYI alt+1 or alt+2 doesn't work on pb6.12x64lts+win10 x64.

Found here (win only)
viewtopic.php?t=40990
viewtopic.php?f=12&t=40676

Code: Select all

#UIS_SET        = 1
#UIS_CLEAR      = 2
#UIS_INITIALIZE = 3
#UISF_HIDEFOCUS = 1
#UISF_HIDEACCEL = 2
#UISF_ACTIVE    = 4

Procedure SetUIState(hwnd.i,LoWord.w,HiWord.w)
  SendMessage_(hwnd,#WM_UPDATEUISTATE,(LoWord & $FFFF) | (HiWord<<16),0)
EndProcedure
 
;Adds Shortcuts for Accelerator key following & in gadget 
;from topic https://www.purebasic.fr/english/viewtopic.php?t=85453

#Gadmask = $FC00  ;n 0 to 63
#Itemmask =$3E0   ;n 0 to 31
#SubGadMask = $1F ;n 0 to 31

Procedure MenuSetActiveGadget() 
  Protected gadget,item,subgadget,event 
  event = EventMenu()
  gadget = (Event & #Gadmask) >> 11 
  item = (Event & #Itemmask) >> 5 
  subgadget = (event & #SubGadMask) 
  SetActiveGadget(gadget)
  If item  
    SetGadgetState(gadget,item-1)
  EndIf
  If subgadget 
    SetActiveGadget(subgadget)
  EndIf 
EndProcedure  

Procedure.s SC(window,shortcut.s,gad,item=0,subGad=0) 
  Protected  key.s,pos,gadget  
  pos = FindString(ShortCut,"&") 
  If pos 
    key=UCase(Mid(ShortCut,pos+1,1)) 
    gadget = (gad << 11) | ((item+1) << 5) | subGad  
    
    Select key 
      Case "A"
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_A, gadget)
      Case "B"
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_B, gadget)  
      Case "C"
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_C, gadget)
      Case "D"
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_D, gadget)
      Case "E"
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_E, gadget)
      Case "F"
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_F, gadget)
      Case "G"
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_G, gadget)
      Case "H" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_H, gadget)
      Case "I" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_I, gadget)
      Case "J" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_J, gadget)
      Case "K" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_K, gadget)
      Case "L" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_L, gadget)
      Case "M" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_M, gadget)
      Case "N" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_N, gadget)
      Case "O" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_O, gadget)
      Case "P" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_P, gadget)
      Case "Q" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Q, gadget)
      Case "R" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_R, gadget)
      Case "S" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_S, gadget)
      Case "T" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_T, gadget)
      Case "U" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_U, gadget)
      Case "V" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_V, gadget)
      Case "W" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_W, gadget)
      Case "X" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_X, gadget)
      Case "Y" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Y, gadget)
      Case "Z" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Z, gadget)
      Case "0"
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_0, gadget)
      Case "1"
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_1, gadget)  
      Case "2"  
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_2, gadget)
      Case "3"  
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_3, gadget)
      Case "4"  
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_4, gadget)
      Case "5"  
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_5, gadget)
      Case "6"  
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_6, gadget)
      Case "7"  
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_7, gadget)
      Case "8"  
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_8, gadget)
      Case "9"  
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_9, gadget)
    EndSelect
    
    CreateMenu(window, WindowID(window))
    BindMenuEvent(window,gadget,@MenuSetActiveGadget()) 
    
  EndIf 
  
  ProcedureReturn shortcut
  
EndProcedure  

CompilerIf #PB_Compiler_IsMainFile 

If OpenWindow(0, 300, 200, 270, 170, "Shortcuts 1", #PB_Window_SystemMenu)
  CheckBoxGadget(0, 10, 10, 250, 25, sc(0,"&CheckBox",0))
  ButtonGadget(1, 10, 50, 250, 25, sc(0,"&Button",1))
  PanelGadget(2, 10, 90, 250, 70)
  AddGadgetItem(2, -1, sc(0,"&One",2,0))
  AddGadgetItem(2, -1, sc(0,"&Two",2,1,3))   ;<- Set PanelGadget to 2nd Tab and activate Button 3 
  ButtonGadget(3, 10, 15, 80, 24,sc(0,"Button &1",3))
  ButtonGadget(4, 95, 15, 80, 24,sc(0,"Button &2",4))
  CloseGadgetList()
  
  OpenWindow(1, 600, 200, 270, 170, "Shortcuts 2", #PB_Window_SystemMenu)
  CheckBoxGadget(5, 10, 10, 250, 25, sc(1,"&CheckBox",5))
  ButtonGadget(6, 10, 50, 250, 25, sc(1,"&Button",6))
  PanelGadget(7, 10, 90, 250, 70)
  AddGadgetItem(7, -1, sc(1,"&One",7,0))
  AddGadgetItem(7, -1, sc(1,"&Two",7,1,9))   ;<- Set PanelGadget to 2nd Tab and activate Button 9 
  ButtonGadget(8, 10, 15, 80, 24,sc(1,"Button &1",8))
  ButtonGadget(9, 95, 15, 80, 24,sc(1,"Button &2",9))
  CloseGadgetList()
    
  SetActiveGadget(0)
SetUIState(WindowID(0),#UIS_CLEAR,#UISF_HIDEFOCUS|#UISF_HIDEACCEL)
 
  Repeat
    ev = WaitWindowEvent()
    If ev = #PB_Event_CloseWindow  
      CloseWindow(EventWindow()) 
      ct+1
    EndIf 


    
  Until ct = 2
EndIf

CompilerEndIf 


Code: Select all

;Adds Shortcuts for Accelerator key following & in gadget 
;from topic https://www.purebasic.fr/english/viewtopic.php?t=85453
;{                                                                        Must be before Open any window
Global Pv.l
#SPI_SETKEYBOARDCUES = $100B

SystemParametersInfo_(#SPI_GETKEYBOARDCUES, 0, @Pv, 0)                   ;Get the current status for the running system
SystemParametersInfo_(#SPI_SETKEYBOARDCUES, 0, 1, 0)                     ;Show underlined letters for keyboard navigation and gadget focus rectangle
;SystemParametersInfo_(#SPI_SETKEYBOARDCUES, 0, 0, 0)                    ;Hide underlined letters for keyboard navigation and gadget focus rectangle
;}

#Gadmask = $FC00  ;n 0 to 63
#Itemmask =$3E0   ;n 0 to 31
#SubGadMask = $1F ;n 0 to 31

Procedure MenuSetActiveGadget() 
  Protected gadget,item,subgadget,event 
  event = EventMenu()
  gadget = (Event & #Gadmask) >> 11 
  item = (Event & #Itemmask) >> 5 
  subgadget = (event & #SubGadMask) 
  SetActiveGadget(gadget)
  If item  
    SetGadgetState(gadget,item-1)
  EndIf
  If subgadget 
    SetActiveGadget(subgadget)
  EndIf 
EndProcedure  

Procedure.s SC(window,shortcut.s,gad,item=0,subGad=0) 
  Protected  key.s,pos,gadget  
  pos = FindString(ShortCut,"&") 
  If pos 
    key=UCase(Mid(ShortCut,pos+1,1)) 
    gadget = (gad << 11) | ((item+1) << 5) | subGad  
    
    Select key 
      Case "A"
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_A, gadget)
      Case "B"
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_B, gadget)  
      Case "C"
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_C, gadget)
      Case "D"
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_D, gadget)
      Case "E"
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_E, gadget)
      Case "F"
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_F, gadget)
      Case "G"
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_G, gadget)
      Case "H" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_H, gadget)
      Case "I" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_I, gadget)
      Case "J" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_J, gadget)
      Case "K" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_K, gadget)
      Case "L" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_L, gadget)
      Case "M" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_M, gadget)
      Case "N" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_N, gadget)
      Case "O" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_O, gadget)
      Case "P" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_P, gadget)
      Case "Q" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Q, gadget)
      Case "R" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_R, gadget)
      Case "S" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_S, gadget)
      Case "T" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_T, gadget)
      Case "U" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_U, gadget)
      Case "V" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_V, gadget)
      Case "W" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_W, gadget)
      Case "X" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_X, gadget)
      Case "Y" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Y, gadget)
      Case "Z" 
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Z, gadget)
      Case "0"
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_0, gadget)
      Case "1"
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_1, gadget)  
      Case "2"  
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_2, gadget)
      Case "3"  
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_3, gadget)
      Case "4"  
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_4, gadget)
      Case "5"  
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_5, gadget)
      Case "6"  
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_6, gadget)
      Case "7"  
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_7, gadget)
      Case "8"  
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_8, gadget)
      Case "9"  
        AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_9, gadget)
    EndSelect
    
    CreateMenu(window, WindowID(window))
    BindMenuEvent(window,gadget,@MenuSetActiveGadget()) 
    
  EndIf 
  
  ProcedureReturn shortcut
  
EndProcedure  

CompilerIf #PB_Compiler_IsMainFile 

If OpenWindow(0, 300, 200, 270, 170, "Shortcuts 1", #PB_Window_SystemMenu)
  CheckBoxGadget(0, 10, 10, 250, 25, sc(0,"&CheckBox",0))
  ButtonGadget(1, 10, 50, 250, 25, sc(0,"&Button",1))
  PanelGadget(2, 10, 90, 250, 70)
  AddGadgetItem(2, -1, sc(0,"&One",2,0))
  AddGadgetItem(2, -1, sc(0,"&Two",2,1,3))   ;<- Set PanelGadget to 2nd Tab and activate Button 3 
  ButtonGadget(3, 10, 15, 80, 24,sc(0,"Button &1",3))
  ButtonGadget(4, 95, 15, 80, 24,sc(0,"Button &2",4))
  CloseGadgetList()
  
  OpenWindow(1, 600, 200, 270, 170, "Shortcuts 2", #PB_Window_SystemMenu)
  CheckBoxGadget(5, 10, 10, 250, 25, sc(1,"&CheckBox",5))
  ButtonGadget(6, 10, 50, 250, 25, sc(1,"&Button",6))
  PanelGadget(7, 10, 90, 250, 70)
  AddGadgetItem(7, -1, sc(1,"&One",7,0))
  AddGadgetItem(7, -1, sc(1,"&Two",7,1,9))   ;<- Set PanelGadget to 2nd Tab and activate Button 9 
  ButtonGadget(8, 10, 15, 80, 24,sc(1,"Button &1",8))
  ButtonGadget(9, 95, 15, 80, 24,sc(1,"Button &2",9))
  CloseGadgetList()
    
  SetActiveGadget(0)
 
  Repeat
    ev = WaitWindowEvent()
    If ev = #PB_Event_CloseWindow
        
        ;{                                                                      To set the system to it's default
        If Pv = 0
            SystemParametersInfo_(#SPI_SETKEYBOARDCUES, 0, 0, 0)
        EndIf
        ;}
       
      CloseWindow(EventWindow()) 
      ct+1
    EndIf 


    
  Until ct = 2
EndIf

CompilerEndIf 


M.
User avatar
idle
Always Here
Always Here
Posts: 5891
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: underline and set accelerator key shortcut

Post by idle »

@Mesa, I don't see any difference on win 11. :?

I could have sworn earlier the underlines and focus didn't show until I pressed alt but just now they do.
or is SystemParametersInfo effecting the current session?
I will look again in the morning later it's 1.24am I need zzzzzz.s
Mesa
Enthusiast
Enthusiast
Posts: 433
Joined: Fri Feb 24, 2012 10:19 am

Re: underline and set accelerator key shortcut

Post by Mesa »

Good zzzzzz.s :)

FYI, the 2 examples upper work on my win10x64.
And yes i think that SystemParametersInfo effects the system or the session.

M.
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: underline and set accelerator key shortcut

Post by ChrisR »

Thanks Mesa for SystemParametersInfo_(#SPI_SETKEYBOARDCUES, 0, 1, 0)
It does the trick to show underlined letters for keyboard navigation and gadget focus rectangle
I wonder why this option is not enabled by default in Windows ?
Mesa wrote: Tue Oct 01, 2024 2:00 pm Good zzzzzz.s :)
:lol:
Mesa wrote: Tue Oct 01, 2024 2:00 pm And yes i think that SystemParametersInfo effects the system or the session.
Yes, it affects the current session but not my other session Administrator

Send the ALT key on ActivateWindow event is probably the easiest way to underline the keyboard shortcuts without affecting the current session and whatever the setting in Control Panel.
Mesa
Enthusiast
Enthusiast
Posts: 433
Joined: Fri Feb 24, 2012 10:19 am

Re: underline and set accelerator key shortcut

Post by Mesa »

I was wrong, alt+1 or alt+2 work, but alt+pad1 and alt+pad2 don't work and it's ok.

I made a module:

Code: Select all

;Adds Shortcuts for Accelerator key following & in gadget
;from topic https://www.purebasic.fr/english/viewtopic.php?t=85453

;- MODULE ACCELERATOR
;- ====================

;- TOP
CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf
;-
;- >> DeclareModuleAccelerator
DeclareModule Accelerator
  
  Global Pv.l
  #SPI_SETKEYBOARDCUES = $100B
  
  Declare DisplayAccelerator()
  Declare RestoreAccelerator()
  Declare.s SC(window, shortcut.s, gad, item = 0, subGad = 0)
  
EndDeclareModule
;- >> EndDeclareModuleAccelerator

;-

;- >> Module Accelerator
Module Accelerator
  
  CompilerIf #PB_Compiler_IsMainFile
    EnableExplicit
  CompilerEndIf
  
  ;- Private
  ; ----------
  #Gadmask    = $FC00  ;n 0 to 63
  #Itemmask   = $3E0   ;n 0 to 31
  #SubGadMask = $1F    ;n 0 to 31
  
  Procedure _MenuSetActiveGadget()
    Protected gadget, item, subgadget, event
    event     = EventMenu()
    gadget    = (Event & #Gadmask) >> 11
    item      = (Event & #Itemmask) >> 5
    subgadget = (event & #SubGadMask)
    SetActiveGadget(gadget)
    If item
      SetGadgetState(gadget, item - 1)
    EndIf
    If subgadget
      SetActiveGadget(subgadget)
    EndIf
  EndProcedure
  
  ;-
  ;- Public
  ; ----------
  Procedure DisplayAccelerator()
    SystemParametersInfo_(#SPI_GETKEYBOARDCUES, 0, @Pv, 0)                   ;Get the current status for the running system
    SystemParametersInfo_(#SPI_SETKEYBOARDCUES, 0, 1, 0)                     ;Show underlined letters for keyboard navigation and gadget focus rectangle
                                                                             ;SystemParametersInfo_(#SPI_SETKEYBOARDCUES, 0, 0, 0)                    ;Hide underlined letters for keyboard navigation and gadget focus rectangle
  EndProcedure
  
  Procedure RestoreAccelerator()
    ; To set the system to it's default
    If Pv = 0
      SystemParametersInfo_(#SPI_SETKEYBOARDCUES, 0, 0, 0)
    EndIf
  EndProcedure
  
  Procedure.s SC(window, shortcut.s, gad, item = 0, subGad = 0)
    Protected key.s, pos, gadget
    pos = FindString(ShortCut, "&")
    If pos
      key    = UCase(Mid(ShortCut, pos + 1, 1))
      gadget = (gad << 11) | ((item + 1) << 5) | subGad
      
      Select key
        Case "A"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_A, gadget)
        Case "B"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_B, gadget)
        Case "C"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_C, gadget)
        Case "D"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_D, gadget)
        Case "E"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_E, gadget)
        Case "F"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_F, gadget)
        Case "G"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_G, gadget)
        Case "H"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_H, gadget)
        Case "I"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_I, gadget)
        Case "J"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_J, gadget)
        Case "K"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_K, gadget)
        Case "L"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_L, gadget)
        Case "M"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_M, gadget)
        Case "N"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_N, gadget)
        Case "O"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_O, gadget)
        Case "P"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_P, gadget)
        Case "Q"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Q, gadget)
        Case "R"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_R, gadget)
        Case "S"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_S, gadget)
        Case "T"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_T, gadget)
        Case "U"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_U, gadget)
        Case "V"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_V, gadget)
        Case "W"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_W, gadget)
        Case "X"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_X, gadget)
        Case "Y"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Y, gadget)
        Case "Z"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Z, gadget)
        Case "0"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_0, gadget)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad0, gadget)
        Case "1"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_1, gadget)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad1, gadget)
        Case "2"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_2, gadget)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad2, gadget)
        Case "3"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_3, gadget)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad3, gadget)
        Case "4"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_4, gadget)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad4, gadget)
        Case "5"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_5, gadget)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad5, gadget)
        Case "6"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_6, gadget)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad6, gadget)
        Case "7"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_7, gadget)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad7, gadget)
        Case "8"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_8, gadget)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad8, gadget)
        Case "9"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_9, gadget)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad9, gadget)
      EndSelect
      
      CreateMenu(window, WindowID(window))
      BindMenuEvent(window, gadget, @_MenuSetActiveGadget())
      
    EndIf
    
    ProcedureReturn shortcut
    
  EndProcedure
EndModule
;- >> EndModule Accelerator
;-


;- TEST
;- ------

CompilerIf #PB_Compiler_IsMainFile
  
  ;- OpenWindow
  UseModule Accelerator
  DisplayAccelerator();Must be before Open any window
  
  ;BEWARE Don't create Menus before gadgets
  
  Define Ev, Quit
  
  If OpenWindow(0, 300, 200, 270, 170 + MenuHeight(), "Shortcuts 1", #PB_Window_SystemMenu)
    CheckBoxGadget(0, 10, 10, 250, 25, sc(0, "&CheckBox", 0))
    ButtonGadget(1, 10, 50, 250, 25, sc(0, "&Button", 1))
    PanelGadget(2, 10, 90, 250, 70)
    AddGadgetItem(2, - 1, sc(0, "&One", 2, 0))
    AddGadgetItem(2, - 1, sc(0, "&Two", 2, 1, 3))   ;<- Set PanelGadget to 2nd Tab and activate Button 3
    ButtonGadget(3, 10, 15, 80, 24, sc(0, "Button &1", 3))
    ButtonGadget(4, 95, 15, 80, 24, sc(0, "Button &2", 4))
    CloseGadgetList()
    
    OpenWindow(1, 600, 200, 270, 170, "Shortcuts 2", #PB_Window_SystemMenu)
    CheckBoxGadget(5, 10, 10, 250, 25, sc(1, "&CheckBox", 5))
    ButtonGadget(6, 10, 50, 250, 25, sc(1, "&Button", 6))
    PanelGadget(7, 10, 90, 250, 70)
    AddGadgetItem(7, - 1, sc(1, "&One", 7, 0))
    AddGadgetItem(7, - 1, sc(1, "&Two", 7, 1, 9))   ;<- Set PanelGadget to 2nd Tab and activate Button 9
    ButtonGadget(8, 10, 15, 80, 24, sc(1, "Button &1", 8))
    ButtonGadget(9, 95, 15, 80, 24, sc(1, "Button &2", 9))
    CloseGadgetList()
    
    SetActiveGadget(0)
    
    
    ; Create Menu here, after gadgets
    CreateMenu(0, WindowID(0))
    MenuTitle("Project")
    MenuItem(1, "Open" + Chr(9) + "Ctrl+O"):AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_O, 1)
    MenuItem(2, "Save" + Chr(9) + "Ctrl+S"):AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_S, 2)
    MenuBar()
    MenuItem(3, "Quit" + Chr(9) + "Ctrl+Q"):AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_Q, 3)
    
    ;- Loop
    Repeat
      
      Ev = WaitWindowEvent()
      
      Select Ev
        Case #PB_Event_CloseWindow
          RestoreAccelerator()
          Quit = 1
          
        Case #PB_Event_Menu
          Select EventMenu()
            Case 1 : Debug "Open"
            Case 2 : Debug "Save"
            Case 3 : End
          EndSelect
          
      EndSelect
      
    Until Quit = 1
    
  EndIf
CompilerEndIf
;-
;- END


Mesa
User avatar
idle
Always Here
Always Here
Posts: 5891
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: underline and set accelerator key shortcut

Post by idle »

Mesa wrote: Tue Oct 01, 2024 4:16 pm I was wrong, alt+1 or alt+2 work, but alt+pad1 and alt+pad2 don't work and it's ok.

I made a module:
Mesa
Cool, there's still an oddity with the project menu when it visible the shortcuts like crtl + O .. don't work :?:
I will post it up top later
User avatar
idle
Always Here
Always Here
Posts: 5891
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: underline and set accelerator key shortcut

Post by idle »

changed it to support 3096 gadgets and 3096 sub gadgets and 255 items
accelerator keys start from 1000

Code: Select all

;Adds Shortcuts for Accelerator key following & in gadget
;from topic https://www.purebasic.fr/english/viewtopic.php?t=85453
;authours  Mesa Idle 
;- MODULE ACCELERATOR
;- ====================

;- TOP
CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf
;-
;- >> DeclareModuleAccelerator
DeclareModule Accelerator
  
  Global Pv.l
  #SPI_SETKEYBOARDCUES = $100B
  
  Declare DisplayAccelerator()
  Declare RestoreAccelerator()
 
  Declare.s SC(window, shortcut.s, gad, item = 0, subGad = 0)
  
EndDeclareModule
;- >> EndDeclareModuleAccelerator

;-

;- >> Module Accelerator
Module Accelerator
  
  CompilerIf #PB_Compiler_IsMainFile
    EnableExplicit
  CompilerEndIf
  
  ;- Private
  ; ----------
  #gadmask = $FFF00000 ;4096
  #SubGadMask = $FFF00 ;4096 
  #Itemmask = $FF      ;255 
    
  Global Dim mindex.l(64000)
  
  Procedure _MenuSetActiveGadget()
    Protected gadget, item, subgadget, event
    Event = mindex(EventMenu())
    
    gadget = (Event & #Gadmask) >> 20 
    subgadget = (Event & #SubGadMask) >> 8  
    item = (event & #Itemmask) 
    SetActiveGadget(gadget)
    If item  
      SetGadgetState(gadget,item-1)
    EndIf
    If subgadget 
      SetActiveGadget(subgadget)
    EndIf 
  EndProcedure
  
  ;-
  ;- Public
  ; ----------
  Procedure DisplayAccelerator()
    SystemParametersInfo_(#SPI_GETKEYBOARDCUES, 0, @Pv, 0)                   ;Get the current status for the running system
    SystemParametersInfo_(#SPI_SETKEYBOARDCUES, 0, 1, 0)                     ;Show underlined letters for keyboard navigation and gadget focus rectangle
                                                                             ;SystemParametersInfo_(#SPI_SETKEYBOARDCUES, 0, 0, 0) Hide underlined letters for keyboard navigation and gadget focus rectangle
  EndProcedure
  
  Procedure RestoreAccelerator()
    ; To set the system to it's default
    If Pv = 0
      SystemParametersInfo_(#SPI_SETKEYBOARDCUES, 0, 0, 0)
    EndIf
  EndProcedure
  
  Procedure.s SC(window, shortcut.s, gad,item = 0,subGad = 0)
    Protected key.s, pos  
    
    Static event  
    If event < 1000 
      event = 1000 
    EndIf   
    
    Static lwindow, Bset 
    If Bset = 0 
      lwindow = -1 
      Bset=1
    EndIf   
    
    pos = FindString(ShortCut,"&") 
    If pos 
      key=UCase(Mid(ShortCut,pos+1,1)) 
      event+1 
      
      mindex(event) = ((gad << 20) | ((subGad) << 8) | item+1)  
      
      Select key
        Case "A"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_A, event)
        Case "B"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_B, event)
        Case "C"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_C, event)
        Case "D"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_D, event)
        Case "E"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_E, event)
        Case "F"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_F, event)
        Case "G"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_G, event)
        Case "H"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_H, event)
        Case "I"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_I, event)
        Case "J"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_J, event)
        Case "K"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_K, event)
        Case "L"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_L, event)
        Case "M"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_M, event)
        Case "N"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_N, event)
        Case "O"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_O, event)
        Case "P"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_P, event)
        Case "Q"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Q, event)
        Case "R"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_R, event)
        Case "S"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_S, event)
        Case "T"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_T, event)
        Case "U"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_U, event)
        Case "V"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_V, event)
        Case "W"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_W, event)
        Case "X"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_X, event)
        Case "Y"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Y, event)
        Case "Z"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Z, event)
        Case "0"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_0, event)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad0, event)
        Case "1"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_1, event)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad1, event)
        Case "2"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_2, event)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad2, event)
        Case "3"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_3, event)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad3, event)
        Case "4"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_4, event)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad4, event)
        Case "5"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_5, event)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad5, event)
        Case "6"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_6, event)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad6, event)
        Case "7"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_7, event)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad7, event)
        Case "8"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_8, event)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad8, event)
        Case "9"
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_9, event)
          AddKeyboardShortcut(window, #PB_Shortcut_Alt | #PB_Shortcut_Pad9, event)
      EndSelect
      
      If window <> lwindow 
        CreateMenu(window, WindowID(window))
        lwindow = window 
      EndIf   
        
      BindMenuEvent(window, event, @_MenuSetActiveGadget())
      
    EndIf
    
    ProcedureReturn shortcut
    
  EndProcedure
EndModule
;- >> EndModule Accelerator
;-


;- TEST
;- ------

CompilerIf #PB_Compiler_IsMainFile
  
  ;- OpenWindow
  UseModule Accelerator
  DisplayAccelerator();Must be before Open any window
  
  ;BEWARE Don't create Menus before gadgets
  
  Define Ev, Quit
  
  If OpenWindow(0, 300, 200, 270, 190, "Shortcuts 1", #PB_Window_SystemMenu)
    CheckBoxGadget(0, 10, 10, 250, 25, sc(0,"&CheckBox",0))
    ButtonGadget(1, 10, 50, 250, 25, sc(0,"&Button",1))
    PanelGadget(2, 10, 90, 250, 70)
    AddGadgetItem(2, -1, sc(0,"&One",2,0))
    AddGadgetItem(2, -1, sc(0,"&Two",2,1,3))   ;<- Set PanelGadget to 2nd Tab and activate Button 3 
    ButtonGadget(3, 10, 15, 80, 24,sc(0,"Button &1",3))
    ButtonGadget(4, 95, 15, 80, 24,sc(0,"Button &2",4))
    CloseGadgetList()
    
    OpenWindow(1, 600, 200, 270, 170, "Shortcuts 2", #PB_Window_SystemMenu)
    CheckBoxGadget(5, 10, 10, 250, 25, sc(1,"&CheckBox",5))
    ButtonGadget(6, 10, 50, 250, 25, sc(1,"&Button",6))
    PanelGadget(7, 10, 90, 250, 70)
    AddGadgetItem(7, -1, sc(1,"&One",7,0))
    AddGadgetItem(7, -1, sc(1,"&Two",7,1,9))   ;<- Set PanelGadget to 2nd Tab and activate Button 9 
    ButtonGadget(8, 10, 15, 80, 24,sc(1,"Button &1",8))
    ButtonGadget(9, 95, 15, 80, 24,sc(1,"Button &2",9))
    CloseGadgetList()
    SetActiveGadget(0)
    
    
    ; Create Menu here, after gadgets
    CreateMenu(0, WindowID(0))
    MenuTitle("Project")
    MenuItem(1, "Open" + Chr(9) + "Ctrl+O"):AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_O, 1)
    MenuItem(2, "Save" + Chr(9) + "Ctrl+S"):AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_S, 2)
    MenuBar()
    MenuItem(3, "Quit" + Chr(9) + "Ctrl+Q"):AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_Q, 3)
    
    ;- Loop
    Repeat
      
      Ev = WaitWindowEvent()
      
      Select Ev
        Case #PB_Event_CloseWindow
          RestoreAccelerator()
          Quit = 1
          
        Case #PB_Event_Menu
          Select EventMenu()
            Case 1 : Debug "Open"
            Case 2 : Debug "Save"
            Case 3 : End
          EndSelect
          
      EndSelect
      
    Until Quit = 1
    
  EndIf
CompilerEndIf
;-
;- END




Post Reply