BRL.PolledInput: Functions Source  


Polled input

The polled input module provides an easy way to detect keyboard and mouse input.

The functions in this module are only available when your program is running in Graphics mode. When working on GUI applications, you should use events instead.

Functions

AppSuspendedGet app suspended state
AppTerminateReturn app terminate state
KeyHitCheck for key hit
KeyDownCheck for key state
GetCharGet next character
FlushKeysFlush key states and character queue.
MouseXGet mouse x location
MouseYGet mouse y location
MouseZGet mouse wheel
MouseXSpeedGet mouse x speed
MouseYSpeedGet mouse y speed
MouseZSpeedGet mouse z speed
FlushMouseFlush mouse button states
MouseHitCheck for mouse button click
MouseDownCheck for mouse button down state
WaitKeyWait for a key press
WaitCharWait for a key press
WaitMouseWait for mouse button click

Function reference

Function AppSuspended()
ReturnsTrue if application is currently suspended.
DescriptionGet app suspended state

Function AppTerminate()
ReturnsTrue if user has requested to terminate application
DescriptionReturn app terminate state
Example
Graphics 640,480,0

While Not AppTerminate() Or Not Confirm( "Terminate?" )

	Cls
	DrawText MouseX()+","+MouseY(),0,0
	Flip

Wend

Function KeyHit( key )
ReturnsNumber of times key has been hit.
DescriptionCheck for key hit
Information The returned value represents the number of the times key has been hit since the last call to KeyHit with the same key.

See the key codes module for a list of valid key codes.

Example
' keyhit.bmx

' the following code draws a circle every time the
' program detects the spacebar has been pressed
' and exits when it detects the ESCAPE key has
' been pressed

graphics 640,480
while not keyhit(KEY_ESCAPE)
	cls
	if keyhit(KEY_SPACE) drawoval 0,0,640,480
	flip
wend

Function KeyDown( key )
ReturnsTrue if key is currently down
DescriptionCheck for key state
Information See the key codes module for a list of valid keycodes.
Example
' keydown.bmx

' the following code draws a circle if the
' program detects the spacebar is pressed
' and exits when it detects the ESCAPE key has
' been pressed

Graphics 640,480
While Not KeyHit(KEY_ESCAPE)
	Cls
	If KeyDown(KEY_SPACE) DrawOval 0,0,640,480
	Flip
Wend

Function GetChar()
ReturnsThe character code of the next character.
DescriptionGet next character
Information As the user hits keys on the keyboard, BlitzMax records the character codes of these keystrokes into an internal 'character queue'.

GetChar removes the next character code from this queue and returns it the application.

If the character queue is empty, 0 is returned.

Function FlushKeys()
DescriptionFlush key states and character queue.
Information FlushKeys resets the state of all keys to 'off', and resets the character queue used by GetChar.

Function MouseX()
ReturnsMouse x axis location
DescriptionGet mouse x location
Information The returned value is relative to the left of the screen.
Example
' mousex.bmx

' the following tracks the position of the mouse

graphics 640,480
while not keyhit(KEY_ESCAPE)
	cls
	drawoval mousex()-10,mousey()-10,20,20
	flip
wend

Function MouseY()
ReturnsMouse y axis location
DescriptionGet mouse y location
Information The returned value is relative to the top of the screen.
Example
' mousey.bmx

' the following tracks the position of the mouse

graphics 640,480
while not keyhit(KEY_ESCAPE)
	cls
	drawrect mousex()-10,mousey()-10,20,20
	flip
wend

Function MouseZ()
ReturnsMouse wheel value
DescriptionGet mouse wheel
Information The mouse wheel value increments when the mouse wheel is rolled 'away' from the user, and decrements when the mouse wheel is rolled 'towards' the user.
Example
' mousez.bmx

' prints mousez() the mousewheel position

Graphics 640,480
While Not keyhit(KEY_ESCAPE)
	cls
	drawtext "MouseZ()="+MouseZ(),0,0
	flip
Wend

Function MouseXSpeed()
ReturnsMouse x speed
DescriptionGet mouse x speed

Function MouseYSpeed()
ReturnsMouse y speed
DescriptionGet mouse y speed

Function MouseZSpeed()
ReturnsMouse z speed
DescriptionGet mouse z speed

Function FlushMouse()
DescriptionFlush mouse button states
Information FlushMouse resets the state of all mouse buttons to 'off'.

Function MouseHit( button )
ReturnsNumber of times button has been clicked.
DescriptionCheck for mouse button click
Information The returned value represents the number of the times button has been clicked since the last call to MouseHit with the same button.

button should be 1 for the left mouse button, 2 for the right mouse button or 3 for the middle mouse button.

Example
' mousehit.bmx

graphics 640,480

while not keyhit(KEY_ESCAPE)
	cls
	if mousehit(1) drawrect 0,0,200,200
	if mousehit(2) drawrect 200,0,200,200
	if mousehit(3) drawrect 400,0,200,200
	flip
wend

Function MouseDown( button )
ReturnsTrue if button is currently down
DescriptionCheck for mouse button down state
Information button should be 1 for the left mouse button, 2 for the right mouse button or 3 for the middle mouse button.
Example
' mousedown.bmx

graphics 640,480

while not keyhit(KEY_ESCAPE)
	cls
	if mousedown(1) drawrect 0,0,200,200
	if mousedown(2) drawrect 200,0,200,200
	if mousedown(3) drawrect 400,0,200,200
	flip
wend

Function WaitKey()
ReturnsThe keycode of the pressed key
DescriptionWait for a key press
Information WaitKey suspends program execution until a key has been hit. The keycode of this key is then returned to the application.

See the key codes module for a list of valid keycodes.

Function WaitChar()
ReturnsThe character code of the pressed key
DescriptionWait for a key press
Information WaitChar suspends program execution until a character is available from GetChar. This character is then returned to the application.

Function WaitMouse()
ReturnsThe clicked button
DescriptionWait for mouse button click
Information WaitMouse suspends program execution until a mouse button is clicked.

WaitMouse returns 1 if the left mouse button was clicked, 2 if the right mouse button was clicked or 3 if the middle mouse button was clicked.