Dynamically Assign Hotkeys?

Anything Cheat Engine related, bugs, suggestions, helping others, etc..
Post Reply
User avatar
gibberishh
Table Makers
Table Makers
Posts: 331
Joined: Fri Jul 02, 2021 5:48 pm
Reputation: 225

Dynamically Assign Hotkeys?

Post by gibberishh »

I'm trying to assign hotkeys to a cheat via Lua. The cheat exists as a memory record in a table, NOT a trainer. The hotkey is supposed to set a value of that record.

Usage: My memory record points to spells in a game. The game doesn't allow keyboard keys for switching between spells. I want to use CE's hotkey feature to assign keyboard keys to spell ids.
Why Lua: Yes, I can assign several keys to the memory record and hard set each one to apply a specific value. In fact, this already works. However, I want to load the keys from an ini file so that my table is more distributable. I upload signed tables, so I can't ask users to assign their own keys if they want different keys mapped to different spells.

I have code that loads, reads and splits the ini into {[T]=23,[I]=5},.... I now face 2 problems:
1. How to transform T and I into the respective VK values -- NOT as "VK084" (string) but as VK084 (constant).
2. How to assign the hotkey so that T sets the memrec value to 23, I sets it to 5 and so on.

Ideally, the memrec will have 0 assigned hotkeys until this function is called. Any help will be appreciated.

Thanks!

User avatar
EpicBirdi
Fearless Donors
Fearless Donors
Posts: 64
Joined: Sat Jul 21, 2018 2:22 pm
Reputation: 58

Re: Dynamically Assign Hotkeys?

Post by EpicBirdi »

Code: Select all

function setHotkey(memrec,hotkeys,mode,value,description)
  local value = value or nil
  local description = description or nil
  local mode = mode or 0
  local al = getAddressList()
  for i=0,al.Count-1 do
    if al[i].Description == memrec then
      al[i].createHotkey(hotkeys,mode,value,description)
    end
  end
end
--T: 84
--I: 73
setHotkey('MyMemrecDescription',{84},5,23,'T sets value to 23')
setHotkey('MyMemrecDescription',{73},5,5,'I sets value to 5')

--if you want to use multiple keys for one hotkey (like shift+T):
setHotkey('MyMemrecDescription',{16,84},5,23,'Shift+T sets value to 23')

-- Modes, from celua.txt
--    mrhToggleActivation(0): Toggles between active/deactive
--    mrhToggleActivationAllowIncrease(1): Toggles between active/deactive. Allows increase when active
--    mrhToggleActivationAllowDecrease(2): Toggles between active/deactive. Allows decrease when active
--    mrhActivate(3): Sets the state to active
--    mrhDeactivate(4):  Sets the state to deactive
--    mrhSetValue(5):  Sets a specific value to the value properyy (see value)
--    mrhIncreaseValue(6):  Increases the current value with the value property (see value)
--    mrhDecreaseValue(7):  Decreases the current value with the value property (see value)

-- Script to find Hotkeys' IDs:
function setDebugHotkey(memrec)
  local al = getAddressList()
  for i=0,al.Count-1 do
    if al[i].Description == memrec then
      for o=0,222 do
        al[i].createHotkey({o},mrhToggleActivation)
      end
    end
  end
end

setDebugHotkey('debug')

--It'll set every possible hotkey from ID #0 (blank) to the final registerable key, #222 under whatever memrec you want, so you can figure out the IDs to put in the array of keys if you need them, or for combinations.

User avatar
gibberishh
Table Makers
Table Makers
Posts: 331
Joined: Fri Jul 02, 2021 5:48 pm
Reputation: 225

Re: Dynamically Assign Hotkeys?

Post by gibberishh »

Thanks a bunch. I'll set to incorporating this in my code right away. I have an array of virtual key Ids from viewtopic.php?p=213386#p213386. I'll be able to easily reverse that array for this function. I was hoping there would be something a little more straightforward than a lookup or loop, but if that's what I gotta do, that's what I'll do. :)
Your name 'Epic' is justified!

GreenHouse
Expert Cheater
Expert Cheater
Posts: 857
Joined: Fri Oct 12, 2018 10:25 pm
Reputation: 889

Re: Dynamically Assign Hotkeys?

Post by GreenHouse »

gibberishh wrote:
Wed Sep 29, 2021 11:38 am
Thanks a bunch. I'll set to incorporating this in my code right away. I have an array of virtual key Ids from viewtopic.php?p=213386#p213386. I'll be able to easily reverse that array for this function. I was hoping there would be something a little more straightforward than a lookup or loop, but if that's what I gotta do, that's what I'll do. :)
Your name 'Epic' is justified!
You could always create a form, get the key pressed, and set it to that.
Create form, add edit boxes for each cheat, use OnKeyPressed, get key from it, then set the hotkey to that. Also, 'createHotkey' does allow using variables, so the variable could be set when OnKeyPressed.

User avatar
gibberishh
Table Makers
Table Makers
Posts: 331
Joined: Fri Jul 02, 2021 5:48 pm
Reputation: 225

Re: Dynamically Assign Hotkeys?

Post by gibberishh »

GreenHouse wrote:
Wed Sep 29, 2021 12:23 pm
You could always create a form, get the key pressed, and set it to that.
Create form, add edit boxes for each cheat, use OnKeyPressed, get key from it, then set the hotkey to that. Also, 'createHotkey' does allow using variables, so the variable could be set when OnKeyPressed.
True, but I was looking for something specific to tables. I'm allergic to forms. That is, I haven't figured out how to build one successfully yet. My experience with forms dates back to Visual Foxpro and Visual Basic days, when one could simply 'connect' a control to a database field without doing anything beyond. MSJet or ODBC would take care of everything inbetween. I still have to learn how to link memrecs to controls in CE.

Plus, most of the tables I have created are huge. They have 30-50 cheats each. When I start hacking into a game, I go to town. I don't have the patience to go about building forms for them... every time I start on a form project, I think of something else I can hack in the game and I'm right back into memory view and assembler! :D

User avatar
EpicBirdi
Fearless Donors
Fearless Donors
Posts: 64
Joined: Sat Jul 21, 2018 2:22 pm
Reputation: 58

Re: Dynamically Assign Hotkeys?

Post by EpicBirdi »

As for making it more "straightforward" you'd still need the memrec's ID to assign it something with Lua automatically, so the loop/lookup method is most ideal, I feel.
Otherwise you can just write out

Code: Select all

memrec.createHotkey(...)
in a script and it'll work on itself. You can reference children and parent memrecs from the same manner, but it's not much different than just supplying it in the main table form, especially if you're reading from file.

User avatar
gibberishh
Table Makers
Table Makers
Posts: 331
Joined: Fri Jul 02, 2021 5:48 pm
Reputation: 225

Re: Dynamically Assign Hotkeys?

Post by gibberishh »

EpicBirdi wrote:
Wed Sep 29, 2021 9:42 pm
As for making it more "straightforward" you'd still need the memrec's ID to assign it
I think you misunderstood me :?. My comment was regarding converting ASCII characters into virtual keys (and looking up an array of VK_ values to do so). Some languages have built-in asc() and chr() functions -- or something like it. Or maybe some variation of String.Format() or even a generic format() that treats input strings and parameters in special ways to do the same conversion.

I have no issues with traversing through memrecs using Lua (or picking out single ones via id, desc or parent-child nests). :)

User avatar
EpicBirdi
Fearless Donors
Fearless Donors
Posts: 64
Joined: Sat Jul 21, 2018 2:22 pm
Reputation: 58

Re: Dynamically Assign Hotkeys?

Post by EpicBirdi »

You can use the above debug function I provided to parse through regular keys to their integer counterparts manually, but here's something for VK_ keys. You could just create another table for IDs to regular keys if you need it for whatever reason, but CE only really needs their literal ID or VK_ counterparts.

Code: Select all

function invertLookup(t, value)
  for k,v in pairs(t) do
    if v == value then return k end
  end
  return nil
end

VKLookup = {["VK_LBUTTON"] = 1,["VK_RBUTTON"] = 2,["VK_XBUTTON1"] = "VK_XBUTTON1",["VK_XBUTTON2"] = "VK_XBUTTON2",["VK_CANCEL"] = 3,["VK_MBUTTON"] = 4,["VK_BACK"] = 8,["VK_TAB"] = 9,["VK_CLEAR"] = 12,["VK_RETURN"] = 13,["VK_SHIFT"] = 16,["VK_CONTROL"] = 17,["VK_MENU"] = 18,["VK_PAUSE"] = 19,["VK_CAPITAL"] = 20,["VK_ESCAPE"] = 27,["VK_SPACE"] = 32,["VK_PRIOR"] = 33,["VK_NEXT"] = 34,["VK_END"] = 35,["VK_HOME"] = 36,["VK_LEFT"] = 37,["VK_UP"] = 38,["VK_RIGHT"] = 39,["VK_DOWN"] = 40,["VK_SELECT"] = 41,["VK_PRINT"] = 42,["VK_EXECUTE"] = 43,["VK_SNAPSHOT"] = 44,["VK_INSERT"] = 45,["VK_DELETE"] = 46,["VK_HELP"] = 47,["VK_0"] = 48,["VK_1"] = 49,["VK_2"] = 50,["VK_3"] = 51,["VK_4"] = 52,["VK_5"] = 53,["VK_6"] = 54,["VK_7"] = 55,["VK_8"] = 56,["VK_9"] = 57,["VK_A"] = 65,["VK_B"] = 66,["VK_C"] = 67,["VK_D"] = 68,["VK_E"] = 69,["VK_F"] = 70,["VK_G"] = 71,["VK_H"] = 72,["VK_I"] = 73,["VK_J"] = 74,["VK_K"] = 75,["VK_L"] = 76,["VK_M"] = 77,["VK_N"] = 78,["VK_O"] = 79,["VK_P"] = 80,["VK_Q"] = 81,["VK_R"] = 82,["VK_S"] = 83,["VK_T"] = 84,["VK_U"] = 85,["VK_V"] = 86,["VK_W"] = 87,["VK_X"] = 88,["VK_Y"] = 89,["VK_Z"] = 90,["VK_LWIN"] = 91,["VK_RWIN"] = 92,["VK_APPS"] = 93,["VK_NUMPAD0"] = 96,["VK_NUMPAD1"] = 97,["VK_NUMPAD2"] = 98,["VK_NUMPAD3"] = 99,["VK_NUMPAD4"] = 100,["VK_NUMPAD5"] = 101,["VK_NUMPAD6"] = 102,["VK_NUMPAD7"] = 103,["VK_NUMPAD8"] = 104,["VK_NUMPAD9"] = 105,["VK_MULTIPLY"] = 106,["VK_ADD"] = 107,["VK_SEPARATOR"] = 108,["VK_SUBTRACT"] = 109,["VK_DECIMAL"] = 110,["VK_DIVIDE"] = 111,["VK_F1"] = 112,["VK_F2"] = 113,["VK_F3"] = 114,["VK_F4"] = 115,["VK_F5"] = 116,["VK_F6"] = 117,["VK_F7"] = 118,["VK_F8"] = 119,["VK_F9"] = 120,["VK_F10"] = 121,["VK_F11"] = 122,["VK_F12"] = 123,["VK_F13"] = 124,["VK_F14"] = 125,["VK_F15"] = 126,["VK_F16"] = 127,["VK_F17"] = 128,["VK_F18"] = 129,["VK_F19"] = 130,["VK_F20"] = 131,["VK_F21"] = 132,["VK_F22"] = 133,["VK_F23"] = 134,["VK_F24"] = 135,["VK_NUMLOCK"] = 144,["VK_SCROLL"] = 145,["VK_LSHIFT"] = 160,["VK_LCONTROL"] = 162,["VK_LMENU"] = 164,["VK_RSHIFT"] = 161,["VK_RCONTROL"] = 163,["VK_RMENU"] = 165}

print(VKLookup["VK_SCROLL"])
--Look up ID by string
print(invertLookup(VKLookup, 47))
--Look up string by ID

User avatar
gibberishh
Table Makers
Table Makers
Posts: 331
Joined: Fri Jul 02, 2021 5:48 pm
Reputation: 225

Re: Dynamically Assign Hotkeys?

Post by gibberishh »

EpicBirdi wrote:
Thu Sep 30, 2021 10:08 am
You can use the above debug function I provided to parse through regular keys to their integer counterparts manually, but here's something for VK_ keys. You could just create another table for IDs to regular keys if you need it for whatever reason, but CE only really needs their literal ID or VK_ counterparts.
Thanks again. I already have the code to lookup keycodes in the array. Very similar to yours. I was only clarifying the earlier thread and explaining why I assumed such a lookup should not be necessary. I've got my ini -> spellid -> hotkey code working as expected, thanks to your first reply. Don't make me feel bad about using up your time unnecessarily! :D

User avatar
EpicBirdi
Fearless Donors
Fearless Donors
Posts: 64
Joined: Sat Jul 21, 2018 2:22 pm
Reputation: 58

Re: Dynamically Assign Hotkeys?

Post by EpicBirdi »

It's no issue, I love helping people out haha. Glad you've got it sorted at least!

Post Reply

Who is online

Users browsing this forum: No registered users