View previous topic :: View next topic |
Author |
Message |
hollow87 Cheater
Reputation: 0
Joined: 07 Feb 2015 Posts: 28
|
Posted: Sat Feb 07, 2015 3:56 am Post subject: Advice on hotkeys for trainers. |
|
|
I was just curious what are the pros and cons of using RegisterHotKey vs hooking the keyboard via SetWindowsHookEx and polling with GetAsyncKeyState when using them for trainer hotkeys.
For example do some work better with certain games than others? |
|
Back to top |
|
 |
Dark Byte Site Admin
Reputation: 470
Joined: 09 May 2003 Posts: 25794 Location: The netherlands
|
Posted: Sat Feb 07, 2015 4:48 am Post subject: |
|
|
registerHotkeys :
con:often don't work with games (they get disabled by the game)
pro: easy to setup and when it works it's fast
SetWindowsHookEx:
con:
complex to setup
runs in every application, and if you're not carefull can crash several applications (e. g 64 bit fpc 2.6 generates exes that crash when any dll does exception handling itself)
And in some cases directx will hijack the keyboard messages so you never receive them
pro:when it works it's fast
getasynckeystate (dislexia is fun, you type completely unrelated apifunctions without realizing):
con: you need to poll a lot, and if not done quick enough will feel like a delay to the user
pro: it can even see mouse buttons and it almost always works _________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping
Last edited by Dark Byte on Mon Feb 09, 2015 6:58 pm; edited 1 time in total |
|
Back to top |
|
 |
hollow87 Cheater
Reputation: 0
Joined: 07 Feb 2015 Posts: 28
|
Posted: Sat Feb 07, 2015 2:24 pm Post subject: |
|
|
Thank you, before you replied I have already implemented RegisterHotKey which works for me at the moment.
But now I think I'm going to be looking into one of the other ones. |
|
Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sun Feb 08, 2015 10:10 pm Post subject: |
|
|
Keep in mind with the suggestions that Dark Byte gave, some can trigger anti-cheats to ban you if your are working with multiplayer games. So you need to be mindful of things like that while developing your cheat.
If worse come to worse, you could always fall back on GetAsyncKeyState which is system-global. _________________
- Retired. |
|
Back to top |
|
 |
hollow87 Cheater
Reputation: 0
Joined: 07 Feb 2015 Posts: 28
|
Posted: Sat Feb 14, 2015 6:48 pm Post subject: |
|
|
Thanks both of you. I have converted my code to use GetAsyncKeyState instead of RegisterHotKey.
I'm using it inside a separate thread running in an infinite loop with Sleep time of about 15ms per iteration of the loop to cut down on the CPU usage of an infinite loop.
At first I was having issues with it detecting the keypress many times when I only pressed it once but I seem to have mitigated that by only handling the key when it is released. |
|
Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
|
Back to top |
|
 |
hollow87 Cheater
Reputation: 0
Joined: 07 Feb 2015 Posts: 28
|
Posted: Sat Feb 14, 2015 7:17 pm Post subject: |
|
|
I'm using C# and I thought about doing it that way, but MSDN does say
Quote: | If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks.
|
And in the remarks section Quote: |
Although the least significant bit of the return value indicates whether the key has been pressed since the last query, due to the pre-emptive multitasking nature of Windows, another application can call GetAsyncKeyState and receive the "recently pressed" bit instead of your application. The behavior of the least significant bit of the return value is retained strictly for compatibility with 16-bit Windows applications (which are non-preemptive) and should not be relied upon. | So I didn't go that route. Here is my current code for it
Code: | private void ThreadProc()
{
while (!shutdown)
{
lock(_lockObj)
{
foreach (var item in hotKeyItems.Values)
{
if ((GetAsyncKeyState(item.Key) & 0x8000) == 0x8000)
{
item.IsKeyDown = true;
}
if(item.Modifiers != KeyModifier.None)
{
if((item.Modifiers & KeyModifier.Alt) == KeyModifier.Alt)
{
if ((GetAsyncKeyState(Keys.Menu) & 0x8000) == 0x8000)
item.ModifersDown |= KeyModifier.Alt;
else
item.ModifersDown -= (item.ModifersDown & KeyModifier.Alt);
}
if ((item.Modifiers & KeyModifier.Ctrl) == KeyModifier.Ctrl)
{
if ((GetAsyncKeyState(Keys.ControlKey) & 0x8000) == 0x8000)
item.ModifersDown |= KeyModifier.Ctrl;
else
item.ModifersDown -= (item.ModifersDown & KeyModifier.Ctrl);
}
if ((item.Modifiers & KeyModifier.Shift) == KeyModifier.Shift)
{
if ((GetAsyncKeyState(Keys.ShiftKey) & 0x8000) == 0x8000)
item.ModifersDown |= KeyModifier.Shift;
else
item.ModifersDown -= (item.ModifersDown & KeyModifier.Shift);
}
if ((item.Modifiers & KeyModifier.Win) == KeyModifier.Win)
{
if ((GetAsyncKeyState(Keys.LWin) & 0x8000) == 0x8000)
item.ModifersDown |= KeyModifier.Win;
else
item.ModifersDown -= (item.ModifersDown & KeyModifier.Win);
if ((item.ModifersDown & KeyModifier.Win) != KeyModifier.Win)
{
if ((GetAsyncKeyState(Keys.RWin) & 0x8000) == 0x8000)
item.ModifersDown |= KeyModifier.Win;
else
item.ModifersDown -= (item.ModifersDown & KeyModifier.Win);
}
}
}
if ((GetAsyncKeyState(item.Key) & 0x8000) != 0x8000 & item.IsKeyDown)
{
if (item.ModifersDown == item.Modifiers)
{
item.IsKeyDown = false;
item.OnHotKeyPressedHandler();
}
}
}
}
Thread.Sleep(15);
}
} |
|
|
Back to top |
|
 |
|