Blitz3D+ Command Reference

WaitKey ( )

Parameters

None.

Description

Halts the program until a character key is pressed, then returns its ASCII code.

This is the "press any key to continue" command. It suits a splash screen, a title card, a level-complete message or a quick tool - anywhere nothing needs to move while you wait.

The big gotcha is right there in the name: it blocks. Your loop stops, nothing animates, no Flip happens, and any drawing you queued on the back buffer is still invisible. Draw and Flip the screen you want the player to look at first, then call WaitKey. Internally it polls about fifty times a second and keeps the window responsive, and if the player closes the window while it is waiting the program simply ends.

Only keys that produce a character end the wait. Shift, Ctrl, Alt and the function keys are skipped, so "press any key" really means "press any typing key". The value you get back is the same character code GetKey would give, so you can use it to pick between options on a menu screen.

In a real game loop you almost never want this. Use KeyHit inside your While/Wend so the screen keeps refreshing, timers keep running and music keeps playing while you wait for the player.

See also: GetKey, KeyHit, FlushKeys, WaitMouse, Input.

Example

; WaitKey Example
; ---------------

Graphics 640,480,0,2
SetBuffer BackBuffer()

; Load the player ship sprite for the launch scene
ship=LoadImage("media/player.bmp")

; Draw the title frame FIRST, then halt - the player needs to see
; the prompt while the program is blocked inside WaitKey.
Cls
Text 320,180,"*  S T A R   P A T R O L  *",True
Text 320,220,"Press any key to launch",True
Flip

; WaitKey halts the program until a key is pressed, then returns
; the key's ASCII code.
key=WaitKey()

y=480

While Not KeyDown(1)

    Cls

    ; The launched ship climbs and wraps forever
    y=y-3
    If y<-36 Then y=480
    DrawImage ship,304,y

    Text 0,0,"Esc: exit"
    Text 0,20,"WaitKey() returned ASCII code "+key
    If key>=32 And key<=126 Then Text 0,40,"That is the character '"+Chr$(key)+"'"

    Flip

Wend

End

Index