Typing Menu Letter into StringGadget.

Just starting out? Need help? Post your questions and find answers here.
User avatar
matalog
Enthusiast
Enthusiast
Posts: 304
Joined: Tue Sep 05, 2017 10:07 am

Typing Menu Letter into StringGadget.

Post 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
infratec
Always Here
Always Here
Posts: 7598
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Typing Menu Letter into StringGadget.

Post 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.
AZJIO
Addict
Addict
Posts: 2155
Joined: Sun May 14, 2017 1:48 am

Re: Typing Menu Letter into StringGadget.

Post 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
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4951
Joined: Sun Apr 12, 2009 6:27 am

Re: Typing Menu Letter into StringGadget.

Post 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
Egypt my love
BarryG
Addict
Addict
Posts: 4160
Joined: Thu Apr 18, 2019 8:17 am

Re: Typing Menu Letter into StringGadget.

Post by BarryG »

Rashad, I can still type "w" into the StringGadget with your code (but not Space).
User avatar
matalog
Enthusiast
Enthusiast
Posts: 304
Joined: Tue Sep 05, 2017 10:07 am

Re: Typing Menu Letter into StringGadget.

Post by matalog »

Thanks guys.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1282
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Typing Menu Letter into StringGadget.

Post 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 :wink:
Image Image
BarryG
Addict
Addict
Posts: 4160
Joined: Thu Apr 18, 2019 8:17 am

Re: Typing Menu Letter into StringGadget.

Post 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?
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1282
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Typing Menu Letter into StringGadget.

Post 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?
Image Image
Post Reply