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_APPLYApply on widget
pygame_menu.controls.KEY_BACKMove back on menu or widget
pygame_menu.controls.KEY_CLOSE_MENUClose menu
pygame_menu.controls.KEY_LEFTMove left
pygame_menu.controls.KEY_MOVE_DOWNMove down
pygame_menu.controls.KEY_MOVE_UPMove up
pygame_menu.controls.KEY_RIGHTMove right
pygame_menu.controls.KEY_TABApply tab
Note: See
pygame_menu.controlsmodule 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
Controller class. Accepts any object and provides functions to handle each event.
Accepts apply key. Requires
pygame.KEYDOWN.
Accepts back key. Requires
pygame.KEYDOWN.
Accepts close menu key. Requires
pygame.KEYDOWN.
Accepts delete key. Requires
pygame.KEYDOWN.
Accepts end key. Requires
pygame.KEYDOWN.
Accepts escape key. Requires
pygame.KEYDOWN.
Accepts home key. Requires
pygame.KEYDOWN.
Accepts joy movement on x-axis (left direction). Requires
pygame.JOYAXISMOTION.
Accepts joy movement on x-axis (right direction). Requires
pygame.JOYAXISMOTION.
Accepts joy movement on y-axis (down direction). Requires
pygame.JOYAXISMOTION.
Accepts joy movement on y-axis (up direction). Requires
pygame.JOYAXISMOTION.
Accepts joy back button. Requires
pygame.JOYBUTTONDOWN.
Accepts joy movement to down direction. Requires
pygame.JOYHATMOTION.
Accepts joy movement to left direction. Requires
pygame.JOYHATMOTION.
Accepts joy movement to right direction. Requires
pygame.JOYHATMOTION.
Accepts joy select button. Also used for apply(). Requires
pygame.JOYBUTTONDOWN.
Accepts joy movement to up direction. Requires
pygame.JOYHATMOTION.
Accepts left key. Requires
pygame.KEYDOWN.
Accepts move down key. Requires
pygame.KEYDOWN.
Accepts move up key. Requires
pygame.KEYDOWN.
Accepts right key. Requires
pygame.KEYDOWN.