AddKeyboardShortcut not working

Just starting out? Need help? Post your questions and find answers here.
User avatar
John Puccio
User
User
Posts: 26
Joined: Fri Jun 12, 2009 6:56 am
Location: My Keyboard

AddKeyboardShortcut not working

Post 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)

PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: AddKeyboardShortcut not working

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: AddKeyboardShortcut not working

Post 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. ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
John Puccio
User
User
Posts: 26
Joined: Fri Jun 12, 2009 6:56 am
Location: My Keyboard

Post 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
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
User avatar
John Puccio
User
User
Posts: 26
Joined: Fri Jun 12, 2009 6:56 am
Location: My Keyboard

Post 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
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

right, but having one select for the gadgets and one for the menu is no "duplication" ;)
oh... and have a nice day.
User avatar
John Puccio
User
User
Posts: 26
Joined: Fri Jun 12, 2009 6:56 am
Location: My Keyboard

Post 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
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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
User avatar
John Puccio
User
User
Posts: 26
Joined: Fri Jun 12, 2009 6:56 am
Location: My Keyboard

Post 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
Last edited by John Puccio on Sat Aug 08, 2009 10:55 pm, edited 1 time in total.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
User avatar
John Puccio
User
User
Posts: 26
Joined: Fri Jun 12, 2009 6:56 am
Location: My Keyboard

Post 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
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
Post Reply