how can I add shortcuts for buttons?

Windows specific forum
sartic
Enthusiast
Enthusiast
Posts: 143
Joined: Thu Aug 26, 2010 8:26 am

how can I add shortcuts for buttons?

Post by sartic »

Example I have OK, left, right buttons and I want keyboard shortcuts ENTER, left arrow, right arrow to join my buttons?
Registered user of PB (on Linux Mint 21.1 & Win 10 64bit)
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: how can I add shortcuts for buttons?

Post by Vera »

Hi,

maybe this can help you out

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
EndIf
cheers ~ Vera
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: how can I add shortcuts for buttons?

Post by Rescator »

sartic wrote:Example I have OK, left, right buttons and I want keyboard shortcuts ENTER, left arrow, right arrow to join my buttons?
Please 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.
Post Reply