' Keyboard shortcuts with ACCEL ATTACH via Win32 DECLARE
#COMPILE EXE
#DIM ALL

FUNCTION LookupShortcut(BYVAL mods AS STRING, BYVAL key AS STRING) AS LONG
    LOCAL mk AS STRING
    LOCAL k AS STRING
    mk = mods + "+" + key
    IF mk = "Ctrl+N" THEN FUNCTION = 100
    IF mk = "Ctrl+O" THEN FUNCTION = 101
    IF mk = "Ctrl+S" THEN FUNCTION = 102
    IF mk = "Ctrl+P" THEN FUNCTION = 103
    IF mk = "Ctrl+F" THEN FUNCTION = 104
    IF mk = "F1" THEN FUNCTION = 105
    IF mk = "Ctrl+Z" THEN FUNCTION = 106
    IF mk = "Ctrl+Y" THEN FUNCTION = 107
    IF mk = "Ctrl+C" THEN FUNCTION = 108
    IF mk = "Ctrl+V" THEN FUNCTION = 109
    IF mk = "Ctrl+X" THEN FUNCTION = 110
    IF mk = "Alt+F4" THEN FUNCTION = 111
    FUNCTION = 0
END FUNCTION

SUB PrintShortcut(BYVAL key AS STRING, BYVAL mods AS STRING, BYVAL desc AS STRING)
    PRINT "  "
    IF LEN(mods) > 0 THEN PRINT mods; "+";
    PRINT key; " -> "; desc
END SUB

SUB InitShortcuts()
    PRINT "Keyboard shortcuts (12):"
    PrintShortcut("N", "Ctrl", "New file")
    PrintShortcut("O", "Ctrl", "Open file")
    PrintShortcut("S", "Ctrl", "Save file")
    PrintShortcut("P", "Ctrl", "Print")
    PrintShortcut("F", "Ctrl", "Find")
    PrintShortcut("F1", "", "Help")
    PrintShortcut("Z", "Ctrl", "Undo")
    PrintShortcut("Y", "Ctrl", "Redo")
    PrintShortcut("C", "Ctrl", "Copy")
    PrintShortcut("V", "Ctrl", "Paste")
    PrintShortcut("X", "Ctrl", "Cut")
    PrintShortcut("F4", "Alt", "Exit")
END SUB

SUB ProcessKey(BYVAL mods AS STRING, BYVAL key AS STRING)
    LOCAL id AS LONG
    id = LookupShortcut(mods, key)
    IF id = 100 THEN PRINT "  New file created"
    IF id = 101 THEN PRINT "  Open file dialog"
    IF id = 102 THEN PRINT "  File saved"
    IF id = 106 THEN PRINT "  Undo"
    IF id = 108 THEN PRINT "  Copied to clipboard"
    IF id = 109 THEN PRINT "  Pasted from clipboard"
    IF id = 111 THEN PRINT "  Exit application"
    IF id = 0 THEN PRINT "  No shortcut for "; mods; "+"; key
END SUB

SUB KeyPressSimulation()
    PRINT ""
    ProcessKey("Ctrl", "S")
    ProcessKey("Ctrl", "Z")
    ProcessKey("Alt", "F4")
    ProcessKey("Ctrl", "Q")
    ProcessKey("Ctrl", "C")
    ProcessKey("Ctrl", "V")
END SUB

SUB ModifierKeyDemo()
    PRINT ""
    PRINT "Modifier combinations:"
    DIM mods(5) AS STRING
    mods(0) = "Ctrl"
    mods(1) = "Alt"
    mods(2) = "Shift"
    mods(3) = "Ctrl+Shift"
    mods(4) = "Ctrl+Alt"
    mods(5) = "Ctrl+Shift+Alt"
    LOCAL i AS LONG
    FOR i = 0 TO 5
        PRINT "  "; mods(i); " + X"
    NEXT i
END SUB

FUNCTION PBMAIN() AS LONG
    PRINT "=== Shortcut Keys Demo ==="
    PRINT "Win32: CreateAcceleratorTable via DECLARE"
    InitShortcuts()
    KeyPressSimulation()
    ModifierKeyDemo()
    PRINT "=== End Shortcut Keys ==="
    FUNCTION = 0
END FUNCTION
