User input example printing 2-3 times in debug

Just starting out? Need help? Post your questions and find answers here.
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 318
Joined: Sun Aug 29, 2021 4:34 am

User input example printing 2-3 times in debug

Post by Distorted Pixel »

Why is it outputing 2 and some times 3 times instead of just one time. I just want one time

https://mega.nz/file/lp0VjYrR#wdfxkWFwa ... p79JbP-uXE

Code: Select all

; output PlayerName variable to debug
  Global PlayerName.s
  
  If OpenWindow(0, 0, 0, 322, 50, "StringGadget example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
      InitSprite()
      InitKeyboard()
      If OpenWindowedScreen(WindowID(0), 0, 0, 322, 50)
        StringGadget(1, 8,  10, 306, 20, "" + PlayerName)
     
      
   Repeat
          
      ExamineKeyboard()
         PlayerName = GetGadgetText(1)
         If KeyboardPushed(#PB_Key_Return)
             Debug PlayerName
         EndIf
         
      
    Until WaitWindowEvent() = #PB_Event_CloseWindow
     EndIf  
  EndIf
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
jacdelad
Addict
Addict
Posts: 2035
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: User input example printing 2-3 times in debug

Post by jacdelad »

I don't know what your goal is (what kind of program), but:

Code: Select all

; output PlayerName variable to debug
Global PlayerName.s

If OpenWindow(0, 0, 0, 322, 50, "StringGadget example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  InitSprite()
  InitKeyboard()
  If OpenWindowedScreen(WindowID(0), 0, 0, 322, 50)
    StringGadget(1, 8,  10, 306, 20, "" + PlayerName)
    
    
    AddKeyboardShortcut(0,#PB_Shortcut_Return,1000)
    Repeat
      
      ;ExamineKeyboard()
      
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Break
        Case #PB_Event_Menu
          Select EventMenu()
            Case 1000
              If GetActiveGadget()=1
                PlayerName = GetGadgetText(1)
                Debug PlayerName
              EndIf
              
          EndSelect
      EndSelect
      
    ForEver
    
  EndIf  
EndIf
ExamineKeyboard is used for...let's call it "game inputs". Keystrokes in gadgets are better handled with Keyboardshortcuts.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 318
Joined: Sun Aug 29, 2021 4:34 am

Re: User input example printing 2-3 times in debug

Post by Distorted Pixel »

jacdelad wrote: Thu Nov 10, 2022 3:02 am I don't know what your goal is (what kind of program), but:

Code: Select all

; output PlayerName variable to debug
Global PlayerName.s

If OpenWindow(0, 0, 0, 322, 50, "StringGadget example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  InitSprite()
  InitKeyboard()
  If OpenWindowedScreen(WindowID(0), 0, 0, 322, 50)
    StringGadget(1, 8,  10, 306, 20, "" + PlayerName)
    
    
    AddKeyboardShortcut(0,#PB_Shortcut_Return,1000)
    Repeat
      
      ;ExamineKeyboard()
      
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Break
        Case #PB_Event_Menu
          Select EventMenu()
            Case 1000
              If GetActiveGadget()=1
                PlayerName = GetGadgetText(1)
                Debug PlayerName
              EndIf
              
          EndSelect
      EndSelect
      
    ForEver
    
  EndIf  
EndIf
ExamineKeyboard is used for...let's call it "game inputs". Keystrokes in gadgets are better handled with Keyboardshortcuts.
My goal is to create an example of user input like typing in their name. Eventually I want to put it into my game I'm developing
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
jacdelad
Addict
Addict
Posts: 2035
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: User input example printing 2-3 times in debug

Post by jacdelad »

Like I said, if you use gadgets, then use a hotkey. If you want the user to type the name into something else (like a canvas or opengl gadget), do it with ExamineKeyboard.
Since I only work with gadgets, someone else may be more helpful on the second one.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Comfort
User
User
Posts: 32
Joined: Thu Jul 05, 2018 11:52 pm

Re: User input example printing 2-3 times in debug

Post by Comfort »

With or without a keyboard shortcut, it needs a test for key repeat.

Something like this (without shortcut):

Code: Select all

; output PlayerName variable to debug
Global PlayerName.s

If OpenWindow(0, 0, 0, 322, 50, "StringGadget example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  InitSprite()
  InitKeyboard()
  If OpenWindowedScreen(WindowID(0), 0, 0, 322, 50)
    StringGadget(1, 8,  10, 306, 20, "" + PlayerName)
        
    Repeat
      
      ExamineKeyboard()
      PlayerName = GetGadgetText(1)
     
      If KeyboardPushed(#PB_Key_Return) And return_key_down = #False
        Debug PlayerName
        return_key_down = #True
      ElseIf KeyboardReleased(#PB_Key_Return) And return_key_down = #True
        return_key_down = #False
      EndIf
            
    Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf  
EndIf
User avatar
mk-soft
Always Here
Always Here
Posts: 6327
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: User input example printing 2-3 times in debug

Post by mk-soft »

@Distored Pixel

I don't know what you want to achieve, but...

- Standard gadgets do not belong in the OpenWindowedScreen window area.
- A FlipBuffer always belongs in the loop
- In a loop all window events must always be queried in their own loop

Please also have a look at the example of PB WindowedScreen.
If you want to have gadgets for a game in the screen, have a look at the Window3D and Gadget3D library.

So far I only see that you want to query the keyboard. Unless you are talking about 2D/3D graphics output for a game, OpenWindowedScreen has no business here.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 318
Joined: Sun Aug 29, 2021 4:34 am

Re: User input example printing 2-3 times in debug

Post by Distorted Pixel »

mk-soft wrote: Thu Nov 10, 2022 9:46 am @Distored Pixel

I don't know what you want to achieve, but...

- Standard gadgets do not belong in the OpenWindowedScreen window area.
- A FlipBuffer always belongs in the loop
- In a loop all window events must always be queried in their own loop

Please also have a look at the example of PB WindowedScreen.
If you want to have gadgets for a game in the screen, have a look at the Window3D and Gadget3D library.

So far I only see that you want to query the keyboard. Unless you are talking about 2D/3D graphics output for a game, OpenWindowedScreen has no business here.
I have not included all the code where the input routine will above will go into. Everything in my current code works, but I don't have a user input routine implemented into it yet. That is what this is all about. I copied and pasted an example from the help files into a new code file to try to get an user input routine to work on it's own first and then implement it in.

Currently I have a window with an ImageGadget and 2 TextGadgets and a StringGadget. My first post is the example I am working on and I am trying to get it so the user can type his name in and hit the "ReturnKey" and it will save their name to a file. But first I was trying to get it work to debug properly and then work on the saving part.
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 318
Joined: Sun Aug 29, 2021 4:34 am

Re: User input example printing 2-3 times in debug

Post by Distorted Pixel »

Comfort wrote: Thu Nov 10, 2022 6:38 am With or without a keyboard shortcut, it needs a test for key repeat.

Something like this (without shortcut):

Code: Select all

; output PlayerName variable to debug
Global PlayerName.s

If OpenWindow(0, 0, 0, 322, 50, "StringGadget example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  InitSprite()
  InitKeyboard()
  If OpenWindowedScreen(WindowID(0), 0, 0, 322, 50)
    StringGadget(1, 8,  10, 306, 20, "" + PlayerName)
        
    Repeat
      
      ExamineKeyboard()
      PlayerName = GetGadgetText(1)
     
      If KeyboardPushed(#PB_Key_Return) And return_key_down = #False
        Debug PlayerName
        return_key_down = #True
      ElseIf KeyboardReleased(#PB_Key_Return) And return_key_down = #True
        return_key_down = #False
      EndIf
            
    Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf  
EndIf
Thank you, yours looks like what I was trying to do, but some times my mind goes blank with what I should be doing in the code. Currently I'm not real good with If/ElseIf's and Select/EndSelect.

Yours works the same as the Select/EndSelect above. I'll study yours and the Select/EndSelect above and learn from them both and figure out which one is the best to use
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 318
Joined: Sun Aug 29, 2021 4:34 am

Re: User input example printing 2-3 times in debug

Post by Distorted Pixel »

jacdelad wrote: Thu Nov 10, 2022 3:02 am I don't know what your goal is (what kind of program), but:

Code: Select all

; output PlayerName variable to debug
Global PlayerName.s

If OpenWindow(0, 0, 0, 322, 50, "StringGadget example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  InitSprite()
  InitKeyboard()
  If OpenWindowedScreen(WindowID(0), 0, 0, 322, 50)
    StringGadget(1, 8,  10, 306, 20, "" + PlayerName)
    
    
    AddKeyboardShortcut(0,#PB_Shortcut_Return,1000)
    Repeat
      
      ;ExamineKeyboard()
      
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Break
        Case #PB_Event_Menu
          Select EventMenu()
            Case 1000
              If GetActiveGadget()=1
                PlayerName = GetGadgetText(1)
                Debug PlayerName
              EndIf
              
          EndSelect
      EndSelect
      
    ForEver
    
  EndIf  
EndIf
ExamineKeyboard is used for...let's call it "game inputs". Keystrokes in gadgets are better handled with Keyboardshortcuts.
Thank you, I'll look and this and trt to learn from it. I'm not real good with Select/ EndSelect or If/ElseIf stuff yet
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
Post Reply