Page 1 of 1

AddKeyboardShortcut not working

Posted: Fri Aug 07, 2009 7:53 am
by John Puccio
I have searched the forums and RTFM but no joy.. The shortcut is underlined ok, but pressing A or a does not do anything. I have winXP support enabled in PB. I don't think this is a PB issue. I'm sure it has to do with my settings. Question is what setting? Did M$ go and change something again?

WinXP SP3
PB4.31 32bit

Thanx In Advance
JP

Code: Select all

ButtonGadget (#Add, #Col + 000, #Row + 000, #Width, #Height, "&Add Contact")
AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_A, #Add)


Re: AddKeyboardShortcut not working

Posted: Fri Aug 07, 2009 8:04 am
by PB
The manual under "AddKeyboardShortcut" says the shortcut triggers a menu
event, which you then need to catch in your main loop and process.

Posted: Fri Aug 07, 2009 10:03 am
by Kaeru Gaman
that's right. Shortcuts can only be connected with Menu Entries, not with Buttons.

I'm not sure but maybe you don't need the Menu itself to be able to recieve the MenuEvent from the Shortcut.

Re: AddKeyboardShortcut not working

Posted: Fri Aug 07, 2009 10:27 am
by PB
> AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_A, #Add)

BTW, you shouldn't be doing the above anyway. Underlined letters are meant
to be used with the Alt key and the letter -- it's in the Windows style guide.
It's what users will expect, unless your app is for private or in-house use.

The above should really be programmed like this:

AddKeyboardShortcut(0, #PB_Shortcut_Alt | #PB_Shortcut_A, #Add)

Just something to consider, if your app is for public use. ;)

Posted: Sat Aug 08, 2009 7:25 am
by John Puccio
Thanks guys for pointing me in the right direction. As it turns out there is nothing wrong with my settings. There is something wrong with the programmer (ME!)..

In my main loop i was trapping #PB_Event_Gadget so I did a test. I changed #PB_Event_Gadget to #PB_Event_Menu and the sortcut worked fine, but now my buttons didn't work. So...

I made a second identical select case structure in the same loop that traps #PB_Event_Menu, now they both work, the sortcut and the buttons.. So my next question is.. Is there a more elegant way of doing this with just one select case instead of 2?

PB thank you for the #PB_Shortcut_Alt tip. I was not aware of that. I was reading through the docs when I came across shortcuts and just wanted to try it out in my code. Now I know. PureBASIC has so many features I wish I could try them all!

You guys rock,

JP

Posted: Sat Aug 08, 2009 8:52 am
by Kaeru Gaman
Is there a more elegant way of doing this with just one select case instead of 2?
nope. why and what for?

note that you could subdevide both with Event_Window and the Event_Gadget with Event_Type...

"elegant" is not a single Select, but a beautiful structural tree subdividing with 42 Selects.
"elegant" is a fractal with a deep recursion, not a flat line.

Posted: Sat Aug 08, 2009 9:10 am
by John Puccio
Kaeru Gaman wrote:"elegant" is not a single Select, but a beautiful structural tree subdividing with 42 Selects.
"elegant" is a fractal with a deep recursion, not a flat line.
Elegant is finding ways to avoid code duplication whenever possible.. ;)

Thanx for the tip!
JP

Posted: Sat Aug 08, 2009 9:30 am
by Kaeru Gaman
right, but having one select for the gadgets and one for the menu is no "duplication" ;)

Posted: Sat Aug 08, 2009 11:16 am
by John Puccio
That's true if that is the only way it will work, but if there is another way then it is code duplication. BTW I looked for Event_Type could not find it.. There is EventType() which is something completely different.

I tried to put #PB_Event_Menu and #PB_Event_Gadget in a seperate select then assign a variable (MyVariable = PB_Event_xxx) depending on what event took place. But it only worked half way...

My eyes are tired but thanks for your help Kaeru, and the poetry. I enjoyed this line especially..
"elegant" is a fractal with a deep recursion, not a flat line.
LOL! Thanks,
JP

Posted: Sat Aug 08, 2009 12:08 pm
by Trond
You can do it like this, but this means both menu events and gadget events triggers the same event handling code, so they must not share numbers unless in the case where you actually want menu events (or shortcuts) to trigger a gadget event.

Code: Select all


Enumeration 
  #ActionAdd

EndEnumeration


OpenWindow(0, 0, 0, 512, 384, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
ButtonGadget(#ActionAdd, 10, 10, 97, 25, "Click")
AddKeyboardShortcut(0, #PB_Shortcut_A | #PB_Shortcut_Control, #ActionAdd)

Repeat
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_Gadget, #PB_Event_Menu
      EventNumber = EventMenu()
      If Event = #PB_Event_Gadget
        EventGadget()
      EndIf
      Select EventNumber
        Case #ActionAdd
          Debug "Adddddd"
      EndSelect
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver

Posted: Sat Aug 08, 2009 10:40 pm
by John Puccio
Yes that is exactly how I want it to work.

all I needed from your example was
Case #PB_Event_Gadget, #PB_Event_Menu

what I had before was
Case #PB_Event_Gadget
...
...
...

and then a second identical select with
Case #PB_Event_Menu
...
...
...

Thank you Trond for providing this most simple solution. I actually feel pretty dumb for not trying that experiment.. As soon as I saw that line of code :shock: I knew that had to be it.

JP

Code: Select all

EnableExplicit

; Button + Keyboard Shortcut multi control demo
; click on the Add button or press alt+A performs the same action
; same with alt+Q

Enumeration 
  #Add
  #Quit
EndEnumeration 

If OpenWindow(0, 0, 0, 512, 384, "Multi Control Demo", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)

  ButtonGadget (#Add, 10, 10, 97, 25, "&Add") 
  AddKeyboardShortcut(0, #PB_Shortcut_Alt | #PB_Shortcut_A, #Add)

  ButtonGadget (#Quit, 10, 40, 97, 25, "&Quit") 
  AddKeyboardShortcut(0, #PB_Shortcut_Alt | #PB_Shortcut_Q, #Quit)

  Repeat 
    Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      CloseWindow(0)
      End
    Case #PB_Event_Gadget, #PB_Event_Menu
      Select EventGadget() 
      Case #Add
        Debug "Add was pressed"
      Case #Quit
        CloseWindow(0)
        End  
      EndSelect
    EndSelect 
  ForEver

 EndIf
 End

Posted: Sat Aug 08, 2009 10:48 pm
by Kaeru Gaman
mind the constriction
> they must not share numbers

keep in mind, if you have a lot of Gadgets and a lot of MenuEntries,
you'll have to have Gaps in both Enumerations,
because not every gadget would have a Menu or Shortcut associated and vice versa...
I looked for Event_Type could not find it.. There is EventType() which is something completely different.
sorry, wrong notation. but correct meaning.

Posted: Sat Aug 08, 2009 11:05 pm
by John Puccio
I'll keep that in mind Kaeru. I'll just have to wait and see how the code evolves over time.

In this case I am re-creating an old dos program I wrote 10+ years ago. So I am attempting to create the same look and feel of the original program. It won't have any menus. The buttons are the menu so it's not a problem at least for now... It's a simple phone book program but I figgure this would be a good project for me to get familiar with PB.

JP

Posted: Sat Aug 08, 2009 11:20 pm
by Kaeru Gaman
yo thats cool.
Enumer the gadgets right thru, equal the menuevent to gadgetevent, and put shortcuts on the same ID like buttons.
should faithfully work.
if you have no menu at all, you could put shortcuts on buttons like that.