Page 1 of 1
StringGadget and AddKeyboardShortcut
Posted: Fri Feb 20, 2015 6:27 pm
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
Re: StringGadget and AddKeyboardShortcut
Posted: Fri Feb 20, 2015 7:14 pm
by NikitaOdnorob98
Windows 8, PureBasic 5.22, this code works normaly.
Re: StringGadget and AddKeyboardShortcut
Posted: Fri Feb 20, 2015 7:26 pm
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.

Re: StringGadget and AddKeyboardShortcut
Posted: Fri Feb 20, 2015 8:00 pm
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.
Re: StringGadget and AddKeyboardShortcut
Posted: Fri Feb 20, 2015 8:02 pm
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
Re: StringGadget and AddKeyboardShortcut
Posted: Fri Feb 20, 2015 8:36 pm
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.
