AutoKeyboardShortcuts()

Share your advanced PureBasic knowledge/code with the community.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

AutoKeyboardShortcuts()

Post by PB »

This tip was created in direct response to:

http://www.purebasic.fr/english/viewtop ... 6&p=456894

This procedure sets up keyboard shortcuts for the char that immediately
follows the ampersand char. Makes it simple to set up the shortcuts. Run
it as-is at first, then comment the first two gadget lines and uncomment
the second two gadget lines and run again, to see how it easily adapts to
whatever shortcut text is used. :)

As I said, this is just a starting point to show auto-shortcuts can be done
without lots of extra coding. But it doesn't do any error-checking with any
other keyboard shortcuts that you may have, etc. Work on it. :)

Code: Select all

Procedure AutoKeyboardShortcuts()

  For gad=1 To 2
    text$=GetGadgetText(gad)
    pos=FindString(text$,"&")
    If pos<>0 And CountString(text$,"&")=1
      c=Asc(UCase(Mid(text$,pos+1,1)))
      AddKeyboardShortcut(0,c,c)
    EndIf
  Next

EndProcedure

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")
;ButtonGadget(1,20,20,100,25,"Enter &name")
;ButtonGadget(2,20,60,100,25,"Enter &code")

AutoKeyboardShortcuts()

Repeat

  ev=WaitWindowEvent()

  If ev=#PB_Event_Menu
    Debug "You pressed: "+Chr(EventMenu())
  EndIf

Until ev=#PB_Event_CloseWindow
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
idle
Always Here
Always Here
Posts: 5915
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: AutoKeyboardShortcuts()

Post by idle »

Good tip there PB
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: AutoKeyboardShortcuts()

Post by Vera »

Thank you both

for you smart and inspiring examples :-)
Post Reply