Lua:sendMessage

From Cheat Engine
Revision as of 00:38, 25 June 2026 by Leunsel (talk | contribs) (Major overhaul of the post.)
Jump to navigation Jump to search

<> Lua API Reference

function sendMessage(hwnd, msg, wparam, lparam) : integer

Sends a Windows message to a window.

This function is a low-level helper. It expects that you already know the target window handle, the message id, and the meaning of the wParam and lParam values for the message you are sending.

The message ids are Windows message constants. They are not specific to Cheat Engine.

Function Parameters

Parameter Type Description
hwnd Integer The window handle that should receive the message.
msg Integer The Windows message id to send.
wparam Integer The wParam value for the message.
lparam Integer The lParam value for the message.

Returns

Integer — The result returned by the message handler.

Common Windows Message IDs

Constant Value Description
WM_NULL 0x0000 Does nothing.
WM_CREATE 0x0001 Sent when a window is being created.
WM_DESTROY 0x0002 Sent when a window is being destroyed.
WM_MOVE 0x0003 Sent after a window has been moved.
WM_SIZE 0x0005 Sent after a window size has changed.
WM_ACTIVATE 0x0006 Sent when a window is activated or deactivated.
WM_SETFOCUS 0x0007 Sent when a window receives keyboard focus.
WM_KILLFOCUS 0x0008 Sent when a window loses keyboard focus.
WM_ENABLE 0x000A Sent when a window is enabled or disabled.
WM_SETREDRAW 0x000B Enables or disables redrawing of a window.
WM_SETTEXT 0x000C Sets the text of a window or control.
WM_GETTEXT 0x000D Gets the text of a window or control.
WM_GETTEXTLENGTH 0x000E Gets the length of the text of a window or control.
WM_PAINT 0x000F Requests that a window paints itself.
WM_CLOSE 0x0010 Requests that a window closes.
WM_SHOWWINDOW 0x0018 Sent when a window is shown or hidden.
WM_SETCURSOR 0x0020 Sent when the cursor is set for a window.
WM_NOTIFY 0x004E Sent by common controls to notify their parent window.
WM_KEYDOWN 0x0100 Sent when a key is pressed.
WM_KEYUP 0x0101 Sent when a key is released.
WM_CHAR 0x0102 Sent when a character input is generated.
WM_COMMAND 0x0111 Sent when a menu item, accelerator, or control notification is triggered.
WM_SYSCOMMAND 0x0112 Sent when a system command is requested.
WM_TIMER 0x0113 Sent when a timer expires.
WM_HSCROLL 0x0114 Sent when a horizontal scroll event occurs.
WM_VSCROLL 0x0115 Sent when a vertical scroll event occurs.
WM_MOUSEMOVE 0x0200 Sent when the mouse moves over a window.
WM_LBUTTONDOWN 0x0201 Sent when the left mouse button is pressed.
WM_LBUTTONUP 0x0202 Sent when the left mouse button is released.
WM_RBUTTONDOWN 0x0204 Sent when the right mouse button is pressed.
WM_RBUTTONUP 0x0205 Sent when the right mouse button is released.

Common Control Message IDs

Constant Value Description
BM_CLICK 0x00F5 Simulates a button click.
EM_SETSEL 0x00B1 Sets the selection range in an edit control.
EM_REPLACESEL 0x00C2 Replaces the current selection in an edit control.
CB_ADDSTRING 0x0143 Adds a string to a combo box.
CB_GETCURSEL 0x0147 Gets the selected item index of a combo box.
CB_SETCURSEL 0x014E Sets the selected item index of a combo box.
LB_ADDSTRING 0x0180 Adds a string to a list box.
LB_GETCURSEL 0x0188 Gets the selected item index of a list box.
LB_SETCURSEL 0x0186 Sets the selected item index of a list box.

Examples

local form = createForm()
form.Caption = "sendMessage Example"
form.show()

local hwnd = form.Handle

sendMessage(hwnd, 0x0007, 0, 0) -- WM_SETFOCUS
local form = createForm()
form.Caption = "Close Example"
form.show()

local hwnd = form.Handle

sendMessage(hwnd, 0x0010, 0, 0) -- WM_CLOSE
local form = createForm()
local button = createButton(form)

button.Parent = form
button.Caption = "Click me"
button.OnClick = function(sender)
  print("Button clicked")
end

form.show()

sendMessage(button.Handle, 0x00F5, 0, 0) -- BM_CLICK

Notes

  • The msg parameter is a Windows message id.
  • wParam and lParam depend entirely on the specific message.
  • Some messages require pointers, buffers, handles, or packed coordinate values.
  • Do not assume that wParam and lParam are always plain integers.
  • Prefer Cheat Engine's class properties and methods when they exist.
  • sendMessage is mainly useful for advanced Windows GUI interaction.

Main Pages

Core Lua documentation entry points

Lua
Script Engine