input function when working in a window... problems ->hel

Everything else that doesn't fall into one of the other PB categories.
bernardfrancois
User
User
Posts: 47
Joined: Tue Sep 02, 2003 9:17 am
Location: Belgium
Contact:

input function when working in a window... problems ->hel

Post by bernardfrancois »

Hi,

I'm working at an input function analog to input() in the console but then for a window.
The function should return the entered value but it doesn't seem to work (I even don't see the typing of the input).

Anyone who sees the error in this?

Code: Select all

Procedure windowinput(window,x,y,question$)
  
  ;lower case  
    AddKeyboardShortcut(window,#PB_Shortcut_A,-1)
    AddKeyboardShortcut(window,#PB_Shortcut_B,-2)
    AddKeyboardShortcut(window,#PB_Shortcut_C,-3)
    AddKeyboardShortcut(window,#PB_Shortcut_D,-4)
    AddKeyboardShortcut(window,#PB_Shortcut_E,-5)
    AddKeyboardShortcut(window,#PB_Shortcut_F,-6)
    AddKeyboardShortcut(window,#PB_Shortcut_G,-7)
    AddKeyboardShortcut(window,#PB_Shortcut_H,-8)
    AddKeyboardShortcut(window,#PB_Shortcut_I,-9)
    AddKeyboardShortcut(window,#PB_Shortcut_J,-10)
    AddKeyboardShortcut(window,#PB_Shortcut_K,-11)
    AddKeyboardShortcut(window,#PB_Shortcut_L,-12)
    AddKeyboardShortcut(window,#PB_Shortcut_M,-13)
    AddKeyboardShortcut(window,#PB_Shortcut_N,-14)
    AddKeyboardShortcut(window,#PB_Shortcut_O,-15)
    AddKeyboardShortcut(window,#PB_Shortcut_P,-16)
    AddKeyboardShortcut(window,#PB_Shortcut_Q,-17)
    AddKeyboardShortcut(window,#PB_Shortcut_R,-18)
    AddKeyboardShortcut(window,#PB_Shortcut_S,-19)
    AddKeyboardShortcut(window,#PB_Shortcut_T,-20)
    AddKeyboardShortcut(window,#PB_Shortcut_U,-21)
    AddKeyboardShortcut(window,#PB_Shortcut_V,-22)
    AddKeyboardShortcut(window,#PB_Shortcut_W,-23)
    AddKeyboardShortcut(window,#PB_Shortcut_X,-24)
    AddKeyboardShortcut(window,#PB_Shortcut_Y,-25)
    AddKeyboardShortcut(window,#PB_Shortcut_Z,-26)
    
  ;upper case
    AddKeyboardShortcut(window,#PB_Shortcut_A|#PB_Shortcut_Shift,-101)
    AddKeyboardShortcut(window,#PB_Shortcut_B|#PB_Shortcut_Shift,-102)
    AddKeyboardShortcut(window,#PB_Shortcut_C|#PB_Shortcut_Shift,-103)
    AddKeyboardShortcut(window,#PB_Shortcut_D|#PB_Shortcut_Shift,-104)
    AddKeyboardShortcut(window,#PB_Shortcut_E|#PB_Shortcut_Shift,-105)
    AddKeyboardShortcut(window,#PB_Shortcut_F|#PB_Shortcut_Shift,-106)
    AddKeyboardShortcut(window,#PB_Shortcut_G|#PB_Shortcut_Shift,-107)
    AddKeyboardShortcut(window,#PB_Shortcut_H|#PB_Shortcut_Shift,-108)
    AddKeyboardShortcut(window,#PB_Shortcut_I|#PB_Shortcut_Shift,-09)
    AddKeyboardShortcut(window,#PB_Shortcut_J|#PB_Shortcut_Shift,-110)
    AddKeyboardShortcut(window,#PB_Shortcut_K|#PB_Shortcut_Shift,-111)
    AddKeyboardShortcut(window,#PB_Shortcut_L|#PB_Shortcut_Shift,-112)
    AddKeyboardShortcut(window,#PB_Shortcut_M|#PB_Shortcut_Shift,-113)
    AddKeyboardShortcut(window,#PB_Shortcut_N|#PB_Shortcut_Shift,-114)
    AddKeyboardShortcut(window,#PB_Shortcut_O|#PB_Shortcut_Shift,-115)
    AddKeyboardShortcut(window,#PB_Shortcut_P|#PB_Shortcut_Shift,-116)
    AddKeyboardShortcut(window,#PB_Shortcut_Q|#PB_Shortcut_Shift,-117)
    AddKeyboardShortcut(window,#PB_Shortcut_R|#PB_Shortcut_Shift,-118)
    AddKeyboardShortcut(window,#PB_Shortcut_S|#PB_Shortcut_Shift,-119)
    AddKeyboardShortcut(window,#PB_Shortcut_T|#PB_Shortcut_Shift,-120)
    AddKeyboardShortcut(window,#PB_Shortcut_U|#PB_Shortcut_Shift,-121)
    AddKeyboardShortcut(window,#PB_Shortcut_V|#PB_Shortcut_Shift,-122)
    AddKeyboardShortcut(window,#PB_Shortcut_W|#PB_Shortcut_Shift,-123)
    AddKeyboardShortcut(window,#PB_Shortcut_X|#PB_Shortcut_Shift,-124)
    AddKeyboardShortcut(window,#PB_Shortcut_Y|#PB_Shortcut_Shift,-125)
    AddKeyboardShortcut(window,#PB_Shortcut_Z|#PB_Shortcut_Shift,-126)

  ;figures  
    AddKeyboardShortcut(window,#PB_Shortcut_0,-1000) 
    AddKeyboardShortcut(window,#PB_Shortcut_1,-2000) 
    AddKeyboardShortcut(window,#PB_Shortcut_2,-3000) 
    AddKeyboardShortcut(window,#PB_Shortcut_3,-4000) 
    AddKeyboardShortcut(window,#PB_Shortcut_4,-5000) 
    AddKeyboardShortcut(window,#PB_Shortcut_5,-6000) 
    AddKeyboardShortcut(window,#PB_Shortcut_6,-7000)
    AddKeyboardShortcut(window,#PB_Shortcut_7,-8000) 
    AddKeyboardShortcut(window,#PB_Shortcut_8,-9000) 
    AddKeyboardShortcut(window,#PB_Shortcut_9,-10000)
    
  ;backspace
    AddKeyboardShortcut(window,#PB_Shortcut_Clear,-200)
    
  ;return
    AddKeyboardShortcut(window,#PB_Shortcut_Return,-50)

  string$=""
  
  Repeat

    ;visualisation  
    StartDrawing(ImageOutput()) 
      frontcolor=RGB(255,255,255)
      Locate(x,y) : DrawText(question$ + "  " + string$ )
    StopDrawing() 
   
    StartDrawing(WindowOutput())
      DrawImage(ImageID(),0,0)
    StopDrawing()
    
    WindowEvent()
    a=EventMenuID()
    a$=""
    
    ;lower case detection
      ;a-z = 97-122
        ascicode=97
        For t=-1 To -26 Step -1
          If a=t : a$=Chr(ascicode) : EndIf
          ascicode=ascicode+1
        Next t
        
    ;upper case detection
      ;A-Z = 65-90 
        ascicode=65
        For t=-101 To -126 Step -1
          If a=t : a$=Chr(ascicode) : EndIf          
          ascicode=ascicode+1  
        Next t      
      
    ;figures detection  
      ;0-9 = 48-57
        ascicode=48
        For t=-1000 To -10000 Step -1000
          If a=t : a$=Chr(ascicode) : EndIf          
          ascicode=ascicode+1  
        Next t    
    
    string$=string$+a$

    ;backspace detection  
    If a=-200 : string$=Left(string$,(Len(string$)-1)) : EndIf
   
    ;Delay(30) 
  
  Until(a=-50)
  
             
  ProcedureReturn string$
  
EndProcedure

merendo
Enthusiast
Enthusiast
Posts: 449
Joined: Sat Apr 26, 2003 7:24 pm
Location: Germany
Contact:

Post by merendo »

Seems to be your negative values U use for the shortcuts. At least i tried to it with one letter with a value above 0 and it worked.

Some other hints: As you use WindowEvent() you should put at least one Delay in the main loop (or at least don't comment it) otherwise your app will be VERY CPU hungry.

And i recognized that you try to draw something into an image which does not exist - you will have to create it first :D

Hope that helps... Ask if you got a question!
The truth is never confined to a single number - especially scientific truth!
bernardfrancois
User
User
Posts: 47
Joined: Tue Sep 02, 2003 9:17 am
Location: Belgium
Contact:

Post by bernardfrancois »

thanks, I'll try it with positive values...
I already created the image, this wasn't my full code :)
the image is created at the beginning of the code...


[edit]

I tried the positive values and at least, something happens now.
But when I once push a key, that letter is repeated all the time (even when I stopped pushing the key...

I just put the code in a bit of it's context so you can see it yourself in purebasic...

Here it is... (just copy and paste it into purebasic and you can test it)

[/edit]

Code: Select all

Procedure sync()
  StartDrawing(WindowOutput())
  DrawImage(ImageID(),0,0)
  StopDrawing()
EndProcedure

Procedure windowinput(window,x,y,question$)
  
  a=0
  
  ;lower case  
    AddKeyboardShortcut(window,#PB_Shortcut_A,1)
    AddKeyboardShortcut(window,#PB_Shortcut_B,2)
    AddKeyboardShortcut(window,#PB_Shortcut_C,3)
    AddKeyboardShortcut(window,#PB_Shortcut_D,4)
    AddKeyboardShortcut(window,#PB_Shortcut_E,5)
    AddKeyboardShortcut(window,#PB_Shortcut_F,6)
    AddKeyboardShortcut(window,#PB_Shortcut_G,7)
    AddKeyboardShortcut(window,#PB_Shortcut_H,8)
    AddKeyboardShortcut(window,#PB_Shortcut_I,9)
    AddKeyboardShortcut(window,#PB_Shortcut_J,10)
    AddKeyboardShortcut(window,#PB_Shortcut_K,11)
    AddKeyboardShortcut(window,#PB_Shortcut_L,12)
    AddKeyboardShortcut(window,#PB_Shortcut_M,13)
    AddKeyboardShortcut(window,#PB_Shortcut_N,14)
    AddKeyboardShortcut(window,#PB_Shortcut_O,15)
    AddKeyboardShortcut(window,#PB_Shortcut_P,16)
    AddKeyboardShortcut(window,#PB_Shortcut_Q,17)
    AddKeyboardShortcut(window,#PB_Shortcut_R,18)
    AddKeyboardShortcut(window,#PB_Shortcut_S,19)
    AddKeyboardShortcut(window,#PB_Shortcut_T,20)
    AddKeyboardShortcut(window,#PB_Shortcut_U,21)
    AddKeyboardShortcut(window,#PB_Shortcut_V,22)
    AddKeyboardShortcut(window,#PB_Shortcut_W,23)
    AddKeyboardShortcut(window,#PB_Shortcut_X,24)
    AddKeyboardShortcut(window,#PB_Shortcut_Y,25)
    AddKeyboardShortcut(window,#PB_Shortcut_Z,26)
    
  ;upper case
    AddKeyboardShortcut(window,#PB_Shortcut_A|#PB_Shortcut_Shift,101)
    AddKeyboardShortcut(window,#PB_Shortcut_B|#PB_Shortcut_Shift,102)
    AddKeyboardShortcut(window,#PB_Shortcut_C|#PB_Shortcut_Shift,103)
    AddKeyboardShortcut(window,#PB_Shortcut_D|#PB_Shortcut_Shift,104)
    AddKeyboardShortcut(window,#PB_Shortcut_E|#PB_Shortcut_Shift,105)
    AddKeyboardShortcut(window,#PB_Shortcut_F|#PB_Shortcut_Shift,106)
    AddKeyboardShortcut(window,#PB_Shortcut_G|#PB_Shortcut_Shift,107)
    AddKeyboardShortcut(window,#PB_Shortcut_H|#PB_Shortcut_Shift,108)
    AddKeyboardShortcut(window,#PB_Shortcut_I|#PB_Shortcut_Shift,109)
    AddKeyboardShortcut(window,#PB_Shortcut_J|#PB_Shortcut_Shift,110)
    AddKeyboardShortcut(window,#PB_Shortcut_K|#PB_Shortcut_Shift,111)
    AddKeyboardShortcut(window,#PB_Shortcut_L|#PB_Shortcut_Shift,112)
    AddKeyboardShortcut(window,#PB_Shortcut_M|#PB_Shortcut_Shift,113)
    AddKeyboardShortcut(window,#PB_Shortcut_N|#PB_Shortcut_Shift,114)
    AddKeyboardShortcut(window,#PB_Shortcut_O|#PB_Shortcut_Shift,115)
    AddKeyboardShortcut(window,#PB_Shortcut_P|#PB_Shortcut_Shift,116)
    AddKeyboardShortcut(window,#PB_Shortcut_Q|#PB_Shortcut_Shift,117)
    AddKeyboardShortcut(window,#PB_Shortcut_R|#PB_Shortcut_Shift,118)
    AddKeyboardShortcut(window,#PB_Shortcut_S|#PB_Shortcut_Shift,119)
    AddKeyboardShortcut(window,#PB_Shortcut_T|#PB_Shortcut_Shift,120)
    AddKeyboardShortcut(window,#PB_Shortcut_U|#PB_Shortcut_Shift,121)
    AddKeyboardShortcut(window,#PB_Shortcut_V|#PB_Shortcut_Shift,122)
    AddKeyboardShortcut(window,#PB_Shortcut_W|#PB_Shortcut_Shift,123)
    AddKeyboardShortcut(window,#PB_Shortcut_X|#PB_Shortcut_Shift,124)
    AddKeyboardShortcut(window,#PB_Shortcut_Y|#PB_Shortcut_Shift,125)
    AddKeyboardShortcut(window,#PB_Shortcut_Z|#PB_Shortcut_Shift,126)

  ;figures  
    AddKeyboardShortcut(window,#PB_Shortcut_0,1000) 
    AddKeyboardShortcut(window,#PB_Shortcut_1,2000) 
    AddKeyboardShortcut(window,#PB_Shortcut_2,3000) 
    AddKeyboardShortcut(window,#PB_Shortcut_3,4000) 
    AddKeyboardShortcut(window,#PB_Shortcut_4,5000) 
    AddKeyboardShortcut(window,#PB_Shortcut_5,6000) 
    AddKeyboardShortcut(window,#PB_Shortcut_6,7000)
    AddKeyboardShortcut(window,#PB_Shortcut_7,8000) 
    AddKeyboardShortcut(window,#PB_Shortcut_8,9000) 
    AddKeyboardShortcut(window,#PB_Shortcut_9,10000)
    
  ;backspace
    AddKeyboardShortcut(window,#PB_Shortcut_Clear,200)
    
  ;return
    AddKeyboardShortcut(window,#PB_Shortcut_Return,50)

  string$=""
  
  Repeat

    ;visualisation  
    StartDrawing(ImageOutput()) 
      frontcolor=RGB(255,255,255)
      Locate(x,y) : DrawText(question$ + "  " + string$ )
    StopDrawing()  
      
    StartDrawing(WindowOutput())
      DrawImage(ImageID(),0,0)
    StopDrawing()
    
    WindowEvent()
    
    ;If EventMenuID()<>0
    ;  geloopt=1
    ;Else
    
    a=0
    a=EventMenuID()
    a$=""
    
    
    ;lower case detection
      ;a-z = 97-122
        ascicode=97
        For t=1 To 26
          If a=t : a$=Chr(ascicode) : EndIf
          ascicode=ascicode+1
        Next t
        
    ;upper case detection
      ;A-Z = 65-90 
        ascicode=65
        For t=101 To 126
          If a=t : a$=Chr(ascicode) : EndIf          
          ascicode=ascicode+1  
        Next t      
      
    ;figures detection  
      ;0-9 = 48-57
        ascicode=48
        For t=1000 To 10000 Step 1000
          If a=t : a$=Chr(ascicode) : EndIf          
          ascicode=ascicode+1  
        Next t    
    
    string$=string$+a$

    ;backspace detection  
    If a=200 : string$=Left(string$,(Len(string$)-1)) : EndIf
   
    Delay(30)
    
  
  Until(a=50)
  
             
  ProcedureReturn string$
  
EndProcedure




















CreateImage(0,800,600)

OpenWindow(0,100,100,800,600,#PB_Window_BorderLess,"4opnrij")

AddKeyboardShortcut(0,#PB_Shortcut_Return,200)
AddKeyboardShortcut(0,#PB_Shortcut_F10,201)

;INITIALISATIE
  
  StartDrawing(ImageOutput()) : DrawingMode(1)          
    FrontColor(0,0,0)
    Box(0,0,800,600)
    
    FrontColor(255,255,255)
    
    a=0
    For b=0 To 5
      LineXY(50,50+a,750,50+a)
      a=a+100
    Next b
    
    a=0
    For b=0 To 7
      LineXY(50+a,50,50+a,550)
      a=a+100
    Next b

    FrontColor(255,255,255)
    
    For t=1 To 7
      Locate(t*100,25) : DrawText(Str(t))
    Next t

    Dim tabel(8,6)         
  StopDrawing() 

  
  ;Repeat
        
    sync()
    
    Delay(200)
    
    ; eigelijk moet ik deze code enkel laten werken wanneer het window werd geactiveerd
    
  ;ForEver  
  
  

  Dim tabel(8,6)

  speler=1
  beurt=0

;SPELER

  speler:
;situatie indien bord vol is
  beurt=beurt+1
  If beurt=36 : Goto einde : EndIf
;merkpunt om verder tegaan indien ingave foutief of kolom vol
  opnieuw:
;hier overschrijf ik de vorige spelersregel en stel ik de kleur in
  StartDrawing(ImageOutput())
    FrontColor(40,40,40)
    Box(22,573,227-22,592-573)
  StopDrawing()
  
  sync()
  
  
  If speler=1
    ;FrontColor(200,0,0)
    ;Locate(25,575)
    ;speler1kort$=Left(speler1$,8)
    ;input speler1kort$;", geef kolom in: ",kolom

                                                               a=0
                                                               windowinput(0,20,500,speler1$+", geef kolom in: ")
                                                               kolom$=string$

  EndIf
  
  einde:
  
  End
  
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Post by Danilo »

@bernardfrancois:
If its for windows only, try this simple sample:

Code: Select all

Global InputString$

#InputString_Length = 20

Procedure UpdateString(char)
  If char > 31 And char < 128
    InputString$ + Chr(char)
  ElseIf char = 8  ; BACKSPACE
    If InputString$
      Inputstring$ = Left(InputString$,Len(InputString$)-1)
    EndIf
  ElseIf char = 13 ; Return
    MessageRequester("INFO","Input String:"+Chr(13)+InputString$)
    ProcedureReturn 0
  EndIf
  InputString$ = Left(InputString$,#InputString_Length) ; limit length
  UseImage(1)
  StartDrawing( ImageOutput() )
    Box(0,0,300,300,GetSysColor_(#COLOR_BTNFACE));RGB($13,$66,$D3))
    TextColor = GetSysColor_(#COLOR_BTNTEXT)
    FrontColor(Red(TextColor),Green(TextColor),Blue(TextColor))
    DrawingMode(1)
    Locate(10,10)
    SetCaretPos_(10+TextLength(InputString$),10)
    DrawText(InputString$)
  StopDrawing()
  SetGadgetState(0,UseImage(1))
EndProcedure

Procedure WinProc(hWnd,Msg,wParam,lParam)
  result = #PB_ProcessPureBasicEvents
  Select Msg
    Case #WM_CHAR
      UpdateString(wParam)
  EndSelect
  ProcedureReturn result
EndProcedure

CreateImage(1,300,300)

OpenWindow(0,0,0,300,300,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"Input")
  CreateGadgetList(WindowID())
  ImageGadget(0,0,0,300,300,UseImage(1))
  SetWindowCallback(@WinProc())
  
  ; optional caret
  CreateCaret_(WindowID(),0,2,16):ShowCaret_(WindowID())

  ; draw it
  UpdateString(0)

Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
Dont draw on the window directly, use an ImageGadget instead.

The ImageGadget is automatically updated, so no need to repaint
it 100 times a second or when the window was moved out of screen.
Just update/redraw your image and refresh the ImageGadget
with SetGadgetState(#ImageGadget,UseImage(#Image)).
cya,
...Danilo
...:-=< http://codedan.net/work >=-:...
-= FaceBook.com/DaniloKrahn =-
bernardfrancois
User
User
Posts: 47
Joined: Tue Sep 02, 2003 9:17 am
Location: Belgium
Contact:

Post by bernardfrancois »

thanks for the imagegadget tip

I've read your code and I can't find the line in which you detect the keyboard input :s
Where is it?

and does anyone see the error in my code?
Berikco
Administrator
Administrator
Posts: 1326
Joined: Wed Apr 23, 2003 7:57 pm
Location: Belgium
Contact:

Post by Berikco »

The code is working with a callback and a message if a key is pressed.

Code: Select all

Procedure WinProc(hWnd,Msg,wParam,lParam) 
  result = #PB_ProcessPureBasicEvents 
  Select Msg 
    Case #WM_CHAR <<<<<<<<< this is the keyboard message
      UpdateString(wParam) 
  EndSelect 
  ProcedureReturn result 
EndProcedure 
bernardfrancois
User
User
Posts: 47
Joined: Tue Sep 02, 2003 9:17 am
Location: Belgium
Contact:

Post by bernardfrancois »

danilo, I just tried to use an imagegadget instead, but it doesn't seem to work properly

when I move a popped-up consolewindow on top of it, the imagegadget doesn't refresh

just check this code to see what I mean (other people can also reply ofcourse! :))

Code: Select all

CreateImage(0,800,600)

OpenWindow(0,100,100,800,600,#PB_Window_BorderLess,"4opnrij")
  CreateGadgetList(WindowID())
    ImageGadget(0,0,0,800,600,0)
    SetGadgetState(0,ImageID())


  StartDrawing(ImageOutput())
    FrontColor(255,255,255)
    
    For t=0 To 9
      Box(t*50,t*50,40,40,RGB(4*t,20*t,5*t))
    Next t
    Box(22,573,227-22,592-573)
  StopDrawing()

  SetGadgetState(0,UseImage(0))

  Delay(1000)
    
    OpenConsole()
      PrintN("just move this console window to see the ImageGadget doesn't update automaticly")
      PrintN("press enter to quit") : kolom$=Input() 
      ClearConsole()
      Print("thanks for testing! if you can help my, then post some reply! :-)")
      Delay(500)
    CloseConsole()
    
End
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: input function when working in a window... problems ->

Post by PB »

What's wrong with a StringGadget for a window?
Or the InputRequester command?
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Post by Danilo »

bernardfrancois wrote:danilo, I just tried to use an imagegadget instead, but it doesn't
seem to work properly

when I move a popped-up consolewindow on top of it, the imagegadget doesn't refresh
It works fine in ImageGadget, but you forgot the event-loop
in your code.
Windows always need an event-handler (WindowEvent() or WaitWindowEvent()).
Windows (the OS) sends messages to the event-handler and tells the
window to refresh if needed. WindowEvent() and WaitWindowEvent()
catch this messages from the message queue.
If you dont add an event-handler the window doesnt get any message
(and the message queue gets full because you dont check events).

You could put the console stuff in a thread:

Code: Select all

Global close

Procedure ShowConsole(x)
  Delay(1000) 
    
    OpenConsole() 
      PrintN("just move this console window to see the ImageGadget doesn't update automaticly") 
      PrintN("press enter to quit") : kolom$=Input() 
      ClearConsole() 
      Print("thanks for testing! if you can help my, then post some reply! :-)") 
      Delay(500) 
    CloseConsole()
    
    close = #TRUE

EndProcedure

CreateImage(0,800,600) 

OpenWindow(0,100,100,800,600,#PB_Window_BorderLess,"4opnrij") 
  CreateGadgetList(WindowID()) 
    ImageGadget(0,0,0,800,600,0) 
    SetGadgetState(0,ImageID()) 


  StartDrawing(ImageOutput()) 
    FrontColor(255,255,255) 
    
    For t=0 To 9 
      Box(t*50,t*50,40,40,RGB(4*t,20*t,5*t)) 
    Next t 
    Box(22,573,227-22,592-573) 
  StopDrawing() 

  SetGadgetState(0,UseImage(0))

  CreateThread(@ShowConsole(),0)

  Repeat
    Repeat
      Event=WindowEvent()
      If Event=#PB_Event_CloseWindow
        close=#TRUE
      ElseIf Event=0
        Delay(10)
      EndIf
    Until Event=0
  Until close
    
End
Or you could use your own input method. You use Input() in your
code, but that stops the whole app until return got pressed.

I wrote my own WaitKey() functions for console, using Inkey().
1 is a simple WaitKey() that returns when a key was pressed,
the second is a WaitKey that limits chars you can input:

Code: Select all

;
; by Danilo, 03.12.2003
;
Procedure.l WaitKey()
  ; waits until the user presses a key
  Repeat
    asc = Asc(Inkey())
    If asc > 0 And asc < 127
      key = asc
    EndIf
    Delay(10)
  Until key
  ProcedureReturn key
EndProcedure

Procedure.l WaitKey2(string$)
  ; waits until the users presses a key
  ; specified in string$
  Repeat
    asc = Asc(Inkey())
    If asc > 0 And asc < 127
      If FindString(string$,Chr(asc),1)
        key = asc
      EndIf
    EndIf
    Delay(10)
  Until key
  ProcedureReturn key
EndProcedure


OpenConsole()

PrintN(""):Print("Input? ")

For a = 1 To 10 ; get 10 chars with WaitChar
  key = WaitKey()
  If key=13
    Break
  ElseIf key > 31
    Print(Chr(key))
  EndIf
Next a

PrintN(""):Print("Number? ")

For a = 1 To 10 ; input a number
  key = WaitKey2("0123456789."+Chr(13))
  If key=13 ; return
    Break
  EndIf
  Print(Chr(key))
Next a
Now you could use the WaitKey2() and simply add a WindowEvent()
to it:

Code: Select all

Procedure.l WaitKey2(string$)
  ; waits until the users presses a key
  ; specified in string$
  Repeat
    asc = Asc(Inkey())
    If asc > 0 And asc < 127
      If FindString(string$,Chr(asc),1)
        key = asc
      EndIf
    EndIf
    Delay(10)

    WindowEvent() ; <- do window events, ONLY FOR bernardfrancois
                  ;    dont use without a window

  Until key
  ProcedureReturn key
EndProcedure


CreateImage(0,800,600) 

OpenWindow(0,100,100,800,600,#PB_Window_BorderLess,"4opnrij") 
  CreateGadgetList(WindowID()) 
    ImageGadget(0,0,0,800,600,0) 
    SetGadgetState(0,ImageID()) 


  StartDrawing(ImageOutput()) 
    FrontColor(255,255,255) 
    
    For t=0 To 9 
      Box(t*50,t*50,40,40,RGB(4*t,20*t,5*t)) 
    Next t 
    Box(22,573,227-22,592-573) 
  StopDrawing() 

  SetGadgetState(0,UseImage(0))

  Delay(1000) 
    
  OpenConsole() 
    PrintN("just move this console window to see the ImageGadget doesn't update automaticly") 
    PrintN("press enter to quit")

    WaitKey2(Chr(13)) ; wait for return

    ClearConsole() 
    Print("thanks for testing! if you can help my, then post some reply! :-)") 
    Delay(500) 
  CloseConsole()

    
End
So the ImageGadget is still updated while waiting for return key.

The question about flickering is asked frequently, we had it in
german forum yesterday.
Another example that shows everything is fine -by using the
ImageGadget- came out:

Code: Select all

;
; original code and idea by buggymonkey, 23.01.2004
;
; changed by Danilo, 23.01.2004
;   - use ImageGadget instead drawing on the main window
;   - use a windows timer
;
Procedure.f DSin(angle_in_degree.f)
  ; returns Sinus of 'angle in degree
  ProcedureReturn Sin(angle_in_degree*0.01745329)
EndProcedure

Procedure.f DCos(angle_in_degree.f)
  ; returns CoSinus of 'angle in degree
  ProcedureReturn Cos(angle_in_degree*0.01745329)
EndProcedure

Procedure Lin(hDC,x,y,x1,y1,width,Color)
  ; by einander, english forum
  pen=createpen_(#PS_Solid,width,Color)  ; You can also change the style with #Ps_dash, #Ps_dot, #Ps_dashdotdot, but only when the pen width equals 1.
  hPenOld=SelectObject_(hDC,pen) 
  MoveToEx_(hDC,x,y,0):LineTo_(hDC,x1,y1) 
  DeleteObject_(SelectObject_(hDC,hPenOld))
EndProcedure 


Procedure UpdateDisplay()
  date   = Date()
  second = Second(date)
  minute = Minute(date)
  sec.f  = (180-second*6)
  min.f  = (180-minute*6);-second/10
  std.f  = (180-Hour(date)*30)-minute/2

  hDC = StartDrawing(ImageOutput())
  If hDC
    Circle(125,125,95,$999999)
    Lin(hDC,125,125,125+DSin(std)*70,125+DCos(std)*70,10,0) ; hours
    Lin(hDC,125,125,125+DSin(min)*80,125+DCos(min)*80, 4,0) ; minutes
    LineXY( 125,125,125+DSin(sec)*90,125+DCos(sec)*90,   0) ; seconds
    StopDrawing()
  EndIf

  SetGadgetState(1,UseImage(1))

EndProcedure 


Procedure InitImage()
  img = CreateImage(1,250,250)
  If img = 0
    MessageRequester("ERROR","Cant create image!",#MB_ICONERROR):End
  EndIf

  If StartDrawing(ImageOutput())
    Box(0,0,250,250,GetSysColor_(#COLOR_BTNFACE))
    Circle(125,125,100,$999999)
    ForeGround = GetSysColor_(#COLOR_BTNTEXT)
    FrontColor(Red(ForeGround),Green(ForeGround),Blue(ForeGround))
    DrawingMode(1)
    DrawingFont(LoadFont(1,"Arial",14))
    Locate(125-TextLength("12")/2,4)
    DrawText("12")
    Locate(10,115)
    DrawText("9")
    Locate(230,115)
    DrawText("3")
    Locate(125-TextLength("6")/2,225)
    DrawText("6")
    For a = 0 To 360 Step 6
      sin.f = DSin(a)
      cos.f = DCos(a)
      Plot(125+sin*99,125+cos*99,0)
      If a % 10 = 0
        For b = 2 To 0 Step -1
          Plot(125+sin*(98-b),125+cos*(98-b),0)
        Next b
      EndIf
    Next a
    StopDrawing()
  EndIf
  
  ProcedureReturn img
EndProcedure


If OpenWindow(1,400,400,270,270,#PB_Window_SystemMenu,"Uhr")
  CreateGadgetList(WindowID())
  ImageGadget(1,10,10,250,250,InitImage())

  UpdateDisplay()

  SetTimer_(WindowID(),0,1000,0) ; LowRes Timer with 1000 milliseconds (1s)

  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = #TRUE
      Case #WM_TIMER
        UpdateDisplay()
    EndSelect
  Until Quit
  
  KillTimer_(WindowID(),0)

EndIf
And a simple animation:

Code: Select all

;
; by Danilo, 08.01.2004
;
Procedure.f DSin(angle_in_degree.f)
  ; returns Sinus of 'angle in degree
  ProcedureReturn Sin(angle_in_degree*0.01745329)
EndProcedure

Procedure.f DCos(angle_in_degree.f)
  ; returns CoSinus of 'angle in degree
  ProcedureReturn Cos(angle_in_degree*0.01745329)
EndProcedure

Global Angle.f

Procedure UpdateDisplay()
  UseImage(1)
  StartDrawing(ImageOutput())
    Box(0,0,200,120,RGB($0A,$94,$C6))
    LineXY(100-Dsin(Angle)*50,60-Dcos(Angle)*50,100-Dsin(Angle)*50,60+Dcos(Angle)*50,$FFFFFF)
    LineXY(100+Dsin(Angle)*50,60+Dcos(Angle)*50,100-Dsin(Angle)*50,60+Dcos(Angle)*50,$FFFFFF)
  StopDrawing()
  Angle + 5
  SetGadgetState(2,UseImage(1))
EndProcedure

If CreateImage(1,200,120)=0
  MessageRequester("ERROR","Cant create image!",#MB_ICONERROR):End
EndIf

OpenWindow(0,0,0,200,140,#PB_Window_ScreenCentered|#WS_POPUP,"Player")
  CreateGadgetList(WindowID())
  ButtonGadget(1,0,0,100,20,"eXit")
  ImageGadget(2,0,20,200,120,UseImage(1)):UpdateDisplay()

SetTimer_(WindowID(),0,20,0) ; LowRes Timer with 20 milliseconds

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      End
    Case #PB_Event_Gadget
      Select EventGadgetID()
        Case 1 ; eXit
          End
      EndSelect
    Case #WM_TIMER
      UpdateDisplay()
  EndSelect
ForEver
Hope that helps...
cya,
...Danilo
...:-=< http://codedan.net/work >=-:...
-= FaceBook.com/DaniloKrahn =-
bernardfrancois
User
User
Posts: 47
Joined: Tue Sep 02, 2003 9:17 am
Location: Belgium
Contact:

Post by bernardfrancois »

Thanks danilo!

I dind't know the windowevent() command could be used for that :). Until now I only used it to check pressed keys.

The CreateThread() command seems to be very usefull too. I'd never found that on my own, as in the help they say it's to run background processes, while that console window is in the foreground :) lol

It seems some lines in your first code were unnessesary (hmm is this word spelled well?) :

Code: Select all

  Repeat 
    ;Repeat 
      Event=WindowEvent() 
      ;If Event=#PB_Event_CloseWindow 
      ;  close=#TRUE 
      ;ElseIf Event=0 
        Delay(10) 
      ;EndIf 
    ;Until Event=0 
  Until close 
It still works fine when I have hided those.

Here's the full code so you don't have to do too much copying-pasting:

Code: Select all

Global close 

Procedure ShowConsole(x) 
  Delay(1000) 
    
    OpenConsole() 
      PrintN("just move this console window to see the ImageGadget doesn't update automaticly") 
      PrintN("press enter to quit") : kolom$=Input() 
      ClearConsole() 
      Print("thanks for testing! if you can help my, then post some reply! :-)") 
      Delay(500) 
    CloseConsole() 
    
    close = #TRUE 

EndProcedure 

CreateImage(0,800,600) 

OpenWindow(0,100,100,800,600,#PB_Window_BorderLess,"4opnrij") 
  CreateGadgetList(WindowID()) 
    ImageGadget(0,0,0,800,600,0) 
    SetGadgetState(0,ImageID()) 


  StartDrawing(ImageOutput()) 
    FrontColor(255,255,255) 
    
    For t=0 To 9 
      Box(t*50,t*50,40,40,RGB(4*t,20*t,5*t)) 
    Next t 
    Box(22,573,227-22,592-573) 
  StopDrawing() 

  ;SetGadgetState(0,UseImage(0)) 
  SetGadgetState(0,ImageID()) 


  CreateThread(@ShowConsole(),0) 

  Repeat 
    ;Repeat 
      Event=WindowEvent() 
      ;If Event=#PB_Event_CloseWindow 
      ;  close=#TRUE 
      ;ElseIf Event=0 
        Delay(10) 
      ;EndIf 
    ;Until Event=0 
  Until close 
    
End





PB: I'm trying to make an exact remake of a game I wrote in Darkbasic. There were no such stringadgets in it...
Or is it possible to hide the frame and background of such a stringagdet, and change the font?
Then it would be great.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> is it possible to hide the frame and background of such a stringagdet,
> and change the font?

You can change the font and border like the following, but I'm not 100%
sure how to change the background colour (someone else should know):

Code: Select all

If OpenWindow(0,200,200,300,200,#PB_Window_SystemMenu,"test")
  CreateGadgetList(WindowID())
  StringGadget(0,10,10,200,20,"",#PB_String_BorderLess)
  SetGadgetFont(0,LoadFont(0,"Courier New",10,#PB_Font_Bold|#PB_Font_Italic))
  ActivateGadget(0)
  Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Post by Danilo »

bernardfrancois wrote:It seems some lines in your first code were unnessesary

Code: Select all

  Repeat 
    ;Repeat 
      Event=WindowEvent() 
      ;If Event=#PB_Event_CloseWindow 
      ;  close=#TRUE 
      ;ElseIf Event=0 
        Delay(10) 
      ;EndIf 
    ;Until Event=0 
  Until close 
The lines you commented out are for closing the main window.
you dont have a titlebar with close button, but remember that
you can close the window with ALT-F4 if focus is on window.

I dont know what you want to do with a window+imagegadget
and open a console window over it, because thats not what
most people do.
If you use windows, you should always do everything with GUI
and not open a console for getting some user input only.
If you write console applications you use console only (or maybe
some requesters if really needed), but mixing both isnt very
common - and not needed for game programming.
Dont forget to look at the examples that come with PB.

If you want to write a game, you should look at the Screen/Sprite
commands, they are especially for game coding.
You open a screen and draw sprites, text and everything on this
screen. With the keyboard commands (keyboardpushed()) you
catch the pressed keys, add the char to a String and draw the
string with DrawText() onto your screen.
You can make a nice graphical interface for input with this stuff.
cya,
...Danilo
...:-=< http://codedan.net/work >=-:...
-= FaceBook.com/DaniloKrahn =-
bernardfrancois
User
User
Posts: 47
Joined: Tue Sep 02, 2003 9:17 am
Location: Belgium
Contact:

Post by bernardfrancois »

That I added the console for the input was just something temporary, afterward I would make an input function.

I learnt a lot converting my old darkbasic code to purebasic. Unfortunaly this darkbasic code isn't very good (I made it the first day I learnt programming :)) and has a lot of goto's.

I'll now try to make it working with the console for input... Afterwards I'll replace those pieces of code with an input function.

Here you can find the old version of the game. It's in dutch but I think (if you know the game) it will be playable.
http://users.telenet.be/bernardfrancois ... opnrij.zip
Post Reply