[BMX] Shortkey Class For Graphic Mode

Started by Filax, April 13, 2025, 11:49:15

Previous topic - Next topic

Filax

Hey! :) Here is my GUI shortcut class autonomous. Use as you want! :)

Code: BASIC
'------------------------------------------------------------------------------
' Shortcut System for BlitzMax
'------------------------------------------------------------------------------
' Author: Creepy Cat 
' Site:https://github.com/BlackCreepyCat
' License: Public domain - use freely!
' 
' Description: 
' A lightweight keyboard shortcut system for BlitzMax applications.
' Triggers Gui_Event_ShortcutPress events when shortcuts are pressed.
' Includes a simple graphical demo showing shortcut status.

' Requirements: BlitzMax, no external dependencies.

' Usage: 
' Create shortcuts with Gui_CreateShortcut(), call Gui_RefreshShortcuts()
' in your main loop, check activations with GetShortcutEvent().
'
' Example: Run this file to see a demo with three shortcuts displayed on screen.
'------------------------------------------------------------------------------

Strict

'------------------------------------------------------------------------------
' Constants
'------------------------------------------------------------------------------
Const Gui_Event_ShortcutPress:Int = 11170  ' Event type for shortcut press

'------------------------------------------------------------------------------
' Global Storage
'------------------------------------------------------------------------------
Global Gui_Shortcuts:TList = New TList     ' List of all shortcuts
Global Gui_CurrentEvent:GuiEvent           ' Current event chain
Global Gui_PreviousEvent:Int               ' Last processed event type
Global LastShortcutId:Int                  ' ID of last triggered shortcut

'------------------------------------------------------------------------------
' Event Class
'------------------------------------------------------------------------------
Type GuiEvent
  Field EventType:Int
  Field NextEvent:GuiEvent
End Type

' Add an event to the chain
Function Gui_AddEvent(EventType:Int)
  Local ev:GuiEvent = New GuiEvent
  ev.EventType = EventType
  
  If Gui_CurrentEvent = Null Then
    Gui_CurrentEvent = ev
  Else
    Local latest:GuiEvent = Gui_CurrentEvent
    While latest.NextEvent <> Null
      latest = latest.NextEvent
    Wend
    latest.NextEvent = ev
  EndIf
End Function

' Process all events
Function Gui_RefreshEvents()
  Local ev:GuiEvent = Gui_CurrentEvent
  Gui_PreviousEvent = 0
  
  While ev <> Null
    If ev.EventType = Gui_Event_ShortcutPress Then
      Gui_PreviousEvent = Gui_Event_ShortcutPress
    EndIf
    ev = ev.NextEvent
  Wend
  
  Gui_ClearEvents()
End Function

' Clear all events
Function Gui_ClearEvents()
  Local ev:GuiEvent
  While Gui_CurrentEvent <> Null
    ev = Gui_CurrentEvent
    Gui_CurrentEvent = Gui_CurrentEvent.NextEvent
    ev = Null
  Wend
End Function

'------------------------------------------------------------------------------
' Shortcut Class
'------------------------------------------------------------------------------
Type GuiShortcut
  Field Key:Int           ' Main key (e.g., KEY_S, KEY_F1)
  Field Ctrl:Int          ' 1 if Ctrl required, 0 otherwise
  Field Alt:Int           ' 1 if Alt required, 0 otherwise
  Field Shift:Int         ' 1 if Shift required, 0 otherwise
  Field Description:String ' Description for display/debugging
  Field Id:Int            ' Unique ID for this shortcut
  
  Global NextId:Int = 1   ' Static counter for unique IDs
End Type

' Create a new shortcut with specified key combination
Function Gui_CreateShortcut:GuiShortcut(key:Int, ctrl:Int = 0, alt:Int = 0, shift:Int = 0, description:String = "")
  Local shortcut:GuiShortcut = New GuiShortcut
  shortcut.Key = key
  shortcut.Ctrl = ctrl
  shortcut.Alt = alt
  shortcut.Shift = shift
  shortcut.Description = description
  shortcut.Id = GuiShortcut.NextId
  GuiShortcut.NextId :+ 1
  
  Gui_Shortcuts.AddLast(shortcut)
  Return shortcut
End Function

' Check for shortcut activations and trigger events
Function Gui_RefreshShortcuts()
  For Local shortcut:GuiShortcut = EachIn Gui_Shortcuts
    If shortcut = Null Then Continue
    
    ' Verify modifiers
    Local ctrlDown:Int = KeyDown(KEY_LCONTROL) Or KeyDown(KEY_RCONTROL)
    Local altDown:Int = KeyDown(KEY_LALT) Or KeyDown(KEY_RALT)
    Local shiftDown:Int = KeyDown(KEY_LSHIFT) Or KeyDown(KEY_RSHIFT)
    
    If (shortcut.Ctrl = ctrlDown) And (shortcut.Alt = altDown) And (shortcut.Shift = shiftDown) Then
      If KeyHit(shortcut.Key) Then
        Gui_AddEvent(Gui_Event_ShortcutPress)
        LastShortcutId = shortcut.Id
        Gui_DebugLog("Shortcut triggered: " + shortcut.Description + " (ID: " + shortcut.Id + ")")
        FlushKeys()
      EndIf
    EndIf
  Next
End Function

' Check if a specific shortcut was triggered
Function GetShortcutEvent:Int(shortcut:GuiShortcut)
  If shortcut = Null Then Return 0
  
  If Gui_PreviousEvent = Gui_Event_ShortcutPress And LastShortcutId = shortcut.Id Then
    Return Gui_Event_ShortcutPress
  EndIf
  Return 0
End Function

' Delete a shortcut
Function Gui_DeleteShortcut(shortcut:GuiShortcut)
  If shortcut = Null Then Return
  Gui_Shortcuts.Remove(shortcut)
  shortcut = Null
End Function

'------------------------------------------------------------------------------
' Utility Functions
'------------------------------------------------------------------------------
' Log messages for debugging
Function Gui_DebugLog(msg:String)
  Print msg
End Function

'------------------------------------------------------------------------------
' Demo Setup
'------------------------------------------------------------------------------
Graphics 400, 300

' Create shortcuts
Local shortcutSave:GuiShortcut = Gui_CreateShortcut(KEY_S, 1, 0, 0, "Ctrl+S (Save)")
Local shortcutHelp:GuiShortcut = Gui_CreateShortcut(KEY_F1, 0, 0, 0, "F1 (Help)")
Local shortcutOpen:GuiShortcut = Gui_CreateShortcut(KEY_O, 1, 0, 0, "Ctrl+O (Open)")

' Action handlers
Function SaveFile()
  DrawText "Saved!", 10, 110
End Function

Function OpenHelp()
  DrawText "Help opened!", 10, 130
End Function

Function OpenFile()
  DrawText "File opened!", 10, 150
End Function

' Main loop
While Not KeyHit(KEY_ESCAPE)
  Cls
  
  ' Update shortcuts and events
  Gui_RefreshShortcuts()
  Gui_RefreshEvents()

  
  ' Handle shortcut actions
  If GetShortcutEvent(shortcutSave) = Gui_Event_ShortcutPress Then
    saveFile()
  EndIf
  If GetShortcutEvent(shortcutHelp) = Gui_Event_ShortcutPress Then
    OpenHelp()
  EndIf
  If GetShortcutEvent(shortcutOpen) = Gui_Event_ShortcutPress Then
    OpenFile()
  EndIf
  
  Flip
Wend