QtCraft API C++ classes QML types Handbook

QcKeyBindings Class

Which keys trigger which action of the game, as a table: every action has a primary and a secondary key (Qt::Key values, 0 = none). More...

Header: #include <common/gameplay/QcKeyBindings.h>

Public Types

struct Displaced
enum class Action { Forward, Back, Left, Right, Jump, …, Pause }
enum class Platform { Mac, Other }

Public Functions

std::optional<QcKeyBindings::Action> actionFor(int key) const
std::optional<QcKeyBindings::Action> actionForPress(int key, Qt::KeyboardModifiers modifiers) const
std::optional<QcKeyBindings::Displaced> bind(QcKeyBindings::Action action, int slot, int key)
QList<std::pair<QcKeyBindings::Action, int>> conflicts() const
QList<std::pair<QcKeyBindings::Action, int>> dropConflicts(const QList<std::pair<QcKeyBindings::Action, int>> &preferred = {})
int key(QcKeyBindings::Action action, int slot) const
QList<int> keys(QcKeyBindings::Action action) const
bool matches(QcKeyBindings::Action action, int key) const
QMap<QString, QStringList> placeholderKeys() const
QcKeyBindings::Platform platform() const
void setKey(QcKeyBindings::Action action, int slot, int key)

Static Public Members

const int actionCount
std::optional<QcKeyBindings::Action> actionFromId(const QString &id)
const QList<QcKeyBindings::Action> &actions()
const int clearKey
QcKeyBindings::Platform currentPlatform()
QcKeyBindings defaults(QcKeyBindings::Platform platform = currentPlatform())
QString id(QcKeyBindings::Action action)
bool isBindable(int key, QcKeyBindings::Platform platform)
bool isFixed(QcKeyBindings::Action action)
bool isFunctionKey(int key)
std::optional<int> keyFromName(const QString &name)
QString keyName(int key)
const int slotCount
Qt::KeyboardModifier stackModifier(QcKeyBindings::Platform platform)

Detailed Description

The defaults work on a Mac keyboard without fn: no primary key is a function key, because macOS uses F1-F12 for system features unless fn is held; the familiar function keys stay as secondary keys. On macOS no default uses Cmd (Qt::Key_Control there) or Control (Qt::Key_Meta), whose combinations macOS takes for itself (Cmd+Q, Cmd+Space, Control+arrow keys, Control+Space); sprint is Option there and Cmd cannot be bound at all. Key names ("V", "F5", "Space", "/") are portable and used in the settings file.

Member Type Documentation

enum class QcKeyBindings::Action

The actions that keys trigger, in table order.

ConstantValueDescription
QcKeyBindings::Action::Forward0Walks forward.
QcKeyBindings::Action::Back1Walks back.
QcKeyBindings::Action::Left2Walks left.
QcKeyBindings::Action::Right3Walks right.
QcKeyBindings::Action::Jump4Jumps (swims up, climbs, flies up).
QcKeyBindings::Action::Sneak5Sneaks (flies down).
QcKeyBindings::Action::Sprint6Sprints.
QcKeyBindings::Action::LookUp7Turns the view up.
QcKeyBindings::Action::LookDown8Turns the view down.
QcKeyBindings::Action::LookLeft9Turns the view left.
QcKeyBindings::Action::LookRight10Turns the view right.
QcKeyBindings::Action::Mine11Mines the targeted block or hits the targeted creature.
QcKeyBindings::Action::Use12Uses the held item or the targeted block.
QcKeyBindings::Action::PickBlock13The targeted block's item into the hand (also the middle mouse button, decision E-285).
QcKeyBindings::Action::Drop14Drops the held item (the whole stack with stackModifier()).
QcKeyBindings::Action::Inventory15Opens the inventory (the item catalogue in creative mode).
QcKeyBindings::Action::Chat16Opens the chat.
QcKeyBindings::Action::Command17Opens the chat with "/" typed.
QcKeyBindings::Action::CameraView18Cycles the camera: first person, behind, front.
QcKeyBindings::Action::Handbook19Opens the handbook.
QcKeyBindings::Action::Advancements20The tree of the adventure guide (decision E-924).
QcKeyBindings::Action::DebugOverlay21Cycles the debug display: off, compact, full (decision E-225).
QcKeyBindings::Action::DebugOptions22The debug options: visualisations switched one by one (decision E-225).
QcKeyBindings::Action::Labels23Shows or hides the labels floating in the world.
QcKeyBindings::Action::PlayerList24Held down: the list of the players on the server (decision E-259).
QcKeyBindings::Action::Hotbar125Selects hotbar slot 1.
QcKeyBindings::Action::Hotbar226Selects hotbar slot 2.
QcKeyBindings::Action::Hotbar327Selects hotbar slot 3.
QcKeyBindings::Action::Hotbar428Selects hotbar slot 4.
QcKeyBindings::Action::Hotbar529Selects hotbar slot 5.
QcKeyBindings::Action::Hotbar630Selects hotbar slot 6.
QcKeyBindings::Action::Hotbar731Selects hotbar slot 7.
QcKeyBindings::Action::Hotbar832Selects hotbar slot 8.
QcKeyBindings::Action::Hotbar933Selects hotbar slot 9.
QcKeyBindings::Action::HotbarRadial34Held down: the hotbar's radial menu, a slot chosen by moving the mouse (decision E-614).
QcKeyBindings::Action::HideHud35Photo mode (decision E-692): hides the HUD with every overlay and the crosshair (the hand too, as the settings say).
QcKeyBindings::Action::Screenshot36Saves a screenshot of the window.
QcKeyBindings::Action::Fullscreen37Fullscreen and back to the window mode before it (decision E-835); F11 as elsewhere.
QcKeyBindings::Action::Pause38Esc: fixed, because Esc also cancels every screen and the key capture.

enum class QcKeyBindings::Platform

Mac keyboards (Cmd, Control, Option) or the others (Ctrl, Meta, Alt).

ConstantValueDescription
QcKeyBindings::Platform::Mac0A Mac keyboard.
QcKeyBindings::Platform::Other1Any other keyboard.

Member Function Documentation

std::optional<QcKeyBindings::Action> QcKeyBindings::actionFor(int key) const

Returns the first action (in table order) bound to key.

std::optional<QcKeyBindings::Action> QcKeyBindings::actionForPress(int key, Qt::KeyboardModifiers modifiers) const

Returns the action of a press of key with modifiers. On a Mac a press with Cmd (Qt::ControlModifier) triggers nothing, so Cmd+Q, Cmd+H, Cmd+W … keep their system meaning; Option and Control do.

[static] std::optional<QcKeyBindings::Action> QcKeyBindings::actionFromId(const QString &id)

Returns the action with the id id; std::nullopt for unknown ids.

[static] const QList<QcKeyBindings::Action> &QcKeyBindings::actions()

Returns every action in table order.

std::optional<QcKeyBindings::Displaced> QcKeyBindings::bind(QcKeyBindings::Action action, int slot, int key)

Binds key (0 clears) to slot of action. A key used elsewhere is swapped: the other action gets the key this slot had before, so no key ends up with two actions. Returns that change, if any. Fixed actions and keys that are not bindable are refused (std::nullopt, nothing changes).

QList<std::pair<QcKeyBindings::Action, int>> QcKeyBindings::conflicts() const

Returns the pairs of (action, slot) that share a key with an earlier one; empty for a consistent table.

[static] QcKeyBindings::Platform QcKeyBindings::currentPlatform()

Returns the platform of the running program.

[static] QcKeyBindings QcKeyBindings::defaults(QcKeyBindings::Platform platform = currentPlatform())

Returns the default table for platform.

QList<std::pair<QcKeyBindings::Action, int>> QcKeyBindings::dropConflicts(const QList<std::pair<QcKeyBindings::Action, int>> &preferred = {})

Removes keys used twice. A key stays with the first of the preferred slots that holds it (loading: the slots the player set himself beat defaults that came later), otherwise with the first action in table order. Returns the slots that lost their key.

[static] QString QcKeyBindings::id(QcKeyBindings::Action action)

Returns the stable id of action used in the settings file and placeholders, e.g. "cameraView", "hotbar1".

[static] bool QcKeyBindings::isBindable(int key, QcKeyBindings::Platform platform)

Returns whether a player may bind key on platform: not Esc, not Backspace (clears), not "unknown", not Cmd on a Mac.

[static] bool QcKeyBindings::isFixed(QcKeyBindings::Action action)

Returns whether action cannot be rebound (the pause key).

[static] bool QcKeyBindings::isFunctionKey(int key)

Returns whether key is one of the function keys F1 to F35.

int QcKeyBindings::key(QcKeyBindings::Action action, int slot) const

Returns the key in slot of action; 0 for none.

See also setKey().

[static] std::optional<int> QcKeyBindings::keyFromName(const QString &name)

Returns the key named name, the inverse of keyName(); std::nullopt for unknown names. "" gives 0 (no key).

[static] QString QcKeyBindings::keyName(int key)

Returns the portable name of key; empty for 0 or keys without a name.

QList<int> QcKeyBindings::keys(QcKeyBindings::Action action) const

Returns both keys of action, primary first, without empty slots.

bool QcKeyBindings::matches(QcKeyBindings::Action action, int key) const

Returns whether key is one of the keys of action.

QMap<QString, QStringList> QcKeyBindings::placeholderKeys() const

Returns the values for handbook placeholders: "key.<id>" lists all keys of an action, "key1.<id>" only the primary key, both as key names; on a Mac the modifiers are "MacCmd", "MacControl" and "MacOption", so the handbook names them like the keyboard, and a function key that comes first is "fn+F11" (no letter key before it; a Mac needs fn for it). "key.stackModifier" names the modifier of stackModifier().

QcKeyBindings::Platform QcKeyBindings::platform() const

Returns the platform the table was made for.

void QcKeyBindings::setKey(QcKeyBindings::Action action, int slot, int key)

Sets slot of action to key as it is, without resolving conflicts (loading; see dropConflicts()). Fixed actions and unbindable keys are ignored.

See also key().

[static] Qt::KeyboardModifier QcKeyBindings::stackModifier(QcKeyBindings::Platform platform)

Returns the modifier that makes the drop key throw the whole stack on platform: Ctrl, on a Mac Control (Cmd+Q quits there).

Member Variable Documentation

const int QcKeyBindings::actionCount

The number of actions.

const int QcKeyBindings::clearKey

The single key that clears a slot while capturing (and therefore cannot be bound).

const int QcKeyBindings::slotCount

Keys per action: primary and secondary.