Difference between revisions of "Lua:Class:GenericHotkey"

From Cheat Engine
Jump to navigation Jump to search
(Created page with "Category:GenericHotkey GenericHotkey '''Class''' : ('''Inheritance''': ''Object'') The generic hotkey class returns an object that can manage up to...")
 
(Major overhaul of the post.)
 
Line 1: Line 1:
[[Category:GenericHotkey]]
+
[[Category:Lua]]
GenericHotkey '''Class''' : ('''Inheritance''': ''[[Lua:Class:Object|Object]]'')
+
{{Class|'''class''' GenericHotkey ''':''' Object}}
  
The generic hotkey class returns an object that can manage up to 5 hotkeys.
+
The GenericHotkey class represents a registered hotkey.
  
== Functions ==
+
A GenericHotkey calls its assigned Lua function when the configured key combination is pressed.
; createHotkey(function, keys, ...) : function
 
: Returns an initialized GenericHotkey class object. Maximum of 5 keys.
 
  
; createHotkey(function, {keys, ...}) : function
+
===Inheritance===
: Initializes a hotkey with a maximum of 5 keys.
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Class
 +
!align="left"|Inherits From
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|GenericHotkey
 +
|Object
 +
|A hotkey object that executes a Lua callback when its key combination is pressed.
 +
|}
  
== Properties ==
+
===Creation===
; DelayBetweenActivate : integer
+
{{CodeBox|'''function''' createHotkey(''function'', ''keys'', ''...'') ''':''' GenericHotkey}}
:Interval in milliseconds that determines the minimum time between hotkey activations. If set to 0, the global delay is used.
 
  
; onHotKey : parameter
+
Creates and initializes a GenericHotkey object.
:The function to call when the hotkey is pressed.
 
  
== Methods ==
+
A maximum of 5 keys can be used for one hotkey.
; getKeys() :
+
 
; setKeys(key, ....) :
+
===Function Parameters===
; setOnHotkey(table) :
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
; getOnHotkey :
+
!align="left"|Parameter
 +
!align="left"|Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|function
 +
|Function
 +
|The Lua function that is called when the hotkey is pressed.
 +
|-
 +
|keys
 +
|Integer
 +
|The first key in the hotkey combination.
 +
|-
 +
|...
 +
|Integer (optional)
 +
|Additional keys in the hotkey combination. A maximum of 5 keys can be used.
 +
|}
 +
 
 +
===Alternative Creation Syntax===
 +
{{CodeBox|'''function''' createHotkey(''function'', ''{keys, ...}'') ''':''' GenericHotkey}}
 +
 
 +
Creates and initializes a GenericHotkey object using a table of keys.
 +
 
 +
===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
 +
|-
 +
|function
 +
|Function
 +
|The Lua function that is called when the hotkey is pressed.
 +
|-
 +
|{keys, ...}
 +
|Table
 +
|A table containing the key combination. A maximum of 5 keys can be used.
 +
|}
 +
 
 +
===Returns===
 +
GenericHotkey — The created and initialized GenericHotkey object.
 +
 
 +
===Properties===
 +
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Property
 +
!align="left"|Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|DelayBetweenActivate
 +
|Integer
 +
|Interval in milliseconds that determines the minimum time between hotkey activations. If 0, the global delay is used.
 +
|-
 +
|onHotkey
 +
|Function
 +
|The function to call when the hotkey is pressed.
 +
|}
 +
 
 +
===Methods===
 +
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Method
 +
!align="left"|Return Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|getKeys()
 +
|Table
 +
|Returns the currently assigned key combination.
 +
|-
 +
|setKeys(key, ...)
 +
|void
 +
|Sets the hotkey keys. A maximum of 5 keys can be used.
 +
|-
 +
|setOnHotkey(table)
 +
|void
 +
|Sets the OnHotkey callback handler.
 +
|-
 +
|getOnHotkey()
 +
|Function
 +
|Returns the current OnHotkey callback handler.
 +
|}
 +
 
 +
===Examples===
 +
<pre>
 +
local hotkey = createHotkey(function()
 +
  print("Hotkey pressed")
 +
end, VK_F1)
 +
</pre>
 +
 
 +
<pre>
 +
local hotkey = createHotkey(function()
 +
  print("Ctrl + F1 pressed")
 +
end, VK_CONTROL, VK_F1)
 +
 
 +
hotkey.DelayBetweenActivate = 1000
 +
</pre>
 +
 
 +
<pre>
 +
local hotkey = createHotkey(function()
 +
  print("Shift + Alt + F2 pressed")
 +
end, {VK_SHIFT, VK_MENU, VK_F2})
 +
</pre>
 +
 
 +
<pre>
 +
local hotkey = createHotkey(function()
 +
  print("Original callback")
 +
end, VK_F3)
 +
 
 +
hotkey.setOnHotkey(function()
 +
  print("Updated callback")
 +
end)
 +
</pre>
 +
 
 +
<pre>
 +
local hotkey = createHotkey(function()
 +
  print("F4 pressed")
 +
end, VK_F4)
 +
 
 +
local key1, key2, key3, key4, key5 = hotkey.getKeys()
 +
 
 +
print("Key 1: " .. tostring(key1))
 +
print("Key 2: " .. tostring(key2))
 +
print("Key 3: " .. tostring(key3))
 +
print("Key 4: " .. tostring(key4))
 +
print("Key 5: " .. tostring(key5))
 +
</pre>
 +
 
 +
<pre>
 +
local hotkey = createHotkey(function()
 +
  print("Ctrl + Alt + F4 pressed")
 +
end, VK_CONTROL, VK_MENU, VK_F4)
 +
 
 +
local keys = { hotkey.getKeys() }
 +
 
 +
for i, key in ipairs(keys) do
 +
  print(i .. ": " .. tostring(key))
 +
end
 +
</pre>
 +
 
 +
<pre>
 +
local hotkey = createHotkey(function()
 +
  print("Old hotkey")
 +
end, VK_F5)
 +
 
 +
hotkey.setKeys(VK_CONTROL, VK_F5)
 +
</pre>
 +
 
 +
{{LuaSeeAlso}}

Latest revision as of 14:47, 25 June 2026

{} Class

class GenericHotkey : Object

The GenericHotkey class represents a registered hotkey.

A GenericHotkey calls its assigned Lua function when the configured key combination is pressed.

Inheritance[edit]

Class Inherits From Description
GenericHotkey Object A hotkey object that executes a Lua callback when its key combination is pressed.

Creation[edit]

<> Lua API Reference

function createHotkey(function, keys, ...) : GenericHotkey

Creates and initializes a GenericHotkey object.

A maximum of 5 keys can be used for one hotkey.

Function Parameters[edit]

Parameter Type Description
function Function The Lua function that is called when the hotkey is pressed.
keys Integer The first key in the hotkey combination.
... Integer (optional) Additional keys in the hotkey combination. A maximum of 5 keys can be used.

Alternative Creation Syntax[edit]

<> Lua API Reference

function createHotkey(function, {keys, ...}) : GenericHotkey

Creates and initializes a GenericHotkey object using a table of keys.

Function Parameters[edit]

Parameter Type Description
function Function The Lua function that is called when the hotkey is pressed.
{keys, ...} Table A table containing the key combination. A maximum of 5 keys can be used.

Returns[edit]

GenericHotkey — The created and initialized GenericHotkey object.

Properties[edit]

Property Type Description
DelayBetweenActivate Integer Interval in milliseconds that determines the minimum time between hotkey activations. If 0, the global delay is used.
onHotkey Function The function to call when the hotkey is pressed.

Methods[edit]

Method Return Type Description
getKeys() Table Returns the currently assigned key combination.
setKeys(key, ...) void Sets the hotkey keys. A maximum of 5 keys can be used.
setOnHotkey(table) void Sets the OnHotkey callback handler.
getOnHotkey() Function Returns the current OnHotkey callback handler.

Examples[edit]

local hotkey = createHotkey(function()
  print("Hotkey pressed")
end, VK_F1)
local hotkey = createHotkey(function()
  print("Ctrl + F1 pressed")
end, VK_CONTROL, VK_F1)

hotkey.DelayBetweenActivate = 1000
local hotkey = createHotkey(function()
  print("Shift + Alt + F2 pressed")
end, {VK_SHIFT, VK_MENU, VK_F2})
local hotkey = createHotkey(function()
  print("Original callback")
end, VK_F3)

hotkey.setOnHotkey(function()
  print("Updated callback")
end)
local hotkey = createHotkey(function()
  print("F4 pressed")
end, VK_F4)

local key1, key2, key3, key4, key5 = hotkey.getKeys()

print("Key 1: " .. tostring(key1))
print("Key 2: " .. tostring(key2))
print("Key 3: " .. tostring(key3))
print("Key 4: " .. tostring(key4))
print("Key 5: " .. tostring(key5))
local hotkey = createHotkey(function()
  print("Ctrl + Alt + F4 pressed")
end, VK_CONTROL, VK_MENU, VK_F4)

local keys = { hotkey.getKeys() }

for i, key in ipairs(keys) do
  print(i .. ": " .. tostring(key))
end
local hotkey = createHotkey(function()
  print("Old hotkey")
end, VK_F5)

hotkey.setKeys(VK_CONTROL, VK_F5)

Main Pages

Core Lua documentation entry points

Lua
Script Engine