Using Mouse Inputs
Introductory Mouse Input functionality guide
Written by: Various Authors
Last updated: July 2024
Mouse Button Functionality
Here are some functions that are used to handle mouse button inputs. These functions all use SplashKit’s Mouse Button data type for the argument and check if the specified button has been clicked, held down, or released.
List of Mouse Button Enum Values
Mouse Button is an enumeration used by SplashKit to read inputs from the mouse. Understanding how to handle mouse buttons is crucial for developers who want users to control their programs using the mouse.
Description | Enum Value in Code |
---|---|
No mouse button | NO_BUTTON |
The left mouse button | LEFT_BUTTON |
The middle mouse button | MIDDLE_BUTTON |
The right mouse button | RIGHT_BUTTON |
The x1 mouse button | MOUSE_X1_BUTTON |
The x2 mouse button | MOUSE_X2_BUTTON |
Description | Enum Value in Code |
---|---|
No mouse button | MouseButton.NoButton |
The left mouse button | MouseButton.LeftButton |
The middle mouse button | MouseButton.MiddleButton |
The right mouse button | MouseButton.RightButton |
The x1 mouse button | MouseButton.MouseX1Button |
The x2 mouse button | MouseButton.MouseX2Button |
Description | Enum Value in Code |
---|---|
No mouse button | MouseButton.no_button |
The left mouse button | MouseButton.left_button |
The middle mouse button | MouseButton.middle_button |
The right mouse button | MouseButton.right_button |
The x1 mouse button | MouseButton.mouse_x1_button |
The x2 mouse button | MouseButton.mouse_x2_button |
Mouse Clicked
Mouse Clicked is a boolean function that checks if the specified Mouse Button
has been clicked since the last time Process Events was called.
Here is a simple example using the left and right mouse buttons:
#include "splashkit.h"
int main(){ open_window("Mouse Inputs", 800, 600);
while (!window_close_requested("Mouse Inputs")) { // process user input events process_events();
// change screen colour to red if left mouse button is clicked if (mouse_clicked(LEFT_BUTTON)) { clear_screen(COLOR_RED); } // change screen colour to yellow if right mouse button is clicked else if (mouse_clicked(RIGHT_BUTTON)) { clear_screen(COLOR_YELLOW); }
refresh_screen(60); }
close_all_windows();
return 0;}
using SplashKitSDK;using static SplashKitSDK.SplashKit;
OpenWindow("Mouse Inputs", 800, 600);
while (!WindowCloseRequested("Mouse Inputs")){ // process user input events ProcessEvents();
// change screen colour to red if left mouse button is clicked if (MouseClicked(MouseButton.LeftButton)) { ClearScreen(ColorRed()); } // change screen colour to yellow if right mouse button is clicked else if (MouseClicked(MouseButton.RightButton)) { ClearScreen(ColorYellow()); }
RefreshScreen(60);}
CloseAllWindows();
from splashkit import *COLOR_RED = color_red()COLOR_YELLOW = color_yellow()COLOR_WHITE = color_white()
def main():
win = open_window("Mouse Inputs", 800, 600)
current_color = COLOR_WHITE
# Main loop while not window_close_requested(win): # Process user input events process_events()
# Check mouse click status and change screen color if mouse_clicked(MouseButton.left_button):
current_color = COLOR_RED # Change color to red elif mouse_clicked(MouseButton.right_button):
current_color = COLOR_YELLOW # Change color to yellow
# Clear screen with the current color clear_screen(current_color)
refresh_window(win)
close_window(win)
if __name__ == "__main__": main()
Mouse Down
Mouse Down checks if the specified button is being held down.
Here is a simple example using the left and right mouse buttons:
#include "splashkit.h"
int main(){ open_window("Mouse Inputs", 800, 600);
while (!window_close_requested("Mouse Inputs")) { // process user input events process_events();
// change screen colour to red if left mouse button is held down if (mouse_down(LEFT_BUTTON)) { clear_screen(COLOR_RED); } // change screen colour to yellow if right mouse button is held down else if (mouse_down(RIGHT_BUTTON)) { clear_screen(COLOR_YELLOW); } // change screen colour to white if no mouse button held down else { clear_screen(COLOR_WHITE); } refresh_screen(60); }
close_all_windows();
return 0;}
using SplashKitSDK;using static SplashKitSDK.SplashKit;
OpenWindow("Mouse Inputs", 800, 600);
while (!WindowCloseRequested("Mouse Inputs")){ // process user input events ProcessEvents();
// change screen colour to red if left mouse button is held down if (MouseDown(MouseButton.LeftButton)) { ClearScreen(ColorRed()); } // change screen colour to yellow if right mouse button is held down else if (MouseDown(MouseButton.RightButton)) { ClearScreen(ColorYellow()); } // change screen colour to white if no mouse button held down else { ClearScreen(ColorWhite()); }
RefreshScreen(60);}
CloseAllWindows();
from splashkit import *
COLOR_RED = color_red()COLOR_YELLOW = color_yellow()COLOR_WHITE = color_white()
def main():
window = open_window("Mouse Inputs", 800, 600)
current_color = COLOR_WHITE
while not window_close_requested(window): # Process user input events process_events()
# Check mouse button status and change screen color if mouse_down(MouseButton.left_button):
current_color = COLOR_RED # Change color to red elif mouse_down(MouseButton.right_button):
current_color = COLOR_YELLOW # Change color to yellow else: current_color = COLOR_WHITE # Default color if no mouse button is held down
clear_screen(current_color)
refresh_window(window)
# Close the window close_window(window)
if __name__ == "__main__": main()
Mouse Up
Opposite to the previous function, Mouse Up checks if the specified button is released.
Here is a simple example using the left mouse button:
#include "splashkit.h"
int main(){ open_window("Mouse Inputs", 800, 600);
while (!window_close_requested("Mouse Inputs")) { // process user input events process_events();
// change screen colour to red if left mouse button is not pressed if (mouse_up(LEFT_BUTTON)) { clear_screen(COLOR_RED); } // change screen colour to white if left button is pressed else { clear_screen(COLOR_WHITE); }
refresh_screen(60); }
close_all_windows();
return 0;}
using SplashKitSDK;using static SplashKitSDK.SplashKit;
OpenWindow("Mouse Inputs", 800, 600);
while (!WindowCloseRequested("Mouse Inputs")){ // process user input events ProcessEvents();
// change screen colour to red if left mouse button is not pressed if (MouseUp(MouseButton.LeftButton)) { ClearScreen(ColorRed()); } // change screen colour to white if left button is pressed else { ClearScreen(ColorWhite()); }
RefreshScreen(60);}
CloseAllWindows();
from splashkit import *
COLOR_RED = color_red()COLOR_WHITE = color_white()
def main():
window = open_window("Mouse Inputs", 800, 600)
while not window_close_requested(window): # Process user input events process_events()
# Change screen color to red if the left mouse button is not pressed if not mouse_down(MouseButton.left_button): clear_screen(COLOR_RED) # Change screen color to white if the left button is pressed else: clear_screen(COLOR_WHITE)
refresh_window(window)
close_window(window)
if __name__ == "__main__": main()
Mouse Movement Functionality
Another thing we can do with mouse input, other than checking button presses, is to check mouse movement in the program.
Mouse Movement
Mouse Movement is a function that returns the amount of accumulated mouse movement since the last time Process Events
was called. It returns the value as a Vector 2D
data type, which shows the direction and distance in the x and y scale.
Here is an example using the left mouse button combined with horizontal movement to change the hue of the screen colour, and the right mouse button combined with vertical movement to change the saturation of the screen colour:
#include "splashkit.h"
int main(){ // colour variables double hue = 0.8; double saturation = 0.8; double brightness = 0.8;
// mouse movement variable vector_2d movement;
open_window("Mouse Movement", 800, 600);
while (!window_close_requested("Mouse Movement")) { // process user input events process_events();
// store the mouse movement direction and distance to the movement variable movement = mouse_movement();
// change screen colour 'hue' if left mouse button is held down if (mouse_down(LEFT_BUTTON)) { // change hue of screen colour based on horizontal mouse movement hue += movement.x / screen_width(); } // change screen colour 'saturation' if right mouse button is held down if (mouse_down(RIGHT_BUTTON)) { // change saturation of screen colour based on vertical mouse movement saturation += movement.y / screen_height(); }
// display current screen colour clear_screen(hsb_color(hue, saturation, brightness)); refresh_screen(60); }
close_all_windows();
return 0;}
using SplashKitSDK;using static SplashKitSDK.SplashKit;
// colour variablesdouble hue = 0.8;double saturation = 0.8;double brightness = 0.8;
// mouse movement variableVector2D movement;
OpenWindow("Mouse Movement", 800, 600);
while (!WindowCloseRequested("Mouse Movement")){ // process user input events ProcessEvents();
// store the mouse movement direction and distance to the movement variable movement = MouseMovement();
// change screen colour 'hue' if left mouse button is held down if (MouseDown(MouseButton.LeftButton)) { // change hue of screen colour based on horizontal mouse movement hue += movement.X / ScreenWidth(); } // change screen colour 'saturation' if right mouse button is held down if (MouseDown(MouseButton.RightButton)) { // change saturation of screen colour based on vertical mouse movement saturation += movement.Y / ScreenHeight(); }
// display current screen colour ClearScreen(HSBColor(hue, saturation, brightness)); RefreshScreen(60);}
CloseAllWindows();
from splashkit import *
def main():
hue = 0.8 saturation = 0.8 brightness = 0.8
window = open_window("Mouse Movement", 800, 600)
while not window_close_requested(window):
process_events()
# Get the mouse movement movement = mouse_movement()
# Update hue and saturation based on mouse movement if mouse_down(MouseButton.left_button): # Adjust hue based on horizontal mouse movement hue = (hue + movement.x / window_width(window)) % 1.0
if mouse_down(MouseButton.right_button): # Adjust saturation based on vertical mouse movement saturation = min(max(saturation + movement.y / window_height(window), 0.0), 1.0)
# Create the color using HSB values color = hsb_color(hue, saturation, brightness)
# Clear the screen with the new color clear_screen(color)
refresh_window(window)
close_window(window)
if __name__ == "__main__": main()
Mouse Wheel Scroll
Mouse Wheel Scroll returns the distance and direction of the mouse scroll since the last time Process Events
was called. Like the previous function, this function returns the Vector 2D
data type.
Here is an example using vertical scrolling to change the hue of the screen colour:
#include "splashkit.h"
int main(){ // color variables double hue = 0.6; double saturation = 0.6; double brightness = 0.6;
// mouse scroll variable vector_2d scroll;
open_window("Mouse Scrolling", 800, 600);
while (!window_close_requested("Mouse Scrolling")) { // process user input events process_events();
// store the mouse wheel scroll direction and distance to the scroll variable scroll = mouse_wheel_scroll();
// change hue of screen colour based on vertical mouse scrolling (two fingers if using trackpad) hue += scroll.y / 100;
// display current screen colour clear_screen(hsb_color(hue, saturation, brightness)); refresh_screen(60); }
close_all_windows();
return 0;}
using SplashKitSDK;using static SplashKitSDK.SplashKit;
// color variablesdouble hue = 0.6;double saturation = 0.6;double brightness = 0.6;
// mouse scroll variableVector2D scroll;
OpenWindow("Mouse Scrolling", 800, 600);
while (!WindowCloseRequested("Mouse Scrolling")){ // process user input events ProcessEvents();
// store the mouse wheel scroll direction and distance to the scroll variable scroll = MouseWheelScroll();
// change hue of screen colour based on vertical mouse scrolling (two fingers if using trackpad) hue += scroll.Y / 100;
// display current screen colour ClearScreen(HSBColor(hue, saturation, brightness)); RefreshScreen(60);}
CloseAllWindows();
from splashkit import *
def main():
hue = 0.6 saturation = 0.6 brightness = 0.6
window = open_window("Mouse Scrolling", 800, 600)
while not window_close_requested(window): # Process user input events process_events()
# Store the mouse wheel scroll direction and distance to the scroll variable scroll = mouse_wheel_scroll()
# Change hue of screen color based on vertical mouse scrolling hue += scroll.y / 100.0
# Ensure hue is within the valid range [0.0, 1.0] hue = min(max(hue, 0.0), 1.0)
# Display current screen color clear_screen(hsb_color(hue, saturation, brightness)) refresh_window(window)
close_window(window)
if __name__ == "__main__": main()
Mouse Location Functionality
Now that we have learned how to handle mouse inputs and movements, let’s look at how to use the mouse’s location in the window, which can be used in your program to make the experience more interactive. Combined with the mouse input functions, it can be used to spawn items where the player clicked or respond to the player clicking where an item is located.
Mouse Position
Mouse Position returns the position of the mouse in the current window as a Point 2d data type, which is its x and y coordinates.
Here is a simple example that draws a circle at the mouse position:
#include "splashkit.h"
int main(){ open_window("Mouse Location", 800, 600);
while (!window_close_requested("Mouse Location")) { // process user input events process_events();
// draw blue circle at mouse position clear_screen(COLOR_WHITE); fill_circle(COLOR_LIGHT_BLUE, circle_at(mouse_position(), 20)); refresh_screen(60); }
close_all_windows();
return 0;}
using SplashKitSDK;using static SplashKitSDK.SplashKit;
OpenWindow("Mouse Location", 800, 600);
while (!WindowCloseRequested("Mouse Location")){ // process user input events ProcessEvents();
// draw blue circle at mouse position ClearScreen(ColorWhite()); FillCircle(ColorLightBlue(), CircleAt(MousePosition(), 20)); RefreshScreen(60);}
CloseAllWindows();
from splashkit import *
def main():
window = open_window("Mouse Location", 800, 600)
while not window_close_requested(window): # Process user input events process_events()
# Clear the screen with white color clear_screen(color_white())
# Get the current mouse position mouse_pos = mouse_position()
# Draw a blue circle at the current mouse position fill_circle(color_light_blue(), mouse_pos.x, mouse_pos.y, 20)
refresh_window(window)
close_window(window)
if __name__ == "__main__": main()
Mouse Position Vector
Mouse Position Vector returns the position of the mouse relative to the window origin as a Vector 2d.
Here is a simple example that displays the mouse position vector as a line:
#include "splashkit.h"
int main(){ open_window("Mouse Location", 800, 600);
while (!window_close_requested("Mouse Location")) { // process user input events process_events();
// draw line from mouse position vector clear_screen(COLOR_WHITE); draw_line(COLOR_BLUE, line_from(mouse_position_vector())); refresh_screen(60); }
close_all_windows();
return 0;}
using SplashKitSDK;using static SplashKitSDK.SplashKit;
OpenWindow("Mouse Location", 800, 600);
while (!WindowCloseRequested("Mouse Location")){ // process user input events ProcessEvents();
// draw line from mouse position vector ClearScreen(ColorWhite()); DrawLine(ColorBlue(), LineFrom(MousePositionVector())); RefreshScreen(60);}
CloseAllWindows();
from splashkit import *
def main():
window = open_window("Mouse Location", 800, 600)
while not window_close_requested(window): # Process user input events process_events()
# Clear the screen with white color clear_screen(color_white())
# Get the current mouse position mouse_pos = mouse_position_vector()
# Draw a line from the top-left corner to the current mouse position draw_line(color_blue(), 0, 0, mouse_pos.x, mouse_pos.y)
refresh_window(window)
close_window(window)
if __name__ == "__main__": main()
Mouse X
Mouse X returns the distance of the mouse from the left edge of the window.
Here is an example using the left mouse button to drag a “curtain” across the screen from the left side, based on the mouse’s x coordinate:
#include "splashkit.h"
int main(){ float x = 0;
open_window("Mouse Location", 800, 600);
while (!window_close_requested("Mouse Location")) { // process user input events process_events();
if (mouse_down(LEFT_BUTTON)) { x = mouse_x(); }
clear_screen(COLOR_WHITE); fill_circle(COLOR_BLUE, 400, 300, 100); // draw purple "curtain" from left side of screen to x coordinate of mouse fill_rectangle(COLOR_PURPLE, 0, 0, x, screen_height()); refresh_screen(60); }
close_all_windows();
return 0;}
using SplashKitSDK;
float x = 0;
OpenWindow("Mouse Location", 800, 600);
while (!WindowCloseRequested("Mouse Location")){ // process user input events ProcessEvents();
if (MouseDown(MouseButton.LeftButton)) { x = MouseX(); }
ClearScreen(ColorWhite()); FillCircle(ColorBlue(), 400, 300, 100); // draw purple "curtain" from left side of screen to x coordinate of mouse FillRectangle(ColorPurple(), 0, 0, x, ScreenHeight()); RefreshScreen(60);}
CloseAllWindows();
from splashkit import *
def main(): x = 0
window = open_window("Mouse Location", 800, 600)
while not window_close_requested(window): # Process user input events process_events()
# Update x-coordinate if the left mouse button is down if mouse_down(MouseButton.left_button): x = mouse_x()
# Clear the screen with white color clear_screen(color_white())
# Draw a blue circle at the center of the screen fill_circle(color_blue(), 400, 300, 100)
# Draw a purple "curtain" from the left side of the screen to the x-coordinate of the mouse fill_rectangle(color_purple(), 0, 0, x, window_height(window))
refresh_window(window)
close_window(window)
if __name__ == "__main__": main()
Mouse Y
Mouse Y returns the distance of the mouse from the top edge of the window.
Here is an example using the left mouse button to drag a “curtain/blind” down the screen from the top, based on the mouse’s y coordinate:
#include "splashkit.h"
int main(){ float y = 0;
open_window("Mouse Location", 800, 600);
while (!window_close_requested("Mouse Location")) { // process user input events process_events();
if (mouse_down(LEFT_BUTTON)) { y = mouse_y(); }
clear_screen(COLOR_WHITE); fill_circle(COLOR_BLUE, 400, 300, 100); // draw purple "curtain" from top of screen to y coordinate of mouse fill_rectangle(COLOR_PURPLE, 0, 0, screen_width(), y); refresh_screen(60); }
close_all_windows();
return 0;}
using SplashKitSDK;using static SplashKitSDK.SplashKit;
float y = 0;
OpenWindow("Mouse Location", 800, 600);
while (!WindowCloseRequested("Mouse Location")){ // process user input events ProcessEvents();
if (MouseDown(MouseButton.LeftButton)) { y = MouseY(); }
ClearScreen(ColorWhite()); FillCircle(ColorBlue(), 400, 300, 100); // draw purple "curtain" from top of screen to y coordinate of mouse FillRectangle(ColorPurple(), 0, 0, ScreenWidth(), y); RefreshScreen(60);}
CloseAllWindows();
from splashkit import *
def main(): y = 0
window = open_window("Mouse Location", 800, 600)
while not window_close_requested(window): # Process user input events process_events()
if mouse_down(MouseButton.left_button): y = mouse_y()
# Clear the screen with white color clear_screen(color_white())
# Draw a blue circle at the center of the screen fill_circle(color_blue(), 400, 300, 100)
# Draw a purple "curtain" from the top of the screen to the y-coordinate of the mouse fill_rectangle(color_purple(), 0, 0, window_width(window), y)
refresh_window(window)
close_window(window)
if __name__ == "__main__": main()
Mouse Visibility
Finally, we can also alter the mouse visibility on SplashKit. The following functions handles the mouse visibility. These can be useful to create a more immersive environment for your game.
Hide/Show Mouse
Hide Mouse hides the mouse from the screen. Show Mouse shows the mouse to the screen.
Here is a simple example where the mouse “disappears in a black hole”:
#include "splashkit.h"
int main(){ // create cirle variable for "black hole" circle blackHole;
open_window("Mouse Visibility", 800, 600);
// create black hole in centre of screen blackHole = circle_at(screen_center(), 50);
while (!window_close_requested("Mouse Visibility")) { // process user input events process_events();
// mouse disappears in black hole if (point_in_circle(mouse_position(), blackHole)) { hide_mouse(); } // mouse reappears once out of black hole else { show_mouse(); }
// display "black hole" on screen clear_screen(COLOR_WHITE); fill_circle(COLOR_BLACK, blackHole); refresh_screen(60); }
close_all_windows();
return 0;}
using SplashKitSDK;using static SplashKitSDK.SplashKit;
// create cirle variable for "black hole"Circle blackHole;
OpenWindow("Mouse Visibility", 800, 600);
// create black hole in centre of screenblackHole = CircleAt(ScreenCenter(), 50);
while (!WindowCloseRequested("Mouse Visibility")){ // process user input events ProcessEvents();
// mouse disappears in black hole if (PointInCircle(MousePosition(), blackHole)) { HideMouse(); } // mouse reappears once out of black hole else { ShowMouse(); }
// display "black hole" on screen ClearScreen(ColorWhite()); FillCircle(ColorBlack(), blackHole); RefreshScreen(60);}
CloseAllWindows();
from splashkit import *
def main():
black_hole_radius = 50 window_width, window_height = 800, 600 window = open_window("Mouse Visibility", window_width, window_height)
black_hole_center = point_at(window_width / 2, window_height / 2)
while not window_close_requested(window): # Process user input events process_events()
# Get the current mouse position mouse_pos = mouse_position()
# Calculate distance from the mouse to the black hole center dx = mouse_pos.x - black_hole_center.x dy = mouse_pos.y - black_hole_center.y distance = (dx ** 2 + dy ** 2) ** 0.5
# Hide or show the mouse based on distance if distance <= black_hole_radius: hide_mouse() else: show_mouse()
# Clear the screen with white color clear_screen(color_white())
# Draw the black hole as a circle fill_circle(color_black(), black_hole_center.x, black_hole_center.y, black_hole_radius)
refresh_window(window)
close_all_windows()
if __name__ == "__main__": main()
Show Mouse
There are two versions of the Show Mouse function. The first version is shown above. The second version shows or hides the mouse based on its boolean parameter. If true, it shows the mouse, while if false, it hides the mouse from the screen.
Here is a simple example where the mouse is only visible in the “spot light”:
#include "splashkit.h"
int main(){ // create cirle variable for emulating a "stage spot light" circle spotLight;
open_window("Mouse Visibility", 800, 600);
// create a "spot light" circle in centre of screen spotLight = circle_at(screen_center(), 100);
while (!window_close_requested("Mouse Visibility")) { // process user input events process_events();
// mouse disappears when not in the "spot light" show_mouse(point_in_circle(mouse_position(), spotLight));
// display "spot light" on screen clear_screen(COLOR_BLACK); fill_circle(COLOR_ALICE_BLUE, spotLight); refresh_screen(60); }
close_all_windows();
return 0;}
using SplashKitSDK;using static SplashKitSDK.SplashKit;
// create cirle variable for emulating a "stage spot light"Circle spotLight;
OpenWindow("Mouse Visibility", 800, 600);
// create a "spot light" circle in centre of screenspotLight = CircleAt(ScreenCenter(), 100);
while (!WindowCloseRequested("Mouse Visibility")){ // process user input events ProcessEvents();
// mouse disappears when not in the "spot light" ShowMouse(PointInCircle(MousePosition(), spotLight));
// display "spot light" on screen ClearScreen(ColorBlack()); FillCircle(ColorWhite(), spotLight); RefreshScreen(60);}
CloseAllWindows();
from splashkit import *
def main():
spotlight_radius = 100 window_width, window_height = 800, 600 window = open_window("Mouse Visibility in Spotlight", window_width, window_height)
spotlight_center = point_at(window_width / 2, window_height / 2)
mouse_visible = True
while not window_close_requested(window): # Process user input events process_events()
# Get the current mouse position mouse_pos = mouse_position()
# Calculate distance from the mouse to the spotlight center dx = mouse_pos.x - spotlight_center.x dy = mouse_pos.y - spotlight_center.y distance = (dx ** 2 + dy ** 2) ** 0.5
# Determine whether to show or hide the mouse if distance <= spotlight_radius: if not mouse_visible: show_mouse() # Show the mouse mouse_visible = True else: if mouse_visible: hide_mouse() # Hide the mouse mouse_visible = False
# Clear the screen with black color clear_screen(color_black())
# Draw the spotlight as a circle fill_circle(color_white(), spotlight_center.x, spotlight_center.y, spotlight_radius)
refresh_window(window)
close_all_windows()
if __name__ == "__main__": main()