Page 1 of 1
Receive a string on a OpenScreen
Posted: Fri Feb 14, 2014 11:22 pm
by KilljoyHeathen
Hey Pure-world!
This is my virgin post lol. Anyways I am fairly new to programming, and just curious if my following code is the only way/correct way to recieve user input while in a fullscreen enviroment. The code is the KeyboardInkey() help code modified. The original did not allow for CAPS. It works, just seems clunky.
Code: Select all
Procedure.s StringIn(X,Y)
AddText.s = ""
AdjText.s = ""
FinText.s = ""
CapFlag.b = 0
Repeat
ClearScreen(RGB(0,0,0))
ExamineKeyboard()
If (KeyboardPushed(#PB_Key_LeftShift) And CapFlag = 0) Or (KeyboardPushed(#PB_Key_RightShift) And CapFlag = 0)
AddText + KeyboardInkey()
AdjText = UCase(AddText)
FinText + AdjText
CapFlag = 1
AddText = ""
AdjText = ""
Else
FinText + KeyboardInkey()
EndIf
CapFlag = 0
If KeyboardReleased(#PB_Key_Back)
FinText = Left(FinText, Len(FinText)-1)
EndIf
If StartDrawing(ScreenOutput())
DrawingMode(1)
FrontColor(RGB(255,255,255))
DrawText(X, Y+40, "Enter some text, press enter to exit.")
DrawText(X, Y, FinText)
StopDrawing()
EndIf
If KeyboardReleased(#PB_Key_Return)
ProcedureReturn FinText
Break
EndIf
FlipBuffers()
ForEver
EndProcedure
InitSprite()
InitKeyboard()
OpenScreen(800,600,32,"Yah String Input!!")
MyString.s = StringIn(100,100)
ClearScreen(RGB(0,0,0))
StartDrawing(ScreenOutput())
DrawText(400,300,MyString)
StopDrawing()
FlipBuffers()
Delay(1000)
End
Thanks

Re: Receive a string on a OpenScreen
Posted: Sat Feb 15, 2014 12:20 am
by VB6_to_PBx
need to replace
with something like this
Code: Select all
Until KeyboardPushed(#PB_Key_Escape) : End
Re: Receive a string on a OpenScreen
Posted: Sat Feb 15, 2014 12:25 am
by KilljoyHeathen
Well it is in a procedure to be included in a program, like entering name for high score. It needs to return a string and the loop break is when the user hits enter. I guess I should put that info in the code.
....There edited so it will display a message saying what to do and how to exit.
Re: Receive a string on a OpenScreen
Posted: Sat Feb 15, 2014 3:50 am
by BasicallyPure
Here is my offering, I even added a cursor.
Code: Select all
InitSprite()
InitKeyboard()
If OpenScreen(800,600,32,"Yah String Input!!")
Define MyString.s, key.s, cursor.s = "_"
Define cursorTimer.i = 30
Repeat
ClearScreen(0)
StartDrawing(ScreenOutput())
DrawText(0,0,"Press 'Esc' or 'Enter' to exit.")
ExamineKeyboard()
key = KeyboardInkey()
If key
Select Asc(key)
Case 13 ; Enter
Break
Case 8 ; Backspace
MyString = Left(MyString,Len(MyString)-1)
Default
; IMO I think KeyboardInkey() is buggy, we shouldn't have to do this.
If KeyboardPushed(#PB_Key_LeftShift) Or KeyboardPushed(#PB_Key_RightShift)
key = UCase(key)
Else
key = LCase(key)
EndIf
;
MyString + key
EndSelect
key = ""
EndIf
cursorTimer - 1
If cursorTimer = 0 : cursorTimer = 30
If cursor = "_" : cursor = " " : Else : cursor = "_" : EndIf
EndIf
DrawText(400-TextWidth(MyString)/2,300-TextHeight(MyString)/2,MyString+cursor)
StopDrawing()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
EndIf
Re: Receive a string on a OpenScreen
Posted: Sat Feb 15, 2014 4:18 am
by KilljoyHeathen
Awesome, thanks for the code, the cursor is handy. Your code, looks much more eloquent, will use as an example.
Re: Receive a string on a OpenScreen
Posted: Sat Feb 15, 2014 4:37 am
by BasicallyPure
Here it is arranged as a procedure which is more like you wanted I think.
Code: Select all
Procedure.S STRING_IN()
Static timeout.i = 30 ; cursor speed
Protected.s text, key, cursor = "_"
Protected.i cursorTimer = timeout
Repeat
ClearScreen(0)
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Escape) : End : EndIf
StartDrawing(ScreenOutput())
DrawText(0,0,"Type some text then press 'Enter' to return.")
key = KeyboardInkey()
If key
Select Asc(key)
Case 13 ; Enter
StopDrawing() : Break
Case 8 ; Backspace
text = Left(text,Len(text)-1)
Default
; IMO I think KeyboardInkey() is buggy, we shouldn't have to do this.
If KeyboardPushed(#PB_Key_LeftShift) Or KeyboardPushed(#PB_Key_RightShift)
key = UCase(key)
Else
key = LCase(key)
EndIf
;
text + key
EndSelect
key = ""
EndIf
cursorTimer - 1
If cursorTimer < 1 : cursorTimer = timeout
If cursor = "_" : cursor = " " : Else : cursor = "_" : EndIf
EndIf
DrawText(400-TextWidth(text)/2,300-TextHeight(text)/2,text+cursor)
StopDrawing()
FlipBuffers()
ForEver
ProcedureReturn text
EndProcedure
If InitSprite() And InitKeyboard() And OpenScreen(800,600,32,"Yah String Input!!")
Define MyString.s
Repeat ; main loop
ClearScreen(0)
StartDrawing(ScreenOutput())
DrawText(0,0,"Press 'Esc' to exit or 'F2' to input text.")
If MyString
DrawText(0,40,"You typed ---> " + MyString)
EndIf
StopDrawing()
FlipBuffers()
ExamineKeyboard()
If KeyboardPushed(#PB_Key_F2)
MyString = STRING_IN()
EndIf
Until KeyboardPushed(#PB_Key_Escape)
EndIf
Re: Receive a string on a OpenScreen
Posted: Sat Feb 15, 2014 5:10 am
by KilljoyHeathen
Thanks, again. The backspace and return keys don't work, is it the wrong keycode??....Guess not the table those codes. I am using Linux if that makes a difference? I will fool around with it, much appreciated.
Re: Receive a string on a OpenScreen
Posted: Sat Feb 15, 2014 9:18 am
by BasicallyPure
Yes, I was using windows.
KeyboardInkey() is even more buggy with Linux.
I switched to Linux and observed no output at all from KeyboardInkey() when I pressed backspace or enter.
You can drop in this procedure and it should make backspace and enter work for you.
Code: Select all
Procedure.S STRING_IN()
Static timeout.i = 10 ; cursor speed
Protected.s text, key, cursor = "_"
Protected.i cursorTimer = timeout
Repeat
ClearScreen(0)
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Escape) : End : EndIf
If KeyboardPushed(#PB_Key_Return) : Break : EndIf
If KeyboardReleased(#PB_Key_Back) : text = Left(text,Len(text)-1) : EndIf
StartDrawing(ScreenOutput())
DrawText(0,0,"Type some text then press 'Enter' to return.")
key = KeyboardInkey()
If key
; IMO I think KeyboardInkey() is buggy, we shouldn't have to do this.
If KeyboardPushed(#PB_Key_LeftShift) Or KeyboardPushed(#PB_Key_RightShift)
key = UCase(key)
Else
key = LCase(key)
EndIf
;
text + key
EndIf
cursorTimer - 1
If cursorTimer < 1 : cursorTimer = timeout
If cursor = "_" : cursor = " " : Else : cursor = "_" : EndIf
EndIf
DrawText(400-TextWidth(text)/2,300-TextHeight(text)/2,text+cursor)
StopDrawing()
FlipBuffers()
ForEver
ProcedureReturn text
EndProcedure
I was having trouble even with drawtext on my Linux system, it was mostly unreadable.
Here is a version using a windowed screen and it solves my text graphic problems.
If you use a windowed screen then you can use an InputRequester to input a line of text.
Simple.
Code: Select all
If InitSprite() And InitKeyboard() And
OpenWindow(0,100,100,800,600,"",#PB_Window_BorderLess|#PB_Window_ScreenCentered) And
OpenWindowedScreen(WindowID(0),0,0,800,600)
Define MyString.s
Repeat ; main loop
ClearScreen(0)
StartDrawing(ScreenOutput())
DrawText(0,0,"Press 'Esc' to exit or 'F2' to input text.")
If MyString
DrawText(0,40,"You typed ---> " + MyString)
EndIf
StopDrawing()
FlipBuffers()
ExamineKeyboard()
If KeyboardReleased(#PB_Key_F2)
MyString = InputRequester("","Enter some text","")
EndIf
; you need this if a window is opened
While WindowEvent() : Wend
Until KeyboardPushed(#PB_Key_Escape)
EndIf
Re: Receive a string on a OpenScreen
Posted: Sat Feb 15, 2014 9:29 am
by VB6_to_PBx
KilljoyHeathen
Joined: Fri Feb 14, 2014 4:56 pm
Posts: 4
i forgot to say "Welcome to PureBasic Forum"
Re: Receive a string on a OpenScreen
Posted: Sat Feb 15, 2014 5:13 pm
by KilljoyHeathen
Thanks VB! I hope to make a run with this and if things look good after a bit I am going to purchase the full version.
@BasicallyPure: I pretty much edited your procedure the same as what you got there to make it work, thats too bad about it being buggy. Are windowed screens quicker/slower/same as fullscreen? I have been looking at them and windows in general, there are a ton of cool features/gadgets to incorporate. For a basic language this is very robust and kind of daunting to myself, but will learn.
Re: Receive a string on a OpenScreen
Posted: Sat Feb 15, 2014 7:46 pm
by BasicallyPure
I don't know about speed but the advantage of a windowed screen is you can use all of the gadgets that would otherwise not be available with screen alone.
As in my example the InputRequester is only available if you have an open window.
The screen on a windowed screen does not have to cover the entire window.
You can leave space for gadgets like buttons and such beside the screen.
BP
Re: Receive a string on a OpenScreen
Posted: Sat Feb 15, 2014 8:13 pm
by KilljoyHeathen
Ok, I am convinced. I am definitley going with the windowed screens, PureBASIC is sooo cool! Off-Topic: I was searching the forums about the visual designer and came across the C.Boss post........wow what a read....I feel dumber now for putting myself through it. lol.