GetKey

Started by Nicola, December 01, 2023, 09:10:46 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Nicola

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

Nicola

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

Charles Pegge

Hi Nicola,

It's a bit sticky so I need to do some  more work on the buffering.

Charles Pegge

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

Nicola

#4
Hi Charles
It looks like all characters are capitalized

Charles Pegge

Yes, these are virtual keyboard codes, not ascii char codes. Almost every key has its own code, not sensitive to ctrl or shift.

Nicola

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.

Charles Pegge

#7
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]

Nicola

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

Nicola

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