how can I add shortcuts for buttons?
Posted: Thu Dec 09, 2010 1:46 pm
Example I have OK, left, right buttons and I want keyboard shortcuts ENTER, left arrow, right arrow to join my buttons?
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
If OpenWindow(0, 0, 0, 130, 90, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(1, 20, 20, 90, 22, "button")
ButtonGadget(2, 20, 50, 90, 22, "button 2")
AddKeyboardShortcut(0, #PB_Shortcut_Return, 0) ; assign enter-key
AddKeyboardShortcut(0, #PB_Shortcut_Up, 1) ; assign up-key
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case 1
Debug "button clicked"
Case 2
Debug "button 2 clicked"
EndSelect
Case #PB_Event_Menu
If EventMenu() = 0 And GetActiveGadget() = 1 ; if enter-key while button has the focus
Debug "enter was pressed"
ElseIf EventMenu() = 1 And GetActiveGadget() = 2 ; if up-key while button2 has the focus
Debug "up-key was pressed"
EndIf
EndSelect
Until Event = #PB_Event_CloseWindow
EndIfPlease be aware that Windows (and possibly other OS) uses the enter and arrow keys for keyboard navigation as an alternative to/in addition to the mouse, so if it's a windowed app you may want to use some other keys instead. (windowed games, and fullscreen games or fullscreen apps are usually the exception) If you must use them, then allow the user to enable/disable them so they can choose instead.sartic wrote:Example I have OK, left, right buttons and I want keyboard shortcuts ENTER, left arrow, right arrow to join my buttons?