Difference between revisions of "Lua:Class:GenericHotkey"
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: | + | [[Category:Lua]] |
| − | + | {{Class|'''class''' GenericHotkey ''':''' Object}} | |
| − | The | + | The GenericHotkey class represents a registered hotkey. |
| − | + | A GenericHotkey calls its assigned Lua function when the configured key combination is pressed. | |
| − | |||
| − | |||
| − | ; | + | ===Inheritance=== |
| − | + | {|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. | ||
| + | |} | ||
| − | == | + | ===Creation=== |
| − | + | {{CodeBox|'''function''' createHotkey(''function'', ''keys'', ''...'') ''':''' GenericHotkey}} | |
| − | |||
| − | + | Creates and initializes a GenericHotkey object. | |
| − | |||
| − | == Methods == | + | A maximum of 5 keys can be used for one hotkey. |
| − | ; getKeys() | + | |
| − | + | ===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 | ||
| + | |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
The GenericHotkey class represents a registered hotkey.
A GenericHotkey calls its assigned Lua function when the configured key combination is pressed.
Contents
Inheritance[edit]
| Class | Inherits From | Description |
|---|---|---|
| GenericHotkey | Object | A hotkey object that executes a Lua callback when its key combination is pressed. |
Creation[edit]
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]
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)