Regarding Accelerators

Just starting out? Need help? Post your questions and find answers here.
missile69
User
User
Posts: 25
Joined: Mon Feb 21, 2011 12:15 pm

Re: Regarding Accelerators

Post by missile69 »

Although the functionality you need isn't built into PureBasic, one thing that makes PB a pleasure to use for Windows API programming, is that many api functions are already recognized by the editor's auto-complete function and most of the structures needed are also already defined.

On the MSDN page for keyboard accelerators: http://msdn.microsoft.com/en-us/library ... g_rt_table for example, the required api calls and the ACCEL data structure they use is defined and ready to use in PureBasic. The code posted by user PB here: http://www.purebasic.fr/english/viewtop ... 12&t=61087 can be easily modified to use the api calls listed on that MSDN page and then you can have the keyboard shortcuts work the way you need them to.

Welcome to PureBasic and happy programming!
User avatar
idle
Always Here
Always Here
Posts: 5915
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Regarding Accelerators

Post by idle »

my two cents worth building on PB's tip as an interim solution something like this maybe?
works the way jscholes asks

Code: Select all

Global NewMap hotkeys() 

Macro AddAccel(text,gadget)
  pos=FindString(text,"&")
    If pos<>0 And CountString(text,"&")=1
      c=Asc(UCase(Mid(text,pos+1,1)))
      AddKeyboardShortcut(0,c,c)
      AddKeyboardShortcut(0,#PB_Shortcut_Alt | c,c<<2)
    EndIf 
    hotkeys(Str(c)) = gadget 
    hotkeys(Str(c)) = gadget 
    hotkeys(Str(c<<2)) = gadget 
  EndMacro 
  
Macro _ButtonGadget(Gadget,x,y,width,height,text,flags=0) 
  ButtonGadget(Gadget,x,y,width,height,text,flags) 
  AddAccel(text,gadget)
EndMacro    


OpenWindow(0,200,200,150,120,"test",#PB_Window_SystemMenu)
_ButtonGadget(1,20,20,100,25,"Enter &username")
_ButtonGadget(2,20,60,100,25,"Enter &password")

Repeat

  ev=WaitWindowEvent()

  If ev=#PB_Event_Menu
    eventmenu = EventMenu()
    If EventMenu > 256  
      If hotkeys(Str(EventMenu))  
        SetActiveGadget(hotkeys(Str(EventMenu))) 
        Debug "set active gadget " + Str(hotkeys(Str(EventMenu)))    
      EndIf    
    ElseIf GetActiveGadget() = hotkeys(Str(EventMenu)) 
      Debug "You pressed: "+Chr(EventMenu())
    EndIf
  EndIf 
Until ev=#PB_Event_CloseWindow
Windows 11, Manjaro, Raspberry Pi OS
Image
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: Regarding Accelerators

Post by mestnyi »

PureBasic это не для Windows. Это для Windows, Linux и Mac.
I sometimes think that this is not the case,
because 80% of the responses focused on Windows :(
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Regarding Accelerators

Post by Vera »

Hello jscholes,
welcome to the forum :-) and I hope you do have a bit more patience but a few hours.

I've tried to merge your example (which I also layouted a bit) with idle's code. I exchanged the mute (non active) textgadgets with buttons and the keyboard behavior comes close to what you're reaching out for.

Code: Select all

EnableExplicit

Enumeration 0 Step 1
  #WindowMain
  #UsernameField
  #PasswordField
EndEnumeration
Enumeration 20
  #UsernameText
  #PasswordText
  #ConnectButton
  #HelpButton
EndEnumeration

Global NewMap hotkeys()

Macro AddAccel(text, gadget)
Define pos.i, c.i
  pos = FindString(text, "&",1)
  If pos <> 0 And CountString(text, "&") = 1
    c = Asc(UCase(Mid(text, pos + 1, 1)))
    AddKeyboardShortcut(0, c, c)
    AddKeyboardShortcut(0, #PB_Shortcut_Alt | c, c << 2)
  EndIf
  hotkeys(Str(c))      = gadget
  hotkeys(Str(c))      = gadget
  hotkeys(Str(c << 2)) = gadget
EndMacro

Macro _ButGadget(Gadget, x, y, width, height, text, flags = 0)
  ButtonGadget(Gadget, x, y, width, height, text, flags)
  AddAccel(text, gadget)
EndMacro


Procedure WindowCreate()
  Protected Flags
  Flags = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_TitleBar
  OpenWindow(#WindowMain, 50, 50, 450, 400, "Accelerator Test", Flags)
  _ButGadget(#UsernameText, 5, 10, 120, 25, "&Username")
  StringGadget(#UsernameField, 140, 10, 100, 25, "")
  _ButGadget(#PasswordText, 5, 40, 120, 25, "&Password")
  StringGadget(#PasswordField, 140, 40, 100, 25, "", #PB_String_Password)
  _ButGadget(#ConnectButton, 30, 70, 80, 25, "&Connect")
  _ButGadget(#HelpButton, 30, 100, 80, 25, "&Help")
  SetActiveGadget(#UsernameField)
EndProcedure

WindowCreate()

Define.l Event, EventWindow, EventGadget, EventType, EventMenu

Repeat
  Event = WaitWindowEvent()
  
  If Event = #PB_Event_Menu 
    EventMenu = EventMenu()
    If EventMenu > 256
      If hotkeys(Str(EventMenu))
        SetActiveGadget(hotkeys(Str(EventMenu)))
        Debug "set active gadget " + Str(hotkeys(Str(EventMenu)))
      EndIf
    ElseIf GetActiveGadget() = hotkeys(Str(EventMenu))
      Debug "You pressed: " + Chr(EventMenu())
    ElseIf GetActiveGadget() <> hotkeys(Str(EventMenu)) And GetActiveGadget() >= 20
      SetActiveGadget(hotkeys(Str(EventMenu)))
      Debug "Jumped to menu: " + Chr(EventMenu())
    EndIf
  EndIf
  
Until Event = #PB_Event_CloseWindow
BUT there's one issue I couldn't fix: all characters which are used in the hotkeys are now omitted from being available in the string fields. Maybe someone else knows how that could be resolved.

greetings ~ Vera


@mestnyi
you're not allone ;-)
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Regarding Accelerators

Post by Tenaja »

mestnyi wrote:
PureBasic это не для Windows. Это для Windows, Linux и Mac.
I sometimes think that this is not the case,
because 80% of the responses focused on Windows :(
So far, 100% of my PB development has been on Windows...

But I sought out PB only because I wanted a cross-platform tool. Otherwise I would have stuck with VB.
Post Reply