Interactive PowerBasic Forum

IT-Consultant: Charles Pegge => OxygenBasic => Topic started by: Nicola on December 01, 2023, 09:10:46 AM

Title: GetKey
Post by: Nicola on December 01, 2023, 09:10:46 AM
Hi Charles.
Could you make it so that getkey can also return the ESC key?

Cheers

uses console
string k
do
  k=getkey
  print k + "-"

  if k = "13" then break
loop

printl "end"

wait
Title: Re: GetKey
Post by: Nicola on December 01, 2023, 05:41:50 PM
Great Charles.
That's what I wanted to tell you, to vary console.inc,
Yes, I knew inkey reported this, but I preferred GetKey as it waits for a key press before continuing.

Currently, the getkey function in console.inc looks like this:
function GetKey() as int
========================
  static long blen,mode
  static byte z[16]
  GetConsoleMode ConsIn,mode
  SetConsoleMode ConsIn,0
  ReadConsole ConsIn,@z,1,@blen,null
  SetConsoleMode ConsIn,mode
  return z
end function
Title: Re: GetKey
Post by: Charles Pegge on December 01, 2023, 05:46:30 PM
Hi Nicola,

It's a bit sticky so I need to do some  more work on the buffering.
Title: Re: GetKey
Post by: Charles Pegge on December 01, 2023, 06:18:45 PM
Better!

/*
  function GetKey() as int
  ========================
  static long blen,mode
  static byte z[4]
  GetConsoleMode ConsIn,mode
  SetConsoleMode ConsIn,0
  ReadConsole ConsIn,@z,1,@blen,null
  SetConsoleMode ConsIn,mode
  return z
  end function
*/

  ! GetAsyncKeyState lib "user32.dll" (int c) as int
  '
  function inkey(int c=0) as int
  ================================
  static KEY_RECORD k[8]
  static int nr
  int d
  PeekConsoleInputA ConsIn,@k,8, @nr
  if nr
    if c=0
      indexbase 1
      int i
      for i=1 to nr
        if k[i].EventType=1 'KEY_EVENT
          if k[i].bKeyDown
            d=k[i].wVirtualKeyCode
            exit for
          endif
        endif
      next
    elseif GetAsyncKeyState(c)
      d=c
    endif
    FlushConsoleInputBuffer ConsIn
    return d   
  endif
  end function

  function GetKey(int c=0) as int
  ===============================
  int d
  do
    d=inkey(c)
    if d
      if c
        if d=c
          return c
        endif
      else 'c=0
        return d
      endif
    endif
  loop
  end function

This supports:

if inkey(27)
...

waitkey (27)

as well as inkey() and getkey()

You can use these functions in your code, but they will be incorporated into the next console.inc
Title: Re: GetKey
Post by: Nicola on December 01, 2023, 09:21:10 PM
Hi Charles
It looks like all characters are capitalized
Title: Re: GetKey
Post by: Charles Pegge on December 01, 2023, 11:09:26 PM
Yes, these are virtual keyboard codes, not ascii char codes. Almost every key has its own code, not sensitive to ctrl or shift.
Title: Re: GetKey
Post by: Nicola on December 02, 2023, 01:06:39 PM
Ok, but I need how the current GetKey works, which recognizes whether they are uppercase or lowercase. The only thing is that, like inkey, the ESC key code also returns.
Title: Re: GetKey
Post by: Charles Pegge on December 02, 2023, 04:30:18 PM
With a small mod the Ascii code (supporting esc) can be made available:

in console.inc
  static KEY_RECORD KeyRecord
  '
  function inkey(int c=0) as int
  ================================
  static KEY_RECORD k[8]
  static int nr
  int d
  PeekConsoleInputA ConsIn,@k,8, @nr
  if nr
    if c=0
      indexbase 1
      int i
      for i=1 to nr
        if k[i].EventType=1 'KEY_EVENT
          if k[i].bKeyDown
            KeyRecord=k[i]
            d=KeyRecord.wVirtualKeyCode
            exit for
          endif
        endif
      next
    elseif GetAsyncKeyState(c)
      d=c
    endif
    FlushConsoleInputBuffer ConsIn
    return d   
  endif
  end function

test code:
/*
  type KEY_RECORD
  ===============
    word EventType
    word wReserved
    dword bKeyDown
    word wRepeatCount
    word wVirtualKeyCode
    word wScanCode
    word wAsciiCode
    dword dwReserved
  end type
*/

uses console
string k
int a,c
do
  a=inkey
  if a
    c=KeyRecord.wAsciiCode
    k=a  ",  " c
    print k + cr
    if a=13 then break
  endif
loop

printl "end"

wait
[/cade]
Title: Re: GetKey
Post by: Nicola on December 04, 2023, 11:38:26 PM
function GetKey(int c=0) as int
  ===============================
  int d,t
  do
    t=inkey(c)
    if t
      d=KeyRecord.wAsciiCode
      if c
        if d=c
          return c
        endif
      else 'c=0
        return d
      endif
    endif
    sleep 10
  loop
  end function

Getkey in console.inc
Now it works well.
Thank you
Title: Re: GetKey
Post by: Nicola on December 04, 2023, 11:46:36 PM
I needed it to complete this small input 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

  do
    setpos x,y
    'print tst + ": " + yy + string(max-len(yy), "_")
    print tst + yy + string(max-len(yy), "_")
    k=getkey  '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
           yy=yy + chr(k)
         endif

     end select
   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