Inkey() and KeyboardInkey()

Just starting out? Need help? Post your questions and find answers here.
jak64
Enthusiast
Enthusiast
Posts: 502
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Inkey() and KeyboardInkey()

Post by jak64 »

Hello,
In the first program (Console), the character is displayed even if you keep your finger pressed on the key.

Code: Select all

  If OpenConsole()
    PrintN("Appuyez sur [Echap] pour quitter")
    Repeat
      KeyPressed$ = Inkey()     
      PrintN(KeyPressed$)     
      Delay(100)
    Until KeyPressed$ = Chr(27) 
  EndIf
In the second program (OpenScreen), the character is displayed ONLY WHEN YOU RELEASE the key !!!

Code: Select all

  If InitSprite() And InitKeyboard() And OpenScreen(800,600,32,"")
    Repeat
      FlipBuffers()
      ClearScreen(RGB(100,100,100))
      ExamineKeyboard() 
      resultat$ + KeyboardInkey()
      If StartDrawing(ScreenOutput())
        DrawingMode(1)
        FrontColor(RGB(200, 200, 200))
        DrawText(20, 20, resultat$)
        StopDrawing()
      EndIf
    Until KeyboardPushed(#PB_Key_Escape)
  EndIf
Why ???
I would like, in the second case, that the character is displayed even if I keep my finger pressed on the key.

Is there a solution ?

thank you
TassyJim
Enthusiast
Enthusiast
Posts: 151
Joined: Sun Jun 16, 2013 6:27 am
Location: Tasmania (Australia)

Re: Inkey() and KeyboardInkey()

Post by TassyJim »

Your second program works for me.
Windows 11 and PB6 both backends
jak64
Enthusiast
Enthusiast
Posts: 502
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Inkey() and KeyboardInkey()

Post by jak64 »

I think I explained myself badly.

The second program works BUT the character entered is only displayed when you release the button!!!

Press on a letter and keep your finger pressed on it, for example 3 seconds.

It is only when you release the key (so after 3 seconds) that the character is displayed.

In the first program, the character is displayed immediately even if you keep your finger pressed on the key.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Inkey() and KeyboardInkey()

Post by BarryG »

The help specifically says Inkey() doesn't halt the program flow. It's by design.
jak64
Enthusiast
Enthusiast
Posts: 502
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Inkey() and KeyboardInkey()

Post by jak64 »

Hello BarryG,

I understand that but I would like to be able to have the equivalent for my game programs that use OpenWindowedScreen.

In this case, using InitKeyboard(), the program continues to run but I only retrieve the character I typed when I release the key or I would like the equivalent of Inkey(), i.e. immediately retrieve the typed character even if I haven't released the key.

This is the subject of my post because I calculate the reaction time of the player in milliseconds.
Jeromyal
Enthusiast
Enthusiast
Posts: 204
Joined: Wed Jul 17, 2013 8:49 am

Re: Inkey() and KeyboardInkey()

Post by Jeromyal »

How about this?

Code: Select all

  If InitSprite() And InitKeyboard() And OpenScreen(800,600,32,"")
    OpenConsole()
    Repeat
      FlipBuffers()
      ClearScreen(RGB(100,100,100))
      resultat$ + Inkey()
      esc = RawKey()
      If StartDrawing(ScreenOutput())
        DrawingMode(1)
        FrontColor(RGB(200, 200, 200))
        DrawText(20, 20, resultat$)
        StopDrawing()
      EndIf
    Until esc = 27
  EndIf
Perhaps not ideal
jak64
Enthusiast
Enthusiast
Posts: 502
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Inkey() and KeyboardInkey()

Post by jak64 »

Hello Jeromyal,

Indeed it responds to what I expect but the problem is that the console is displayed!

Is there no way to hide it?
Jeromyal
Enthusiast
Enthusiast
Posts: 204
Joined: Wed Jul 17, 2013 8:49 am

Re: Inkey() and KeyboardInkey()

Post by Jeromyal »

jak64 wrote: Fri Aug 05, 2022 10:12 am Hello Jeromyal,

Indeed it responds to what I expect but the problem is that the console is displayed!

Is there no way to hide it?
That is strange. with me it remained hidden while the screen was taken over.
Is this a windows applications that you are writing? You could use some api tricks like making the console a borderless transparent window.
If this has to be cross platform I am not sure how to go about it as of yet.
Jeromyal
Enthusiast
Enthusiast
Posts: 204
Joined: Wed Jul 17, 2013 8:49 am

Re: Inkey() and KeyboardInkey()

Post by Jeromyal »

Also to me it seems to work exactly as you describe that you want unless user clicks the mouse.
then the console looses focus of the keyboard. Rendering it useless for the screen. :(
Jeromyal
Enthusiast
Enthusiast
Posts: 204
Joined: Wed Jul 17, 2013 8:49 am

Re: Inkey() and KeyboardInkey()

Post by Jeromyal »

Perhaps a fullscreen window using a canvas gadget or opengl gadget would yield different keyboard handlers.
jak64
Enthusiast
Enthusiast
Posts: 502
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Inkey() and KeyboardInkey()

Post by jak64 »

Maybe but I don't know...
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Inkey() and KeyboardInkey()

Post by NicTheQuick »

Why don't you want to use KeyboardPushed()? Because it does not return the given character directly?
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
jak64
Enthusiast
Enthusiast
Posts: 502
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Inkey() and KeyboardInkey()

Post by jak64 »

Absolutely, because I measure reaction times.

I wrote this small test program with Console and Inkey()

Code: Select all

If OpenConsole()
    Delay(1000 + Random(2000))
    PrintN("press the letter q")
    debut=ElapsedMilliseconds()
    Repeat
      keypressed$=Inkey()
      If keypressed$ ="q"
        fin=ElapsedMilliseconds()
        finish=#True
      EndIf 
    Until KeyPressed$ = "q"
    total$="Reaction time=" + StrD(fin-debut)
    PrintN( total$)
    PrintN("[Enter] to quit")
    Input()
  EndIf
And I wrote this second small program to test with KeyboardInkey()

Code: Select all

If InitSprite() And InitKeyboard() And OpenScreen(800,600,32,"")
  cpt.w=100
  att.w=50+Random(100,0)
    Repeat
      FlipBuffers()
      ClearScreen(RGB(100,100,100))
      att-1
      If att<0
        If Not deb
          deb=#True
          debut=ElapsedMilliseconds()
        EndIf
        att=-1
        If StartDrawing(ScreenOutput())
          DrawingMode(1)
          FrontColor(RGB(255, 255, 255))
          DrawText(20, 20, "press the letter q")
          StopDrawing()
        EndIf
      EndIf
      ExamineKeyboard() 
      resultat$ = KeyboardInkey()
      If resultat$="q"
        fin=ElapsedMilliseconds()
        finish=#True
      EndIf
      If finish
        cpt-1
        total$="Reaction time=" + StrD(fin-debut)
        If StartDrawing(ScreenOutput())
          DrawingMode(1)
          FrontColor(RGB(200, 200, 200))
          DrawText(20, 40, total$)
          StopDrawing()
        EndIf
      EndIf
    Until cpt<0
  EndIf
I have found that in general the measured reaction time is lower when using Inkey().
This is probably due to the time (even minimal) to release the key.
That's why I'm looking for the equivalent of Inkey() in a Screen screen
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Inkey() and KeyboardInkey()

Post by BarryG »

How many keys do you need to test? Because this works if you only test specific keys:

Code: Select all

If InitSprite() And InitKeyboard() And OpenScreen(800,600,32,"")
  Repeat
    ExamineKeyboard()
    If KeyboardPushed(#PB_Key_W) : key$="w"
    ElseIf KeyboardPushed(#PB_Key_A) : key$="a"
    ElseIf KeyboardPushed(#PB_Key_S) : key$="s"
    ElseIf KeyboardPushed(#PB_Key_D) : key$="d"
    Else : key$ = ""
    EndIf
    If key$ <> ""
      resultat$ + key$
      FlipBuffers()
      ClearScreen(RGB(100,100,100))
      If StartDrawing(ScreenOutput())
        DrawingMode(1)
        FrontColor(RGB(200, 200, 200))
        DrawText(20, 20, resultat$)
        StopDrawing()
      EndIf
    EndIf
  Until KeyboardPushed(#PB_Key_Escape)
EndIf
Or to just test any key without caring what it is:

Code: Select all

If InitSprite() And InitKeyboard() And OpenScreen(800,600,32,"")
  Repeat
    ExamineKeyboard()
    If KeyboardPushed(#PB_Key_Escape) : done=1
    ElseIf KeyboardPushed(#PB_All) : key$="K"
    Else : key$ = ""
    EndIf
    If key$ <> ""
      resultat$ + key$
      FlipBuffers()
      ClearScreen(RGB(100,100,100))
      If StartDrawing(ScreenOutput())
        DrawingMode(1)
        FrontColor(RGB(200, 200, 200))
        DrawText(20, 20, resultat$)
        StopDrawing()
      EndIf
    EndIf
  Until done
EndIf
jak64
Enthusiast
Enthusiast
Posts: 502
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Inkey() and KeyboardInkey()

Post by jak64 »

That's actually what I wanted, thank you.

I don't understand why with KeyboardInkey() it doesn't work (like in the second program).

Thanks again, I made the modifications in the game program I am currently writing and I find that the reaction times are much better with KeyboardPushed() than with KeyboardInkey() which I used before.

Again a big thank you
Post Reply