Hier ein kleines Modul Include mit dem man KeyboardShortcuts zu Gadgets hinzufügen kann.
Es übernimmt einfach das leidige Add... und Remove...
Vielleicht kann es jemand gebrauchen
Declare Sektion
Code: Alles auswählen
;============================================================
;= GadgetShortcuts:: Adds KeyboardShortcuts to Gadgets
;= Version: 0.1a
;= Author: Derren (german and english purebasic forum)
;= PB Version: 5.31 LTS x86 (written in)
;= Infothread: http://purebasic.fr/german/viewtopic.php?f=8&t=29410
;============================================================
DeclareModule GadgetShortcuts
Declare Add(Window, Gadget, Key, Event=#PB_Any) ;Returns Event, in case of #PB_Any or EMPTY
Declare Remove(Window, Gadget, Key)
Declare Do(EventWindow, EventGadget, EventType)
EndDeclareModule
;Usage:
;
; Repeat
; Event = WaitWindowEvent()
; Select Event
; Case #PB_Event_Gadget
; GadgetShortcuts::Do(EventWindow(), EventGadget(), EventType())
;
; Select EventGadget()
; Case #myGadget ;...
Code: Alles auswählen
Module GadgetShortcuts
Structure shortcutEvent
Shortcut.i
Event.i
EndStructure
Structure MapStructure
List events.shortcutEvent()
EndStructure
NewMap shortcuts.MapStructure()
Procedure Add(Window, Gadget, Key, Event=#PB_Any)
Shared shortcuts()
Static any_Event.i
If Event = #PB_Any
any_Event = any_Event + 1
Event = any_Event
EndIf
AddElement(shortcuts(Str(Window)+"|"+Str(Gadget))\events())
shortcuts(Str(Window)+"|"+Str(Gadget))\events()\Event = Event
shortcuts(Str(Window)+"|"+Str(Gadget))\events()\Shortcut = Key
ProcedureReturn Event
EndProcedure
Procedure Remove(Window, Gadget, Key)
Shared shortcuts()
ForEach shortcuts(Str(Window)+"|"+Str(Gadget))\events()
If shortcuts(Str(Window)+"|"+Str(Gadget))\events()\Shortcut = Key
DeleteElement(shortcuts(Str(Window)+"|"+Str(Gadget))\events(), #True)
EndIf
Next
EndProcedure
Procedure Do(EventWindow, EventGadget, EventType)
Shared shortcuts()
Select EventType
Case #PB_EventType_Focus
ForEach shortcuts(Str(EventWindow)+"|"+Str(EventGadget))\events()
AddKeyboardShortcut(EventWindow, shortcuts(Str(EventWindow)+"|"+Str(EventGadget))\events()\Shortcut, shortcuts(Str(EventWindow)+"|"+Str(EventGadget))\events()\Event)
Next
Case #PB_EventType_LostFocus
ForEach shortcuts(Str(EventWindow)+"|"+Str(EventGadget))\events()
RemoveKeyboardShortcut(EventWindow, shortcuts(Str(EventWindow)+"|"+Str(EventGadget))\events()\Shortcut)
Next
EndSelect
EndProcedure
EndModule
Code: Alles auswählen
If OpenWindow(0, 100, 200, 195, 260, "PureBasic Window", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget)
n = 0
StringGadget(0, 5, 5, 140, 20, Str(n) + " x Strg+P gedrückt")
StringGadget(1, 5, 130, 150, 20, "String2: Strg+T -> MsgBox")
GadgetShortcuts::Add(0, 0, #PB_Shortcut_Control|#PB_Shortcut_P, 15)
myEvent = GadgetShortcuts::Add(0, 1, #PB_Shortcut_Control|#PB_Shortcut_T)
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
GadgetShortcuts::Do(EventWindow(), EventGadget(), EventType())
Case #PB_Event_Menu
Select EventMenu()
Case 15
n=n+1
SetGadgetText(0, Str(n) + " x Strg+P gedrückt")
Case myEvent
MessageRequester("MsgBox", "Strg+T mit Fokus auf String 2 gedrückt")
EndSelect
Case #PB_Event_CloseWindow
Quit = 1
EndSelect
Until Quit = 1
EndIf
End