Shortcut description text generator CreateShortcutDesc()
Posted: Sun Oct 05, 2014 6:31 pm
I wanted to make my shortcuts user editable, so I wrote CreateShortcutDesc() to change the menu item text (description).
I included the Command key as an untested attempt to make it cross-platform.
Feel free to use it as you see fit. I am mostly posting it so the next guy who searches will be more likely to find it. My initial search did not bring up any results, but after I was done I tried new search terms and found one from Trond. His is very similar, but missing some shortcut options, such as the number keys, and his chokes on NumLock because of it--but obviously this is an easy fix.
...and a link to Trond's version:
http://www.purebasic.fr/english/viewtop ... 12&t=19474
I included the Command key as an untested attempt to make it cross-platform.
Feel free to use it as you see fit. I am mostly posting it so the next guy who searches will be more likely to find it. My initial search did not bring up any results, but after I was done I tried new search terms and found one from Trond. His is very similar, but missing some shortcut options, such as the number keys, and his chokes on NumLock because of it--but obviously this is an easy fix.
Code: Select all
#ShortcutKeySeparatorControl = "+"
#ShortcutKeySeparatorCharacter = " + "
Macro AddKey(s, Spacer)
If text <> ""
text + Spacer
EndIf
text + s
EndMacro
; CreateShortcutDesc(Shortcut.i)
; Takes an AddKeyboardShortcut() value and returns a string description.
; Returns an empty string ("") if the value is invalid.
Procedure.s CreateShortcutDesc(Shortcut.i)
Protected.s text
; Shift, Control, Alt & Command:
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
If Shortcut & #PB_Shortcut_Command
text = "Cmd"
Shortcut ! #PB_Shortcut_Command
EndIf
CompilerElse
If Shortcut & #PB_Shortcut_Control
text = "Ctrl"
Shortcut ! #PB_Shortcut_Control
EndIf
CompilerEndIf
If Shortcut & #PB_Shortcut_Alt
AddKey("Alt", #ShortcutKeySeparatorControl)
Shortcut ! #PB_Shortcut_Alt
EndIf
If Shortcut & #PB_Shortcut_Shift
AddKey("Shift", #ShortcutKeySeparatorControl)
Shortcut ! #PB_Shortcut_Shift
EndIf
; Regular keys:
Select Shortcut
Case 0
ProcedureReturn text
; Ranges:
Case #PB_Shortcut_0 To #PB_Shortcut_9
AddKey(Str(Shortcut - #PB_Shortcut_0), #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_A To #PB_Shortcut_Z
AddKey(Chr(Shortcut), #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_F1 To #PB_Shortcut_F24
AddKey("F" + Str(Shortcut - #PB_Shortcut_F1 +1), #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Pad0 To #PB_Shortcut_Pad9
AddKey("Num_" + Str(Shortcut - #PB_Shortcut_Pad0), #ShortcutKeySeparatorCharacter)
; Unique keys:
Case #PB_Shortcut_Back : AddKey("Back", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Tab : AddKey("Tab", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Clear : AddKey("Clear", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Return : AddKey("Enter", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Menu : AddKey("Menu", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Pause : AddKey("Pause", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Print : AddKey("Print", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Capital : AddKey("Shift", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Escape : AddKey("Esc", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Space : AddKey("Space", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_PageUp : AddKey("PageUp", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_PageDown : AddKey("PageDown", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_End : AddKey("End", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Home : AddKey("Home", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Left : AddKey("Left", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Up : AddKey("Up", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Right : AddKey("Right", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Down : AddKey("Down", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Select : AddKey("Select", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Execute : AddKey("Execute", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Snapshot : AddKey("Snapshot", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Insert : AddKey("Insert", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Delete : AddKey("Delete", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Help : AddKey("Help", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_LeftWindows : AddKey("LeftWindow", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_RightWindows: AddKey("RightWindow", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Apps : AddKey("Apps", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Multiply : AddKey("*", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Add : AddKey("+", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Separator : AddKey("|", #ShortcutKeySeparatorCharacter) ; verify character
Case #PB_Shortcut_Subtract : AddKey("-", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Decimal : AddKey(".", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Divide : AddKey("/", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Numlock : AddKey("NumLock", #ShortcutKeySeparatorCharacter)
Case #PB_Shortcut_Scroll : AddKey("Scroll", #ShortcutKeySeparatorCharacter)
Default
ProcedureReturn ""
EndSelect
ProcedureReturn text
EndProcedure
CompilerIf #PB_Compiler_IsMainFile
Debug CreateShortcutDesc(#PB_Shortcut_Control)
Debug CreateShortcutDesc(#PB_Shortcut_Control | #PB_Shortcut_Alt | #PB_Shortcut_F3)
Debug CreateShortcutDesc(#PB_Shortcut_Control | #PB_Shortcut_Alt | #PB_Shortcut_Shift)
Debug CreateShortcutDesc(#PB_Shortcut_Alt | #PB_Shortcut_C)
Debug CreateShortcutDesc(#PB_Shortcut_Alt | #PB_Shortcut_Shift | #PB_Shortcut_Numlock)
Debug CreateShortcutDesc(#PB_Shortcut_Add)
Debug CreateShortcutDesc(#PB_Shortcut_F2 | #PB_Shortcut_Control)
Debug CreateShortcutDesc(#PB_Shortcut_0 | #PB_Shortcut_Control)
Debug CreateShortcutDesc(#PB_Shortcut_B | #PB_Shortcut_Alt)
Debug CreateShortcutDesc(#PB_Shortcut_C | #PB_Shortcut_Shift)
Debug CreateShortcutDesc(#PB_Shortcut_D | #PB_Shortcut_Control | #PB_Shortcut_Alt)
Debug CreateShortcutDesc(#PB_Shortcut_E | #PB_Shortcut_Control | #PB_Shortcut_Shift)
Debug CreateShortcutDesc(#PB_Shortcut_F | #PB_Shortcut_Alt | #PB_Shortcut_Shift)
Debug CreateShortcutDesc(#PB_Shortcut_G | #PB_Shortcut_Control | #PB_Shortcut_Alt | #PB_Shortcut_Shift)
CompilerEndIf
http://www.purebasic.fr/english/viewtop ... 12&t=19474