The keybord accelerator for ButtonGadget

Just starting out? Need help? Post your questions and find answers here.
Jan2004
Enthusiast
Enthusiast
Posts: 163
Joined: Fri Jan 07, 2005 7:17 pm

The keybord accelerator for ButtonGadget

Post by Jan2004 »

Is it possible to add the keybord accelerator for ButtonGadget: similiar to AddKeyboardShortcut for menu ?

Jan
Jellybean
User
User
Posts: 95
Joined: Wed Aug 24, 2005 7:33 pm

Post by Jellybean »

Accellerators and shortcuts are two different things.
Jan2004
Enthusiast
Enthusiast
Posts: 163
Joined: Fri Jan 07, 2005 7:17 pm

Post by Jan2004 »

Jellybean, thank you for your answer.

My terminology is based on the old Basic, I have been using for a few years: CA-Realizer. In CA-Realizer printed manual stays, for example: "You can set accelerator keys for menu items with ...". The next quote from the same source: "The keyboard equivalents of commands are called keyboard accelerators". Add the quote from PureBasic help file, topic AddKeyboardShortcut: "A shortcut generates a menu event (like a menu item) as most of them are used in conjonction with menus".
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'

Simply: I want achieve the same effect with the ButtonGadget: fire a button gadget with a keyboard shortcut. I want to use a button without the mouse.

Jellybean, if you know all subtleties, explain me, please.

Jan
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

You can modify this code :

viewtopic.php?t=16773
Jellybean
User
User
Posts: 95
Joined: Wed Aug 24, 2005 7:33 pm

Post by Jellybean »

Image
See those Ctrl+N and Ctrl+O stuff? Those are keyboard shortcuts. You can, when the window has focus, press that key combination to invoke the corresponding menu command.

See those underlined letters (underlined N in New, etc...)? Those are the accellerators. Pressing the keyboard key with the underlined letter will invoke the menu command. These are local to the menu and only active when the menu is open. Different submenus can use the same letters, because they only apply to the current open menu.

Image
See that underlined D in Dial Properties? That's the keyboard accellerator for that button. Keyboard accellerators are made by preceding a character with an &.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

I made you a little test program which demonstrates how I would do it. Please keep in mind that my VCR has flashed 12:00 for the past seven years, so a word to the wise :lol:

Code: Select all


Enumeration 1
  #Window_Form1
  #Gadget_Form1_Button2
  #Gadget_Form1_Text2
  #Gadget_Form1_Text3
  #Button2_Shortcut = 13101 ; I obtained this number by adding the keyboard shortcut
EndEnumeration              ; and debugging the window event that fires when you press ctrl-F.
                            ; It WILL be a different number in your program depending on what gadgets
                            ; you have that fire events


If OpenWindow(#Window_Form1,277,165,400,300,#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible,"Shortcut Example by Network Maestro")
  If CreateGadgetList(WindowID(#Window_Form1))
    ButtonGadget(#Gadget_Form1_Button2,130,240,115,20,"Do Stuff Now")
    TextGadget(#Gadget_Form1_Text2,120,70,190,15,"CTRL_F Presses the button")
    TextGadget(#Gadget_Form1_Text3,130,145,190,15,"The rain in Spain")
    HideWindow(#Window_Form1,0)
  EndIf
EndIf
AddKeyboardShortcut(#Window_Form1, #PB_Shortcut_Control | #PB_Shortcut_F, #Button2_Shortcut)

quitForm1=0
Repeat
  EventID  =WaitWindowEvent()
  GadgetID =EventGadgetID()
                                      ;Process shortcuts first
  If EventID = #Button2_Shortcut      ;if the control-F shortcut was pressed,
    EventID = #PB_Event_Gadget        ;just change the event details to same as button2 press
    GadgetID = #Gadget_Form1_Button2  ;and then process the events normally
  EndIf
  
  Select EventID
    Case #PB_Event_CloseWindow
         quitForm1=1
    Case #PB_Event_Gadget
      Select GadgetID
        Case #Gadget_Form1_Button2 
          If GetGadgetText(#Gadget_Form1_text3) = "The rain in Spain"
            SetGadgetText(#Gadget_Form1_text3, "Falls mainly on the plain")
          Else
            SetGadgetText(#Gadget_Form1_text3, "The rain in Spain")
          EndIf
      EndSelect
  EndSelect
Until quitForm1
CloseWindow(#Window_Form1)
End
BERESHEIT
Jan2004
Enthusiast
Enthusiast
Posts: 163
Joined: Fri Jan 07, 2005 7:17 pm

Post by Jan2004 »

Jellybean, thank you once again for explaniation.
Netmaestro, Droopy: your support is great.

Jan
Post Reply