underline and set accelerator key shortcut
Posted: Sun Sep 29, 2024 10:04 pm
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.
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