Page 1 of 1
Typing Menu Letter into StringGadget.
Posted: Tue Mar 04, 2025 3:22 pm
by matalog
This code stops the letter W from working in the StringGadget. Is there any way to use the W key as a menu shortcut and have it work fine on the Stringgadget?
Code: Select all
Enumeration
#Menu_Escape
#Menu_Space
#Menu_W
EndEnumeration
main = OpenWindow(#PB_Any, 100, 100, 300, 200, "Window")
StringGadget(1,20,20,100,24,"Text")
AddKeyboardShortcut(main, #PB_Shortcut_Escape, #Menu_Escape)
AddKeyboardShortcut(main, #PB_Shortcut_Space, #Menu_Space)
AddKeyboardShortcut(main, #PB_Shortcut_W, #Menu_W)
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Menu
Select EventMenu()
Case #Menu_Escape
quit = #True
Case #Menu_Space
Case #Menu_W
EndSelect
Case #PB_Event_Gadget
EventGadget = EventGadget()
Select EventGadget
EndSelect
Case #PB_Event_CloseWindow
Quit=#True
EndSelect
Until quit
Re: Typing Menu Letter into StringGadget.
Posted: Tue Mar 04, 2025 3:27 pm
by infratec
If your StringGadget gets the focus you have to remove the shortcut.
If your StringGadget losts the focus you have to add it again.
Re: Typing Menu Letter into StringGadget.
Posted: Tue Mar 04, 2025 5:20 pm
by AZJIO
https://www.purebasic.fr/english/viewto ... 02#p636802
Code: Select all
Case #WM_KEYDOWN
KeyCode = EventlParam() >> 16 & $FF
Select KeyCode
Case 28
If GetActiveGadget() = #Gadget
Re: Typing Menu Letter into StringGadget.
Posted: Tue Mar 04, 2025 5:51 pm
by RASHAD
Hi matalog
Use what infratec suggested
Code: Select all
Enumeration
#Menu_Escape
#Menu_Space
#Menu_W
EndEnumeration
main = OpenWindow(#PB_Any, 100, 100, 300, 200, "Window")
StringGadget(1,20,20,100,24,"Text")
AddKeyboardShortcut(main, #PB_Shortcut_Escape, #Menu_Escape)
AddKeyboardShortcut(main, #PB_Shortcut_Space, #Menu_Space)
AddKeyboardShortcut(main, #PB_Shortcut_W, #Menu_W)
Repeat
Event = WaitWindowEvent(1)
Select Event
Case #PB_Event_Menu
Select EventMenu()
Case #Menu_Escape
quit = #True
Case #Menu_Space
Case #Menu_W
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
Case 1
Select EventType()
Case #PB_EventType_Focus
RemoveKeyboardShortcut(main,#PB_Shortcut_W)
Case #PB_EventType_LostFocus
AddKeyboardShortcut(main, #PB_Shortcut_W, #Menu_W)
EndSelect
EndSelect
Case #PB_Event_CloseWindow
Quit=#True
EndSelect
Until quit
Re: Typing Menu Letter into StringGadget.
Posted: Tue Mar 04, 2025 10:08 pm
by BarryG
Rashad, I can still type "w" into the StringGadget with your code (but not Space).
Re: Typing Menu Letter into StringGadget.
Posted: Thu Mar 06, 2025 12:07 am
by matalog
Thanks guys.
Re: Typing Menu Letter into StringGadget.
Posted: Thu Mar 06, 2025 12:42 am
by Paul
BarryG wrote: Tue Mar 04, 2025 10:08 pm
Rashad, I can still type "w" into the StringGadget with your code (but not Space).
You would have to add/remove "space" as well if you want "space" to work

Re: Typing Menu Letter into StringGadget.
Posted: Thu Mar 06, 2025 1:02 am
by BarryG
But why? There's a Select/Case, so if I don't press Space, shouldn't the Case detect "W" and do nothing?
Re: Typing Menu Letter into StringGadget.
Posted: Thu Mar 06, 2025 1:23 am
by Paul
When Space is pressed a menu event is triggered
When W is press a menu event is triggered
If you are typing in the StringGadget you don't want a menu event to trigger, you want Space or W to display in the StringGadget which is why you would remove the keyboard shortcut for Space and W when the StringGadget is in Focus and add them again when StringGadget is not in Focus (so menu events are triggered again)
Maybe it would make more sense if you put a Debug "SPACE" after Case #Menu_Space and a Debug "W" after Case #Menu_W so you see these events triggered in the code snippet?