Configure controller

The pygame-menu driver controller is very simple, and can be modified in two different ways.

First, in pygame_menu.controls there are different constants to modify each key to different events, such as apply on a widget, scroll left or right, among others. If any constant is modified it will apply to the whole library.

Event

Description

pygame_menu.controls.KEY_APPLY

Apply on widget

pygame_menu.controls.KEY_BACK

Move back on menu or widget

pygame_menu.controls.KEY_CLOSE_MENU

Close menu

pygame_menu.controls.KEY_LEFT

Move left

pygame_menu.controls.KEY_MOVE_DOWN

Move down

pygame_menu.controls.KEY_MOVE_UP

Move up

pygame_menu.controls.KEY_RIGHT

Move right

pygame_menu.controls.KEY_TAB

Apply tab

Note: See pygame_menu.controls module for more information.

The following example changes the return key from RETURN to A:

import pygame_menu.controls as ctrl
ctrl.KEY_APPLY = pygame.K_a

...
menu.add.button('button', lambda: print('Clicked!'))

As you might have seen, this procedure to change events is very simple, but limited. For such reason, pygame-menu provides another way to change the controls, by using the Controller object.

This class can be passed to widgets or menu by calling object.set_controller(controller). Controller class defines several functions that can be overriden, or you can create a subclass using inheritance. For example, the following example changes apply event to keys A, B or C, and additionally this changes the menu background color on demand. Methods must return True if satisfy the event condition.

from pygame_menu.controls import Controller
from random import randrange

# Create a controller
custom_controller = Controller()

def btn_apply(event, menu_object):
    applied = event.key in (pygame.K_a, pygame.K_b, pygame.K_c)
    if applied:
        random_color = (randrange(0, 255), randrange(0, 255), randrange(0, 255))
        menu_object.get_scrollarea().update_area_color(random_color)
    return applied

custom_controller.apply = btn_apply

...
button = menu.add.button('My button', lambda: print('Clicked!'))
button.set_controller(custom_controller) # Pass new controller to object
class pygame_menu.controls.Controller[source]

Controller class. Accepts any object and provides functions to handle each event.

static apply(event, widget)[source]

Accepts apply key. Requires pygame.KEYDOWN.

Parameters:
Return type:

bool

Returns:

True if event matches

static back(event, widget)[source]

Accepts back key. Requires pygame.KEYDOWN.

Parameters:
Return type:

bool

Returns:

True if event matches

static close_menu(event, widget)[source]

Accepts close menu key. Requires pygame.KEYDOWN.

Parameters:
Return type:

bool

Returns:

True if event matches

static delete(event, widget)[source]

Accepts delete key. Requires pygame.KEYDOWN.

Parameters:
Return type:

bool

Returns:

True if event matches

static end(event, widget)[source]

Accepts end key. Requires pygame.KEYDOWN.

Parameters:
Return type:

bool

Returns:

True if event matches

static escape(event, widget)[source]

Accepts escape key. Requires pygame.KEYDOWN.

Parameters:
Return type:

bool

Returns:

True if event matches

static home(event, widget)[source]

Accepts home key. Requires pygame.KEYDOWN.

Parameters:
Return type:

bool

Returns:

True if event matches

static joy_axis_x_left(event, widget)[source]

Accepts joy movement on x-axis (left direction). Requires pygame.JOYAXISMOTION.

Parameters:
Return type:

bool

Returns:

True if event matches

static joy_axis_x_right(event, widget)[source]

Accepts joy movement on x-axis (right direction). Requires pygame.JOYAXISMOTION.

Parameters:
Return type:

bool

Returns:

True if event matches

static joy_axis_y_down(event, widget)[source]

Accepts joy movement on y-axis (down direction). Requires pygame.JOYAXISMOTION.

Parameters:
Return type:

bool

Returns:

True if event matches

static joy_axis_y_up(event, widget)[source]

Accepts joy movement on y-axis (up direction). Requires pygame.JOYAXISMOTION.

Parameters:
Return type:

bool

Returns:

True if event matches

static joy_back(event, widget)[source]

Accepts joy back button. Requires pygame.JOYBUTTONDOWN.

Parameters:
Return type:

bool

Returns:

True if event matches

static joy_down(event, widget)[source]

Accepts joy movement to down direction. Requires pygame.JOYHATMOTION.

Parameters:
Return type:

bool

Returns:

True if event matches

static joy_left(event, widget)[source]

Accepts joy movement to left direction. Requires pygame.JOYHATMOTION.

Parameters:
Return type:

bool

Returns:

True if event matches

static joy_right(event, widget)[source]

Accepts joy movement to right direction. Requires pygame.JOYHATMOTION.

Parameters:
Return type:

bool

Returns:

True if event matches

static joy_select(event, widget)[source]

Accepts joy select button. Also used for apply(). Requires pygame.JOYBUTTONDOWN.

Parameters:
Return type:

bool

Returns:

True if event matches

static joy_up(event, widget)[source]

Accepts joy movement to up direction. Requires pygame.JOYHATMOTION.

Parameters:
Return type:

bool

Returns:

True if event matches

static left(event, widget)[source]

Accepts left key. Requires pygame.KEYDOWN.

Parameters:
Return type:

bool

Returns:

True if event matches

static move_down(event, widget)[source]

Accepts move down key. Requires pygame.KEYDOWN.

Parameters:
Return type:

bool

Returns:

True if event matches

static move_up(event, widget)[source]

Accepts move up key. Requires pygame.KEYDOWN.

Parameters:
Return type:

bool

Returns:

True if event matches

static right(event, widget)[source]

Accepts right key. Requires pygame.KEYDOWN.

Parameters:
Return type:

bool

Returns:

True if event matches

static tab(event, widget)[source]

Accepts tab key. Requires pygame.KEYDOWN.

Parameters:
Return type:

bool

Returns:

True if event matches