RemoveKeyboardShortcut(#PB_Shortcut_Revert)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
ozzie
Enthusiast
Enthusiast
Posts: 397
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

RemoveKeyboardShortcut(#PB_Shortcut_Revert)

Post by ozzie »

As discussed and suggested in my topic RemoveKeyboardShortcut with #PB_Shortcut_All kills tab, could we have an option in RemoveKeyboardShortcut() to remove only those shortcuts added in the program by AddKeyboardShortcut()? The suggestion in that topic was a new constant: #PB_Shortcut_Revert.

The reason: I provide users with the ability to select their own shortcuts for many program functions, just as the PB IDE does under Preferences. If the user changes some shortcuts and then clicks OK to apply the changes, I want to remove all the existing shortcuts and then add the currently-specified shortcuts. Since RemoveKeyboardShortcut(#Window, #PB_Shortcut_All) also removes the defaults of #PB_Shortcut_Tab and #PB_Shortcut_Tab|#PB_Shortcut_Shift, I currently preserve the old program-specified shortcut settings and loop through those to remove them.

Since this issue could apply to anyone wanting to provide user-specified shortcuts, it would be much easier if we could have something similar to #PB_Shortcut_All but which only removes our own shortcuts.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: RemoveKeyboardShortcut(#PB_Shortcut_Revert)

Post by Rescator »

It might "look" better if it's called RemoveKeyboardShortcut(#PB_Default) instead of revert.


Alternatively allow #PB_Default to be used with AddKeyboardShortcut() provided the constant do not clash with any existing keyboard shortcuts that is (I haven't checked).
User avatar
kenmo
Addict
Addict
Posts: 1932
Joined: Tue Dec 23, 2003 3:54 am

Re: RemoveKeyboardShortcut(#PB_Shortcut_Revert)

Post by kenmo »

#PB_Default wouldn't work because it has the same value as #PB_Shortcut_All (-1) :)

In the meantime, here is a workaround.
Just copy these Macros + Procedure into your code, and NO OTHER CHANGES are required!

Code: Select all

;-
;- *** START ***

CompilerIf (#True)
  
  Macro _AddKeyboardShortcut(Action, Window, Key, Event)
    Action#KeyboardShortcut((Window), (Key), (Event))
  EndMacro
  
  Macro _RemoveKeyboardShortcut(Action, Window, Key)
    Action#KeyboardShortcut((Window), (Key))
  EndMacro
  
  Procedure _KeyboardShortcutProc(Remove.i, Window.i, Key.i, Event.i)
    Static NewList _KeyboardShortcuts.i()
    If (Remove)
      ResetList(_KeyboardShortcuts())
      While (NextElement(_KeyboardShortcuts()))
        If (_KeyboardShortcuts() = Window)
          NextElement(_KeyboardShortcuts())
          If ((Key = #PB_Shortcut_All) Or (_KeyboardShortcuts() = Key))
            _RemoveKeyboardShortcut(Remove, Window, _KeyboardShortcuts())
            DeleteElement(_KeyboardShortcuts())
            DeleteElement(_KeyboardShortcuts())
          EndIf
        Else
          NextElement(_KeyboardShortcuts())
        EndIf
      Wend
    Else
      LastElement(_KeyboardShortcuts())
      AddElement(_KeyboardShortcuts()) : _KeyboardShortcuts() = Window
      AddElement(_KeyboardShortcuts()) : _KeyboardShortcuts() = Key
      _AddKeyboardShortcut(Add, Window, Key, Event)
    EndIf
  EndProcedure
  
  Macro AddKeyboardShortcut(Window, Key, Event)
    _KeyboardShortcutProc(#False, (Window), (Key), (Event))
  EndMacro
  
  Macro RemoveKeyboardShortcut(Window, Key)
    _KeyboardShortcutProc(#True, (Window), (Key), #Null)
  EndMacro

CompilerEndIf

;- *** END ***
;-





;-
;- DEMO

CompilerIf (#PB_Compiler_IsMainFile)
  
  OpenWindow(0, 0, 0, 240, 240, "Keyboard Shortcuts", #PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)
  
  StringGadget(0, 10, 10, 220, 20, "Try pressing Tab, Shift+Tab,")
  StringGadget(1, 10, 40, 220, 20, "Ctrl + B,")
  StringGadget(2, 10, 70, 220, 20, "F5,")
  StringGadget(3, 10, 100, 220, 20, "and Escape.")
  SetActiveGadget(0)
  
  AddKeyboardShortcut(0, #PB_Shortcut_B|#PB_Shortcut_Command, 0)
  AddKeyboardShortcut(0, #PB_Shortcut_F5, 1)
  AddKeyboardShortcut(0, #PB_Shortcut_Escape, 2)
  
  ButtonGadget(4, 10, 130, 220, 25, "Remove All Shortcuts")
  
  Repeat
    Event = WaitWindowEvent()
    If (Event = #PB_Event_Menu)
      Select (EventMenu())
        Case 0 : MessageRequester("", "You pressed Ctrl + B!")
        Case 1 : MessageRequester("", "You pressed F5!")
        Case 2 : MessageRequester("", "You pressed Escape!")
      EndSelect
    ElseIf (Event = #PB_Event_Gadget) And (EventGadget() = 4)
      RemoveKeyboardShortcut(0, #PB_Shortcut_All)
      MessageRequester("", "Shortcuts removed!")
    EndIf
  Until Event = #PB_Event_CloseWindow

CompilerEndIf

;-
ozzie
Enthusiast
Enthusiast
Posts: 397
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

Re: RemoveKeyboardShortcut(#PB_Shortcut_Revert)

Post by ozzie »

Thanks, kenmo. I'll give that a try next week.
Post Reply