Difference between revisions of "Lua:textToShortCut"

From Cheat Engine
Jump to navigation Jump to search
m (Minor edit. Added Version Notice Template.)
(Major overhaul of the post.)
 
Line 1: Line 1:
 +
[[Category:Lua]]
 
{{VersionNotice|6.4+}}
 
{{VersionNotice|6.4+}}
{{NeedWork}}
+
{{CodeBox|'''function''' textToShortCut(''shortcutstring'') ''':''' integer}}
<pre>
+
 
print(textToShortCut("A")) -- 65
+
Converts a textual shortcut representation to a shortcut integer.
print(textToShortCut("up")) -- 38
+
 
print(textToShortCut("down")) -- 40
+
This function is available in Cheat Engine 6.4 and later.
print(textToShortCut("left")) -- 37
+
 
print(textToShortCut("right")) -- 39
+
===Function Parameters===
</pre>
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Parameter
 +
!align="left"|Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|shortcutstring
 +
|String
 +
|The textual shortcut representation to convert, such as "Ctrl+Alt+F1".
 +
|}
 +
 
 +
===Returns===
 +
Integer — The shortcut integer represented by the given string.
 +
 
 +
===Description===
 +
textToShortCut converts a human-readable shortcut string into the integer format used by Cheat Engine/Lazarus shortcut handling.
 +
 
 +
This is useful when shortcut values need to be stored as readable text and later applied to menu items, actions, or other objects that expect a shortcut integer.
 +
 
 +
===Examples===
 +
 
 +
====Convert a shortcut string====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
local shortcut = textToShortCut("Ctrl+Alt+F1")
 +
 
 +
print(shortcut)
 +
</syntaxhighlight>
 +
 
 +
====Assign a shortcut to a menu item====
 +
<syntaxhighlight lang="lua" line highlight="6">
 +
local menuItem = createMenuItem(MainForm)
 +
menuItem.Caption = "Custom Action"
 +
menuItem.OnClick = function()
 +
  print("Custom action executed")
 +
end
 +
menuItem.ShortCut = textToShortCut("Ctrl+Shift+F5")
 +
</syntaxhighlight>
 +
 
 +
====Store shortcuts as readable strings====
 +
<syntaxhighlight lang="lua" line highlight="6">
 +
local shortcuts = {
 +
  OpenMenu = "Ctrl+Alt+O",
 +
  RunAction = "Ctrl+Shift+R"
 +
}
 +
 
 +
local runShortcut = textToShortCut(shortcuts.RunAction)
 +
 
 +
print(runShortcut)
 +
</syntaxhighlight>
 +
 
 +
====Convert user input to a shortcut====
 +
<syntaxhighlight lang="lua" line highlight="4">
 +
local shortcutText = inputQuery("Shortcut", "Enter a shortcut:", "Ctrl+F1")
 +
 
 +
if shortcutText ~= nil and shortcutText ~= "" then
 +
  local shortcut = textToShortCut(shortcutText)
 +
 
 +
  print("Shortcut integer: " .. tostring(shortcut))
 +
end
 +
</syntaxhighlight>
 +
 
 +
====Use a helper function====
 +
<syntaxhighlight lang="lua" line highlight="6">
 +
local function applyShortcut(menuItem, shortcutText)
 +
  if menuItem == nil or shortcutText == nil or shortcutText == "" then
 +
    return false
 +
  end
 +
 
 +
  menuItem.ShortCut = textToShortCut(shortcutText)
 +
 
 +
  return true
 +
end
 +
 
 +
applyShortcut(MainForm.MenuItem2, "Ctrl+Alt+M")
 +
</syntaxhighlight>
 +
 
 +
====Convert multiple shortcut strings====
 +
<syntaxhighlight lang="lua" line highlight="8">
 +
local shortcutTexts = {
 +
  "Ctrl+F1",
 +
  "Ctrl+F2",
 +
  "Ctrl+F3"
 +
}
 +
 
 +
for i, shortcutText in ipairs(shortcutTexts) do
 +
  local shortcut = textToShortCut(shortcutText)
 +
 
 +
  print(shortcutText .. " = " .. tostring(shortcut))
 +
end
 +
</syntaxhighlight>
 +
 
 +
{{LuaSeeAlso}}

Latest revision as of 19:48, 25 June 2026

⚠️ Version Notice

This function requires Cheat Engine 6.4+.

<> Lua API Reference

function textToShortCut(shortcutstring) : integer

Converts a textual shortcut representation to a shortcut integer.

This function is available in Cheat Engine 6.4 and later.

Function Parameters[edit]

Parameter Type Description
shortcutstring String The textual shortcut representation to convert, such as "Ctrl+Alt+F1".

Returns[edit]

Integer — The shortcut integer represented by the given string.

Description[edit]

textToShortCut converts a human-readable shortcut string into the integer format used by Cheat Engine/Lazarus shortcut handling.

This is useful when shortcut values need to be stored as readable text and later applied to menu items, actions, or other objects that expect a shortcut integer.

Examples[edit]

Convert a shortcut string[edit]

1 local shortcut = textToShortCut("Ctrl+Alt+F1")
2 
3 print(shortcut)

Assign a shortcut to a menu item[edit]

1 local menuItem = createMenuItem(MainForm)
2 menuItem.Caption = "Custom Action"
3 menuItem.OnClick = function()
4   print("Custom action executed")
5 end
6 menuItem.ShortCut = textToShortCut("Ctrl+Shift+F5")

Store shortcuts as readable strings[edit]

1 local shortcuts = {
2   OpenMenu = "Ctrl+Alt+O",
3   RunAction = "Ctrl+Shift+R"
4 }
5 
6 local runShortcut = textToShortCut(shortcuts.RunAction)
7 
8 print(runShortcut)

Convert user input to a shortcut[edit]

1 local shortcutText = inputQuery("Shortcut", "Enter a shortcut:", "Ctrl+F1")
2 
3 if shortcutText ~= nil and shortcutText ~= "" then
4   local shortcut = textToShortCut(shortcutText)
5 
6   print("Shortcut integer: " .. tostring(shortcut))
7 end

Use a helper function[edit]

 1 local function applyShortcut(menuItem, shortcutText)
 2   if menuItem == nil or shortcutText == nil or shortcutText == "" then
 3     return false
 4   end
 5 
 6   menuItem.ShortCut = textToShortCut(shortcutText)
 7 
 8   return true
 9 end
10 
11 applyShortcut(MainForm.MenuItem2, "Ctrl+Alt+M")

Convert multiple shortcut strings[edit]

 1 local shortcutTexts = {
 2   "Ctrl+F1",
 3   "Ctrl+F2",
 4   "Ctrl+F3"
 5 }
 6 
 7 for i, shortcutText in ipairs(shortcutTexts) do
 8   local shortcut = textToShortCut(shortcutText)
 9 
10   print(shortcutText .. " = " .. tostring(shortcut))
11 end

Main Pages

Core Lua documentation entry points

Lua
Script Engine