Inkey() and KeyboardInkey()

Just starting out? Need help? Post your questions and find answers here.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Inkey() and KeyboardInkey()

Post by Mijikai »

I dont really understand the problem...
but you would need to use the OS api or a custom function.
On Windows you could for example subclass the window or directly listen to the window key messages via PeekMessage_().
Iirc. you can also obtain those messages through Wait/WindowEvent() but it is not officially supported.
Then there is GetKeyboardState_() which gives you the complete state of the keyboard, map the key you are interested in an see if its pressed or not.
The example you posted with the console would also work with KeyboardPushed() because you already know what key you want to time.
jak64
Enthusiast
Enthusiast
Posts: 502
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Inkey() and KeyboardInkey()

Post by jak64 »

Thank you Mijikai for all this information...

It's just that I don't understand why KeyboardInkey() waits for the key to be released to return the value while Inkey() returns the value immediately whether the key is released or not...
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Inkey() and KeyboardInkey()

Post by BarryG »

jak64 wrote: Fri Aug 05, 2022 12:57 pmI don't understand why KeyboardInkey()waits for the key to be released
Well, it doesn't actually... run the below code and you can see the program changing the text color rapidly, and when you press Esc you get a debug output of the keys that were held down, showing them multiple times while they were held down.

So the program flow continues regardless of using KeyboardInkey(), as you can see by the color still toggling while a key is being held down.

Code: Select all

If InitSprite() And InitKeyboard() And OpenScreen(800,600,32,"")
  Repeat
    FlipBuffers()
    ClearScreen(RGB(100,100,100))
    ExamineKeyboard() 
    resultat$ + KeyboardInkey()
    Debug resultat$
    If StartDrawing(ScreenOutput())
      DrawingMode(1)
      t=1-t
      If t=0
        c=#Red
      Else
        c=#Green
      EndIf
      FrontColor(c)
      DrawText(20, 20, resultat$)
      StopDrawing()
    EndIf
  Until KeyboardPushed(#PB_Key_Escape)
EndIf
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 tested the program and I don't see what you are saying...

In this small program, the debug window only appears when I release the a key

Try and keep your finger pressed for a few seconds. The debug window will only appear when you release your finger... That's my question.

Code: Select all

If InitSprite() And InitKeyboard() And OpenScreen(800,600,32,"")
  Repeat
    FlipBuffers()
    ClearScreen(RGB(100,100,100))
    ExamineKeyboard() 
    resultat$ + KeyboardInkey()
    If resultat$="a"
      Debug ("OK")
    EndIf
  Until KeyboardPushed(#PB_Key_Escape)
EndIf
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Inkey() and KeyboardInkey()

Post by Mijikai »

Either wait for the mapping KeyInput() facilitates or query the keys states directly.
In order to directly see if a char is pressed you need to map the char to a key scan code.
jak64
Enthusiast
Enthusiast
Posts: 502
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Inkey() and KeyboardInkey()

Post by jak64 »

My original question was why is there a difference between Inkey() and keyboardInkey()
In the first case, the result is displayed even if the finger is pressed.
In the second case, the result is only displayed WHEN THE finger is removed from the key.
It was just that.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Inkey() and KeyboardInkey()

Post by netmaestro »

You are mixing apples and oranges here. InKey() is a command you use in a console and it takes a keypress in. KeyboardInKey() is used after InitKeyboard(), ExamineKeyboard(), and KeyboardPushed() (or -released()) with a screen or windowed screen open. With a game I expect that's the one you want. However, they do not work the same. InKey() in a console takes a keypress in. KeyboardInKey() in a screen does not take a keypress in. All it does is remember the last key pressed. Use KeyboardPushed() or KeyboardReleased() to get the character in. Then you can use the KeyboardInKey() command to see what it was and then use it in some way. But remember this, calling it empties it out so you must assign it to a string variable. The help topic for KeyBoardInKey() has example code which shows this quite clearly. Check it out. It should clear up any confusion.

Edit: It occurs to me that a better name for KeyboardInKey() might be KeyboardLastKey(), it's more descriptive of what it does. Just an idea.
BERESHEIT
jak64
Enthusiast
Enthusiast
Posts: 502
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Inkey() and KeyboardInkey()

Post by jak64 »

Hello netmaster,

Thank you for your answer.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Inkey() and KeyboardInkey()

Post by BarryG »

jak64 wrote: Sat Aug 13, 2022 12:02 pmThe debug window will only appear when you release your finger
I can't see that because the screen blocks it.
Post Reply