Page 1 of 1

RemoveKeyboardShortcut(#PB_Shortcut_Revert)

Posted: Tue Jan 06, 2015 12:34 am
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.

Re: RemoveKeyboardShortcut(#PB_Shortcut_Revert)

Posted: Tue Jan 06, 2015 10:35 pm
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).

Re: RemoveKeyboardShortcut(#PB_Shortcut_Revert)

Posted: Fri Jan 09, 2015 2:21 am
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

;-

Re: RemoveKeyboardShortcut(#PB_Shortcut_Revert)

Posted: Sat Jan 10, 2015 4:06 am
by ozzie
Thanks, kenmo. I'll give that a try next week.