StringGadget and AddKeyboardShortcut

Just starting out? Need help? Post your questions and find answers here.
mestnyi
Addict
Addict
Posts: 1103
Joined: Mon Nov 25, 2013 6:41 am

StringGadget and AddKeyboardShortcut

Post by mestnyi »

Why can't I enter a space?

Code: Select all

Procedure TestHandler()
    Debug "Test menu event"
  EndProcedure
  
  OpenWindow(0, 100, 100, 200, 50, "Click test", #PB_Window_SystemMenu)
  AddKeyboardShortcut(0, #PB_Shortcut_Space, 1)
  
  StringGadget(0, 4,  4, 192, 20, "Normal StringGadget...")
    
  CreateMenu(0, WindowID(0))
    MenuTitle("File")
      MenuItem(0, "Test")
      MenuItem(1, "Quit")
  
  BindMenuEvent(0, 1, @TestHandler())
  
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
NikitaOdnorob98
User
User
Posts: 74
Joined: Fri Jun 29, 2012 4:50 pm

Re: StringGadget and AddKeyboardShortcut

Post by NikitaOdnorob98 »

Windows 8, PureBasic 5.22, this code works normaly.
User avatar
TI-994A
Addict
Addict
Posts: 2752
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: StringGadget and AddKeyboardShortcut

Post by TI-994A »

Hi mestnyi. If you're trying to activate the menu by pressing the space key, this should do it:

Code: Select all

Procedure TestHandler()
  keybd_event_(#VK_MENU, 0, 0, 0)
  keybd_event_(#VK_MENU, 0, #KEYEVENTF_KEYUP, 0)
  keybd_event_(#VK_F, 0, 0, 0)
  keybd_event_(#VK_F, 0, #KEYEVENTF_KEYUP, 0)
EndProcedure

OpenWindow(0, 100, 100, 200, 50, "Click test", #PB_Window_SystemMenu)
AddKeyboardShortcut(0, #PB_Shortcut_Space, 0)

StringGadget(0, 4,  4, 192, 20, "Normal StringGadget...")

CreateMenu(0, WindowID(0))
MenuTitle("File")
MenuItem(1, "Test")
MenuItem(2, "Quit")
BindMenuEvent(0, 0, @TestHandler())

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
    Case #PB_Event_Menu
      Select EventMenu()
        Case 1
          Debug "Menu item TEST clicked."
        Case 2
          appQuit = 1
      EndSelect
  EndSelect
Until appQuit = 1
It's a Windows-only solution, but hope it helps. :)
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
TI-994A
Addict
Addict
Posts: 2752
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: StringGadget and AddKeyboardShortcut

Post by TI-994A »

mestnyi wrote:Why can't I enter a space?
Once you've hooked a key with the AddKeyboardShortcut() function, it will be intercepted by PureBasic's menu events to trigger the corresponding action. That is why accelerator functions are formed by key combinations, with the CTRL or ALT keys.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4995
Joined: Sun Apr 12, 2009 6:27 am

Re: StringGadget and AddKeyboardShortcut

Post by RASHAD »

Code: Select all

Procedure TestHandler()
    Debug "Test menu event"
  EndProcedure
  
  OpenWindow(0, 100, 100, 200, 100, "Click test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered) 
  AddKeyboardShortcut(0, #PB_Shortcut_Space, 1)
  StringGadget(0, 4,  4, 192, 20, "Normal StringGadget...")
  ButtonGadget(1,10,50,80,20,"Switch Space")
    
  CreateMenu(0, WindowID(0))
    MenuTitle("File")
      MenuItem(0, "Test")
      MenuItem(1, "Quit")     
  
  BindMenuEvent(0, 1, @TestHandler())
  
  Repeat
    Select WaitWindowEvent()
          Case #PB_Event_CloseWindow
                  Quit = 1
                  
          Case #PB_Event_Gadget
                Select EventGadget()
                       Case 0                              

                              
                       Case 1
                              Run ! 1
                              If Run = 1
                                    UnbindMenuEvent(0, 1, @TestHandler())
                                    RemoveKeyboardShortcut(0,#PB_Shortcut_All) 
                              Else
                                   AddKeyboardShortcut(0, #PB_Shortcut_Space, 1)
                                   BindMenuEvent(0, 1, @TestHandler())
                              EndIf
                EndSelect
 
    EndSelect
  Until Quit = 1
Egypt my love
mestnyi
Addict
Addict
Posts: 1103
Joined: Mon Nov 25, 2013 6:41 am

Re: StringGadget and AddKeyboardShortcut

Post by mestnyi »

I understand that it is logical, and I thought that would have this behavior, that is, if the active gadget processed event gadget if no event window. Here is an example of how I imagined it.

Code: Select all

Procedure TestHandler()
  Debug "Test menu event"
  EndProcedure
  
  OpenWindow(0, 100, 100, 200, 150, "Click test", #PB_Window_SystemMenu)
      AddKeyboardShortcut(0, #PB_Shortcut_Space, 1)
  
  StringGadget(0, 4,  4, 192, 20, "Normal StringGadget...")
    
  CreateMenu(0, WindowID(0))
    MenuTitle("File")
      MenuItem(0, "Test")
      MenuItem(1, "Quit")
  
  BindMenuEvent(0, 1, @TestHandler())
  
  Repeat
    Event = WaitWindowEvent()
    If Event = #PB_Event_LeftClick
      SetActiveGadget(-1)
    EndIf
    If GetActiveGadget() >=0
      RemoveKeyboardShortcut(0, #PB_Shortcut_Space)
    Else
      AddKeyboardShortcut(0, #PB_Shortcut_Space, 1)
    EndIf
  Until Event = #PB_Event_CloseWindow
  
  
Made a mistake, it happens to everyone. :)
Post Reply