export waitmouse
export waitkey
export waitjoy

DECLARE IMPORT, GetStdHandle(num as int),int
DECLARE IMPORT, FlushConsoleInputBuffer(handle as int),int


SUB waitkey(wkey as int)
' routine waits for any key to be pressed
int handle,keyw
string keyc

'clears out any existing key in console key buffer
handle = GetStdHandle(-10)
FlushConsoleInputBuffer(handle)
DO:UNTIL INKEY$ = ""

DO
wait 1
UNTIL GETKEYSTATE(wkey) = 0 

if (wkey = 0)
	keyc = ""
	keyw = 0x0D
ELSE
	keyc = chr$(wkey)
	keyw = wkey
endif
 
DO
wait 1
' in Windows, wait for the Return key ..
UNTIL INKEY$ <> keyc AND GETKEYSTATE(keyw) <> 0

RETURN
ENDSUB


SUB waitmouse
' routine waits for any mouse button to be pressed
DO:UNTIL GETKEYSTATE(1)=0
DO:UNTIL GETKEYSTATE(2)=0
DO:UNTIL GETKEYSTATE(4)=0
DO
WAIT 1
UNTIL GETKEYSTATE(1) or GETKEYSTATE(2) or GETKEYSTATE(4)
RETURN
ENDSUB


SUB waitjoy
' routine waits for a joystick button to be pressed
' UDT for joystick ..
type joyinfo 
  def Xpos:int 
  def Ypos:int 
  def Zpos:int 
  def Button:int 
endtype 
def joy:joyinfo 

declare "winmm",joyGetPos(id:int,n:joyinfo),int 

do
r = joyGetPos(0,joy)
wait 1
until (joy.Button > 0)

RETURN
ENDSUB