Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Input without freezing
#1
This is what I have so far. I was wondering if 1) This is a bad way to do it, and 2) If there was a way to implement backspace better than it is here.
Thanks

Code:
#INCLUDE "fbgfx.bi"
Option Explicit

CONST True=1
CONST False=0

SCREENRES 640, 480, 16, 2, 0
Dim Work_Page As Integer
Work_Page = 0
      
Dim cmd As String

Do
     SCREENSET Work_Page, Work_Page Xor 1
     SCREENSYNC
     Cls
          
     Print cmd
     cmd = cmd + INKEY$
     If MULTIKEY(SC_BACKSPACE) Then cmd = Left$(cmd, Len(cmd) - 1)
    
     Work_Page = Work_Page Xor 1
Loop Until MULTIKEY(SC_ESCAPE)
Reply
#2
It depends on the context you'll be using this in. You've grasped the concent of building an input string one character at a time, which is good. When you move on to implement such a scheme in an actual interface you'll come upon other ideas for a more sophiticated input routine.

A few comments:
You may have noticed that using the MULTIKEY function to check for a backspace results in a very touchy back space operation. The INKEY$ function returns a character for the backspace key, chr$(8), as well as for many other key strokes.

Also, your code spends a lot of time clearing the screen and rewriting your string while it waits for the user to press a key. This consumes proccessor time. Here's a quick edit of your code demonstrating an different method.
Code:
#INCLUDE "fbgfx.bi"
Option Explicit

CONST True=1
CONST False=0

SCREENRES 640, 480, 16, 2, 0
Dim Work_Page As Integer
Dim keyhit$
Work_Page = 0
      
Dim cmd As String
SCREENSET Work_Page, Work_Page Xor 1
SCREENSYNC
cls    
Print "_";  
'Just a visual cue.

Do
     Do  
'This inner loop allow us to wait for a key
         keyhit$=inkey$        
         sleep 50    
     Loop while keyhit$=""
    
     select case keyhit$  
'These case block could be implemented with
'IF blocks if that's your taste.

     case chr$(27) 'Escape
         exit do          
     case chr$(13) 'Enter
         exit do
     case chr$(8)  'Back space
         if len(cmd)>0 then    'Check the string
                               'length first.
             cmd=left$(cmd,Len(cmd)-1)
         end if
     case else
         cmd = cmd + keyhit$
     end select
     SCREENSYNC
     cls    
     Print cmd ;"_"
     Work_Page = Work_Page Xor 1
     SCREENSET Work_Page, Work_Page Xor 1    
Loop

This isn't by any means the best way, because that will depend upon the rest of your program.
Reply
#3
This is cool RyanKelly. I forgot that inkey$ would do that. Thanks again for the help. Smile
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)