Skip to main content

Input Module

Session::Input() is the toolbox for plugins with their own UI: it delivers raw input events, lets you block clicks from reaching the game, shows the mouse cursor, and locks game input while your interface is open.

Input events

EventPayloadFires when
OnKeykey code, OS flags, down/upA key is pressed or released.
OnCharUTF-32 codepointText is typed. Use this for text fields, not OnKey.
OnMouseMovex, y (client-relative)The mouse moves.
OnMouseWheelsigned notch deltaThe wheel scrolls.
OnMouseButtonbutton, down/up, block flagA mouse button changes state.
g_session->Input().OnKey.Register( []( TruckersMP::InputKeyEvent &e )
{
if( e.GetDown() && e.GetKey() == VK_F9 )
{
ToggleMyOverlay();
}
} );

OnMouseButton is the one payload you can write to: set Block and the click never reaches the game.

g_session->Input().OnMouseButton.Register( []( TruckersMP::InputMouseButtonEvent &e )
{
if( g_overlayOpen && IsOverMyWindow() )
{
HandleClick( e.GetButton(), e.GetDown() );
e.SetBlock( true );
}
} );

The mouse cursor

The cursor is reference-counted across every consumer in the client:

std::optional< Bool > IsMouseVisible() const;
Result IncreaseMouseRef();
Result DecreaseMouseRef();

IncreaseMouseRef asks for the cursor; DecreaseMouseRef gives that request back.

Pair every increase with exactly one decrease

An unmatched increase keeps the cursor on screen for the rest of the session.

A small RAII guard makes this hard to get wrong:

struct MouseCursorScope
{
MouseCursorScope() { g_session->Input().IncreaseMouseRef(); }
~MouseCursorScope() { g_session->Input().DecreaseMouseRef(); }
};

Game input locks

While your UI is open you usually want the game to stop reacting to input:

std::optional< Bool > IsGameMouseLocked() const;
Result SetGameMouseLocked( Bool locked );

std::optional< Bool > IsGameKeyboardLocked() const;
Result SetGameKeyboardLocked( Bool locked );

Locking the game mouse or keyboard stops game reactions; your plugin keeps receiving the input events either way. Unlock both when your UI closes, and also on truckersmp_shutdown paths that might leave your UI open.

A typical overlay flow

  1. User presses your hotkey (OnKey).
  2. Open your window, IncreaseMouseRef(), SetGameMouseLocked( true ), SetGameKeyboardLocked( true ).
  3. Route OnMouseMove, OnMouseButton (with SetBlock( true ) over your window), and OnChar into your UI.
  4. On close, reverse step 2 exactly once.