KEYBOARD AND MOUSE
Input
This function opens a console input to screen. Unlike other programming languages, console input operates on background and doesn't freeze the game until the input is accepted. Of course you can write your own "pause" code for input. You must maintain console input in the main loop until you want it to close. You MUST close the console input with the CloseInput-command.
Optionally, you can provide a password character which masks typed string with a character of your choosing (typically an asterix *). See the example for more details. Also note that Locate and AddText modifies the location where the console appears.
Usage:
myText$ = Input(message$ [,passChar$])
Example:
movex=1
movey=1
Repeat
'UPDATE CONSOLE
Color cbwhite
If console=OFF Then
Text 0,0,"Press SPACE to open console"
If KeyHit(cbkeyspace) Then
console=ON
Locate 10,30
ClearKeys
EndIf
Else
command$=Input("? ")
'to enable password field, comment the line above
'and uncomment the line below
'command$=Input("? ","*")
Text 0,0,"Close console by pressing RETURN"
If KeyHit(cbkeyreturn)
console=OFF
CloseInput
ClearKeys
EndIf
EndIf
'UPDATE "GAME"
Color cbred
Circle x-20,y-20,40
x=x+movex
y=y+movey
If x<0 Then movex= -movex
If y<0 Then movey= -movey
If x>400 Then movex= -movex
If y>300 Then movey= -movey
DrawScreen
Until EscapeKey()
CloseInput
Closes an open console input. See the Input-command example.
PositionMouse
Positions the mouse pointer to the given coordinates on screen. The coordinates are relative to window, not to desktop.
Usage:
PositionMouse, X, Y
ShowMouse
Shows, hides or customs the mouse pointer. The value of 0 (OFF) hides it while value of 1 (ON) shows it, and any other variable pointing to a loaded IMAGE changes the mouse pointer to that image. You can even make animated cursors this way (store images into an array). Please note that if you choose to mark an image pointer, the image permanently gets its handle moved to the middle. So X,Y is in the middle of the image at drawing.
Usage:
ShowMouse mode
Example:
AddText "F1 = hide cursor"
AddText "F2 = show cursor"
AddText "F3 = custom pointer"
AddText "F4 = custom target"
pointer=LoadImage("d:\cursor.bmp")
crosshair=LoadImage("d:\crosshair.bmp")
Repeat
If KeyHit(cbkeyf1) Then ShowMouse OFF
If KeyHit(cbkeyf2) Then ShowMouse ON
If KeyHit(cbkeyf3) Then ShowMouse pointer
If KeyHit(cbkeyf4) Then ShowMouse crosshair
DrawScreen
Until EscapeKey()
ClearKeys
Flush the keyboard buffer and zero all input. Handy for example just after loading when you want to reset the input before the game starts.
ClearMouse
See ClearKeys. The same thing goes for mouse.
SafeExit
CoolBasic allows you to quit the program if the Escape key is pressed. Any time. This prevents situations where you can't quit the program because you forgot to write the ending routine. However, you might want to turn this off if your game uses Escape, for example, to return menu. Command SAFEEXIT OFF at the beginning of your code.
Usage:
SafeExit ON/OFF
KeyDown
Returns True if the specified key is being held down. You need to know the SCANCODE of the requested key. See Language Reference/Constants/Key Constants for more details.
Usage:
state = KeyDown(SCANCODE)
KeyHit
Returns True if the specified key was just hit down. You need to know the SCANCODE of the requested key. See Language Reference/Constants/Key Constants for more details.
Usage:
state = KeyHit(SCANCODE)
KeyUp
Returns True if the specified key was just released. You need to know the SCANCODE of the requested key. See Language Reference/Constants/Key Constants for more details.
Usage:
state = KeyUp(SCANCODE)
GetKey
Returns the ASCII-code of key pressing (number 1-255). Zero if nothing is pressed.
Usage:
ASCII = GetKey()
Example:
stri$
= stri$ + Chr(GetKey())
WaitKey
Halts the program execution until a key is pressed. If used as function, it returns its SCANCODE.
Usage:
WaitKey
or
k = WaitKey()
MouseDown
Check is a certain mouse button is currently pressed down. True if it, zero of not.
Usage:
md = MouseDown(button)
MouseHit
Check if a click has just happened (mouse down). Returns True if is, zero if not
Usage:
mh = MouseHit(button)
MouseUp
Check if a click has just happened (mouse released). Returns True if is, zero if not
Usage:
mu = MouseUp(button)
GetMouse
Tells which mouse button is down. Returns 1=left, 2=right, 3=middle, 0 if none.
Usage:
mouse = GetMouse()
WaitMouse
Halts the program execution until a mouse button is pressed. If used as function, it returns the button code: 1=left, 2=right, 3=middle
Usage:
WaitKey
or
m = WaitKey()
MouseX, MouseY
Tell the mouse coordinates (pixels, relative to window):
Usage:
x = MouseX()
y = MouseY()
MouseWX, MouseWY
Tell the mouse coordinates (pixels, relative to game world):
Usage:
x = MouseWX()
y = MouseWY()
MouseMoveX, MouseMoveY
Tell the speed at which the mouse has been moved since last call.
Usage:
speedx = MouseMoveX()
speedy = MouseMoveY()
MouseZ
Tells the position of mouse wheel. At program start the position is zero. If moved upwards, the result is positive. If moved downwards, the result is negative.
Usage:
mz = MouseZ()
MouseMoveZ
Tells which direction mouse wheel has been rolled. Returns -1, 0 or 1.
Usage:
msz = MouseMoveZ()
LeftKey, RightKey, UpKey, DownKey
Return True if an arrow is pressed (being held down), otherwise zero.
Usage:
is = ***Key()
Example:
FrameLimit 40
hero = LoadObject("Media\guy.bmp", 72)
ClsColor cbWhite
Repeat
If LeftKey() Then TurnObject hero, 5
If RightKey() Then TurnObject hero, -5
If UpKey() Then MoveObject hero, 2
If DownKey() Then MoveObject hero, -2
DrawScreen
Forever
EscapeKey
Return True if the Escape is being held down, otherwise zero.
Usage:
isEsc = EscapeKey()