AddKeyboardShortcut()
Syntax
AddKeyboardShortcut(#Window, Shortcut, Event)Description
Add or replace a keyboard shortcut to the specified window. A shortcut generates a menu event (like a menu item) as most of them are used in conjunction with menus.
Parameters
#Window The window to use. Shortcut It can be one of the following constants: #PB_Shortcut_Back #PB_Shortcut_Tab #PB_Shortcut_Clear #PB_Shortcut_Return #PB_Shortcut_Menu #PB_Shortcut_Pause #PB_Shortcut_Print #PB_Shortcut_Capital #PB_Shortcut_Escape #PB_Shortcut_Space #PB_Shortcut_PageUp #PB_Shortcut_PageDown #PB_Shortcut_End #PB_Shortcut_Home #PB_Shortcut_Left #PB_Shortcut_Up #PB_Shortcut_Right #PB_Shortcut_Down #PB_Shortcut_Select #PB_Shortcut_Execute #PB_Shortcut_Snapshot #PB_Shortcut_Insert #PB_Shortcut_Delete #PB_Shortcut_Help #PB_Shortcut_0 #PB_Shortcut_1 #PB_Shortcut_2 #PB_Shortcut_3 #PB_Shortcut_4 #PB_Shortcut_5 #PB_Shortcut_6 #PB_Shortcut_7 #PB_Shortcut_8 #PB_Shortcut_9 #PB_Shortcut_A #PB_Shortcut_B #PB_Shortcut_C #PB_Shortcut_D #PB_Shortcut_E #PB_Shortcut_F #PB_Shortcut_G #PB_Shortcut_H #PB_Shortcut_I #PB_Shortcut_J #PB_Shortcut_K #PB_Shortcut_L #PB_Shortcut_M #PB_Shortcut_N #PB_Shortcut_O #PB_Shortcut_P #PB_Shortcut_Q #PB_Shortcut_R #PB_Shortcut_S #PB_Shortcut_T #PB_Shortcut_U #PB_Shortcut_V #PB_Shortcut_W #PB_Shortcut_X #PB_Shortcut_Y #PB_Shortcut_Z #PB_Shortcut_LeftWindows #PB_Shortcut_RightWindows #PB_Shortcut_Apps #PB_Shortcut_Pad0 #PB_Shortcut_Pad1 #PB_Shortcut_Pad2 #PB_Shortcut_Pad3 #PB_Shortcut_Pad4 #PB_Shortcut_Pad5 #PB_Shortcut_Pad6 #PB_Shortcut_Pad7 #PB_Shortcut_Pad8 #PB_Shortcut_Pad9 #PB_Shortcut_Multiply #PB_Shortcut_Add #PB_Shortcut_Separator #PB_Shortcut_Subtract #PB_Shortcut_Decimal #PB_Shortcut_Divide #PB_Shortcut_F1 #PB_Shortcut_F2 #PB_Shortcut_F3 #PB_Shortcut_F4 #PB_Shortcut_F5 #PB_Shortcut_F6 #PB_Shortcut_F7 #PB_Shortcut_F8 #PB_Shortcut_F9 #PB_Shortcut_F10 #PB_Shortcut_F11 #PB_Shortcut_F12 #PB_Shortcut_F13 #PB_Shortcut_F14 #PB_Shortcut_F15 #PB_Shortcut_F16 #PB_Shortcut_F17 #PB_Shortcut_F18 #PB_Shortcut_F19 #PB_Shortcut_F20 #PB_Shortcut_F21 #PB_Shortcut_F22 #PB_Shortcut_F23 #PB_Shortcut_F24 #PB_Shortcut_Numlock #PB_Shortcut_ScrollThe above key can be combined with any of the following constants:#PB_Shortcut_Shift #PB_Shortcut_Control #PB_Shortcut_Alt #PB_Shortcut_CommandEvent The number which will be returned by the EventMenu() function. This value has a limited range, from 0 to 64000. By default, a window already has the #PB_Shortcut_Tab and #PB_Shortcut_Tab|#PB_Shortcut_Shift shortcuts to handle tab and shift-tab correctly through the gadgets. A shortcut can be removed with RemoveKeyboardShortcut().
Return value
None.
Remarks
The #PB_Shortcut_Command constant is only useful on Mac OSX and allow to use the 'Apple' key (left or right) to define shortcuts. This constant is also supported on others OS (to ease portability), but will act like #PB_Shortcut_Control. The shortcuts Apple+Q and Apple+P, are predefined on Mac OSX for the #PB_Menu_Quit and #PB_Menu_Preferences menu entries in the application menu and cannot be reassigned. MDI child windows are not supported.
Example
AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_ShortCut_F, 15) ; Will create a keyboard shortcut CTRL+F on the window 0
; which will fires a menu event '15'
Example: Extended example with keyboard shortcuts for menu items and standalone
#Window = 0
Enumeration Menu
#Menu
#PopupMenu
EndEnumeration
Enumeration Menu_items
#mOpen
#mCopy
#mDummy
EndEnumeration
If OpenWindow(#Window, 200, 200, 200, 100, "Press Ctrl+D")
If CreateMenu(#Menu, WindowID(#Window)) ; Create a regular menu with title and one item
MenuTitle("File")
MenuItem(#mOpen, "Open" + #TAB$ + "Ctrl+O")
EndIf
If CreatePopupMenu(#PopupMenu) ; Create an additional pop-up menu
MenuItem(#mCopy, "Copy" + #TAB$ + "Ctrl+Shift+C")
EndIf
AddKeyboardShortcut(#Window, #PB_Shortcut_Control | #PB_Shortcut_D, #mDummy) ; keyboard shortcut standalone (without menu item)
AddKeyboardShortcut(#Window, #PB_Shortcut_Control | #PB_Shortcut_O, #mOpen) ; shortcut for the menu item
AddKeyboardShortcut(#Window, #PB_Shortcut_Control | #PB_Shortcut_Shift | #PB_Shortcut_C, #mCopy) ; shortcut for the pop-up menu item
Repeat
Select WaitWindowEvent()
Case #PB_Event_RightClick ; Display the pop-up menu on right mouse-click
DisplayPopupMenu(#PopupMenu, WindowID(#Window))
Case #PB_Event_Menu
Select EventMenu()
Case #mDummy : Debug "Dummy"
Case #mOpen : Debug "Open"
Case #mCopy : Debug "Copy"
EndSelect
Case #PB_Event_CloseWindow
End
EndSelect
ForEver
EndIf
See Also
RemoveKeyboardShortcut()
Supported OS
All