OpenTTD Source 20260911-master-gee2b2ac12a
hotkeys.cpp File Reference

Implementation of hotkey related functions. More...

#include "stdafx.h"
#include "core/backup_type.hpp"
#include "gfx_func.h"
#include "openttd.h"
#include "hotkeys.h"
#include "ini_type.h"
#include "string_func.h"
#include "window_gui.h"
#include "core/string_consumer.hpp"
#include "safeguards.h"

Go to the source code of this file.

Data Structures

struct  KeycodeNames
 String representation of a keycode. More...

Functions

static std::optional< uint16_t > ParseCode (std::string_view keystr)
 Try to parse a single part of a keycode.
static std::optional< uint16_t > ParseKeycode (std::string_view keystr)
 Parse a string representation of a keycode.
static void ParseHotkeys (Hotkey &hotkey, std::string_view value)
 Parse a string to the keycodes it represents.
static std::string KeycodeToString (uint16_t keycode)
 Convert a hotkey to it's string representation so it can be written to the config file.
std::string SaveKeycodes (const Hotkey &hotkey)
 Convert all keycodes attached to a hotkey to a single string.
static void SaveLoadHotkeys (bool save)
void LoadHotkeysFromConfig ()
 Load the hotkeys from the config file.
void SaveHotkeysToConfig ()
 Save the hotkeys to the config file.
static EventState HandleGlobalHotkey (HotkeyList::GlobalHotkeyHandlerFunc func, int hotkey)
 Call the global handler for a hotkey.
void HandleGlobalHotkeys (char32_t key, uint16_t keycode)

Variables

std::string _hotkeys_file
static std::vector< HotkeyList * > * _hotkey_lists = nullptr
 List of all HotkeyLists.
static const std::initializer_list< KeycodeNames_keycode_to_name
 Array of non-standard keycodes that can be used in the hotkeys config file.

Detailed Description

Implementation of hotkey related functions.

Definition in file hotkeys.cpp.

Function Documentation

◆ HandleGlobalHotkey()

EventState HandleGlobalHotkey ( HotkeyList::GlobalHotkeyHandlerFunc func,
int hotkey )
static

Call the global handler for a hotkey.

Note
Clears shift and ctrl keystate to allow hotkeys to use modifiers.
Parameters
funcThe global hotkey handler.
hotkeyThe hotkey.
Returns
EventState of the hotkey handler.

Definition at line 353 of file hotkeys.cpp.

References _ctrl_pressed, and _shift_pressed.

◆ HandleGlobalHotkeys()

void HandleGlobalHotkeys ( char32_t key,
uint16_t keycode )

Definition at line 362 of file hotkeys.cpp.

◆ KeycodeToString()

std::string KeycodeToString ( uint16_t keycode)
static

Convert a hotkey to it's string representation so it can be written to the config file.

Separate parts of the keycode (like "CTRL" and "F1" are split by a '+'.

Parameters
keycodeThe keycode to convert to a string.
Returns
A string representation of this keycode.

Definition at line 165 of file hotkeys.cpp.

References _keycode_to_name, and WKC_GLOBAL_HOTKEY.

Referenced by SaveKeycodes().

◆ LoadHotkeysFromConfig()

void LoadHotkeysFromConfig ( )

Load the hotkeys from the config file.

Definition at line 335 of file hotkeys.cpp.

Referenced by AfterNewGRFScan::OnNewGRFsScanned().

◆ ParseCode()

std::optional< uint16_t > ParseCode ( std::string_view keystr)
static

Try to parse a single part of a keycode.

Parameters
keystrInput.
Returns
A keycode if a match is found.

Definition at line 99 of file hotkeys.cpp.

References _keycode_to_name, StrEqualsIgnoreCase(), and StringConsumer::WHITESPACE_NO_NEWLINE.

Referenced by ParseKeycode().

◆ ParseHotkeys()

void ParseHotkeys ( Hotkey & hotkey,
std::string_view value )
static

Parse a string to the keycodes it represents.

Parameters
hotkeyThe hotkey object to add the keycodes to
valueThe string to parse

Definition at line 148 of file hotkeys.cpp.

References Hotkey::AddKeycode(), StringConsumer::AnyBytesLeft(), ParseKeycode(), StringConsumer::ReadUntilChar(), and StringConsumer::SKIP_ONE_SEPARATOR.

Referenced by HotkeyList::Load().

◆ ParseKeycode()

std::optional< uint16_t > ParseKeycode ( std::string_view keystr)
static

Parse a string representation of a keycode.

Parameters
keystrInput.
Returns
A valid keycode or std::nullopt.

Definition at line 121 of file hotkeys.cpp.

References StringConsumer::AnyBytesLeft(), ParseCode(), StringConsumer::ReadUntilChar(), and StringConsumer::SKIP_ONE_SEPARATOR.

Referenced by ParseHotkeys().

◆ SaveHotkeysToConfig()

void SaveHotkeysToConfig ( )

Save the hotkeys to the config file.

Definition at line 341 of file hotkeys.cpp.

◆ SaveKeycodes()

std::string SaveKeycodes ( const Hotkey & hotkey)

Convert all keycodes attached to a hotkey to a single string.

If multiple keycodes are attached to the hotkey they are split by a comma.

Parameters
hotkeyThe keycodes of this hotkey need to be converted to a string.
Returns
A string representation of all keycodes.

Definition at line 207 of file hotkeys.cpp.

References KeycodeToString().

Referenced by HotkeyList::Save().

◆ SaveLoadHotkeys()

void SaveLoadHotkeys ( bool save)
static

Definition at line 317 of file hotkeys.cpp.

Variable Documentation

◆ _hotkey_lists

std::vector<HotkeyList*>* _hotkey_lists = nullptr
static

List of all HotkeyLists.

This is a pointer to ensure initialisation order with the various static HotkeyList instances.

Definition at line 28 of file hotkeys.cpp.

Referenced by HotkeyList::~HotkeyList().

◆ _hotkeys_file

std::string _hotkeys_file

Definition at line 22 of file hotkeys.cpp.

◆ _keycode_to_name

const std::initializer_list<KeycodeNames> _keycode_to_name
static

Array of non-standard keycodes that can be used in the hotkeys config file.

Definition at line 37 of file hotkeys.cpp.

Referenced by KeycodeToString(), and ParseCode().