Difference between revisions of "Lua:sendMessage"

From Cheat Engine
Jump to navigation Jump to search
(Created page with "<p> <b>sendMessage(hwnd, msg, wparam, lparam)</b>: result - Sends a message to a window. Those that wish to use it, should know how to use it (and fill in the msg id's yoursel...")
 
m (Examples: Syntax Highlight.)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
<p>
+
[[Category:Lua]]
<b>sendMessage(hwnd, msg, wparam, lparam)</b>: result - Sends a message to a window. Those that wish to use it, should know how to use it (and fill in the msg id's yourself)
+
{{CodeBox|'''function''' sendMessage(''hwnd'', ''msg'', ''wparam'', ''lparam'') ''':''' integer}}
</p>
 
<p>https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendmessage</p>
 
<pre>
 
local WM_IME_CHAR = 0x0286
 
local hWnd = 0x000406E0
 
  
local text = "hello"
+
Sends a Windows message to a window.
  
for i=1,#text do
+
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.
  local c = string.sub(text, i, i)
+
 
  local wParam = textToShortCut(c)
+
The message ids are Windows message constants. They are not specific to Cheat Engine.
  local lParam = textToShortCut(c)
+
 
  sendMessage( hWnd, WM_IME_CHAR, wParam, lParam )
+
===Function Parameters===
 +
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Parameter
 +
!align="left"|Type
 +
!style="width: 80%;background-color:white;" align="left"|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===
 +
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Constant
 +
!align="left"|Value
 +
!style="width: 80%;background-color:white;" align="left"|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===
 +
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Constant
 +
!align="left"|Value
 +
!style="width: 80%;background-color:white;" align="left"|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===
 +
<syntaxhighlight lang="lua" line>
 +
local form = createForm()
 +
form.Caption = "sendMessage Example"
 +
form.show()
 +
 
 +
local hwnd = form.Handle
 +
 
 +
sendMessage(hwnd, 0x0007, 0, 0) -- WM_SETFOCUS
 +
</syntaxhighlight>
 +
 
 +
<syntaxhighlight lang="lua" line>
 +
local form = createForm()
 +
form.Caption = "Close Example"
 +
form.show()
 +
 
 +
local hwnd = form.Handle
 +
 
 +
sendMessage(hwnd, 0x0010, 0, 0) -- WM_CLOSE
 +
</syntaxhighlight>
 +
 
 +
<syntaxhighlight lang="lua" line>
 +
local form = createForm()
 +
local button = createButton(form)
 +
 
 +
button.Parent = form
 +
button.Caption = "Click me"
 +
button.OnClick = function(sender)
 +
  print("Button clicked")
 
end
 
end
</pre>
+
 
 +
form.show()
 +
 
 +
sendMessage(button.Handle, 0x00F5, 0, 0) -- BM_CLICK
 +
</syntaxhighlight>
 +
 
 +
===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.
 +
 
 +
{{LuaSeeAlso}}

Latest revision as of 23:58, 26 June 2026

<> 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[edit]

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[edit]

Integer — The result returned by the message handler.

Common Windows Message IDs[edit]

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[edit]

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[edit]

1 local form = createForm()
2 form.Caption = "sendMessage Example"
3 form.show()
4 
5 local hwnd = form.Handle
6 
7 sendMessage(hwnd, 0x0007, 0, 0) -- WM_SETFOCUS
1 local form = createForm()
2 form.Caption = "Close Example"
3 form.show()
4 
5 local hwnd = form.Handle
6 
7 sendMessage(hwnd, 0x0010, 0, 0) -- WM_CLOSE
 1 local form = createForm()
 2 local button = createButton(form)
 3 
 4 button.Parent = form
 5 button.Caption = "Click me"
 6 button.OnClick = function(sender)
 7   print("Button clicked")
 8 end
 9 
10 form.show()
11 
12 sendMessage(button.Handle, 0x00F5, 0, 0) -- BM_CLICK

Notes[edit]

  • 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