Managed Input Console

Started by Nicola, December 29, 2023, 04:46:18 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Nicola

A small example:

'La routine serve per tracciare delle "_" al posto
'delle lettere da inserire

'y e x corrispondono alle coordinate
'max è il valore della lunghezza massima della parola
'-----------------------------------------------------

use console

function infos(string tst="", int y, x, max) as string
  local string yy
  local int l, k, c

  do
    setpos x,y
    'print tst + ": " + yy + string(max-len(yy), "_")
    print tst + yy + string(max-len(yy), "_")
    k=getkey  'aspetta pressione tasto
    'k=inkey  'aspetta pressione tasto
    select k
      case 9, 13  'tab, enter
        if len(yy)> 0 then
          setpos x,y
          print tst + yy + string(max-len(yy), " ")
          return yy
        endif
     
      case 27    'tasto ESC permette di uscire senza scrivere niente
        return ""

      case 8  'del, cancella i caratteri inseriti
        if len(yy)> 0 then
          yy=left(yy,len(yy)-1)
        endif

      case 32 to 127
        if len(yy)< max then
          c=KeyRecord.wAsciiCode
          yy=yy + chr(c)
        endif

    end select
  'sleep 50  'da usare nel caso uso di inkey
  loop
end function

'--------------
'--- MAIN -----
string esempio=infos("Nome: ",3,5,10)
string esempio2=infos("Cognome: ",3,25,10)
printl
printl esempio " len= " len(esempio)
printl esempio2 " len= " len(esempio2)

wait

Charles Pegge


Using inkey to return mouse click events:

'test console position selection
uses console
do
  int k=inkey()
  if k=1 or k=2 'left or right click
    def mx MouseRecord.dwMousePosition.x
    def my MouseRecord.dwMousePosition.y
    print "mouse click " k tab mx tab my cr
  elseif k
    print "virtual key " k cr
  endif
  if k=27 or k=13 'esc or enter
    exit do
  endif
loop

Charles Pegge

Using an ad-hoc variadic definition of locate

uses console
'console location / color
def locate SetConsoleCursorPosition ConsOut,(%1)*0x10000+0 %3
cls
locate 5
color 0xe
print "y=5"
locate 5,10
color 0x1e
print "y=5,x=10"
locate 10
color 7
print "left click mouse to exit"
wait(1)

Charles Pegge

#3
To read user edited line:

'reading characters direct from console
uses sysutil
uses console
byte c  'ascii of byte read
int n=1 'requested chars to read
int r   'chars read
cls
print cr
'read fifth character on second line
int p=1*0x10000+4
print "0123456789" 'demo input
'string s=input 'user imput
ReadConsoleOutputCharacter consout,@c,n,p,@r
'WriteConsoleOutputCharacter consout,@c,n,p,@r
print cr "ascii " c "  char: " chr(c)
wait

Nicola

#4
Hi Charles, thank you.

An update with the ability to select alphanumeric/alpha-only/numeric-only input.

/*
01-01-2024 Nicola P.
La routine serve per tracciare delle "_" al posto
delle lettere da inserire

y e x corrispondono alle coordinate
max è il valore della lunghezza massima della parola
cs --> 0=alfanumerico, 1=alfa, 2=numerico
-----------------------------------------------------
*/

use console

macro addcar(yy)
  c=KeyRecord.wAsciiCode
  yy=yy + chr(c)
end macro

function infos(string tst="", int y, x, max,cs=0) as string
===========================================================
  local string yy
  local int l, k, c

  do
    setpos x,y
    'print tst + ": " + yy + string(max-len(yy), "_")
    print tst + yy + string(max-len(yy), "_")
    k=getkey  'aspetta pressione tasto
    'k=inkey  'aspetta pressione tasto
    select k
      case 9, 13  'tab, enter
        if len(yy)> 0 then
          setpos x,y
          print tst + yy + string(max-len(yy), " ")
          return yy
        endif
     
      case 27    'tasto ESC permette di uscire senza scrivere niente
        return ""

      case 8  'del, cancella i caratteri inseriti
        if len(yy)> 0 then
          yy=left(yy,len(yy)-1)
        endif

      case 32 to 127
        if len(yy)< max and cs=0 then addcar(yy)
        if k>45 and k<58 and cs=2 and len(yy)< max then addcar(yy)
        if cs=1 and len(yy)<max and instr("48,49,50,51,52,53,54,55,56,57",k)=0 then addcar(yy)


      case 188 to 190    '.
        if len(yy)< max and cs=0 then addcar(yy)
        if cs=2 and k=190 and len(yy)<max and instr(yy,".")=0 then addcar(yy)

   end select
  'sleep 50  'da usare nel caso uso di inkey
  loop
end function

'--------------
'--- MAIN -----
string esempio=infos("Name: ",3,5,10)
string esempio2=infos("Surname: ",3,25,10)
string eta=infos("Age: ",3,45,3,2)
string alfa=infos("alfa: ",4,45,6,1)
printl
printl esempio " len= " len(esempio)
printl esempio2 " len= " len(esempio2)
printl eta " len= " len(eta)
printl alfa " len= " len(alfa)

wait


Nicola

Quote from: Charles Pegge on January 05, 2024, 02:47:34 PMdef locate SetConsoleCursorPosition ConsOut,(%1)*0x10000+0 %3
very good.
Is it possible to insert the text color, background color in the def?

Nicola

Quote from: Charles Pegge on January 05, 2024, 05:30:53 PMReadConsoleOutputCharacter consout,@c,n,p,@r
Fine.
A very interesting feature.

Charles Pegge

Quote from: Nicola on January 05, 2024, 10:09:06 PM
Quote from: Charles Pegge on January 05, 2024, 02:47:34 PMdef locate SetConsoleCursorPosition ConsOut,(%1)*0x10000+0 %3
very good.
Is it possible to insert the text color, background color in the def?

Maybe, but we could also make color consistent with the locate macro:

  def Color   SetConsoleTextAttribute ConsOut, (%1) + (0 %3)*0x10
  color 7 'white with black background
  color 7,1 'white with blue background

Nicola

How about a macro like in the example?

locat(x,y,color1,color2)

uses console
'console location / color
def locate
  SetConsoleCursorPosition ConsOut,(%1)*0x10000+0 %3
  SetConsoleTextAttribute  ConsOut, (%4) + (0 %5)*0x10
end def

macro locat(a,b,c,d)
  SetConsoleCursorPosition ConsOut,(a)*0x10000+0 b
  SetConsoleTextAttribute  ConsOut, c + (0 d)*0x10
end macro
'def Color  SetConsoleTextAttribute  ConsOut, (%1) + (0 %3)*0x10
  'color 7 'white with black background
  'color 7,1 'white with blue background

cls
locat(5,0,7,6)
'locate 5 0 7 6

'color 0xe
print "y=5"
'locate 5 10 7 1
locat(5,10,7,1)
print "y=5,x=10"
'locate 10 0 7 1
locat(10,5,5,4)
int i
for i=0 to 20
  locat(10+i,i,10-i,i)
  print "left click mouse to exit"
next
wait(1)


Charles Pegge

Color and Locate are really separate concepts. Think about what makes the code easier to read and understand.

Nicola

We could do:
color txt,bkg
printat x,y,txt

use console

def Color  SetConsoleTextAttribute  ConsOut, (%1) + (0 %3)*0x10

macro printat(a,b,t)
  SetConsoleCursorPosition ConsOut,(a)*0x10000+0 b
  print t
end macro