Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Advice on hotkeys for trainers.

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
hollow87
Cheater
Reputation: 0

Joined: 07 Feb 2015
Posts: 28

PostPosted: Sat Feb 07, 2015 3:56 am    Post subject: Advice on hotkeys for trainers. Reply with quote

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
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 470

Joined: 09 May 2003
Posts: 25794
Location: The netherlands

PostPosted: Sat Feb 07, 2015 4:48 am    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
hollow87
Cheater
Reputation: 0

Joined: 07 Feb 2015
Posts: 28

PostPosted: Sat Feb 07, 2015 2:24 pm    Post subject: Reply with quote

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
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Sun Feb 08, 2015 10:10 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
hollow87
Cheater
Reputation: 0

Joined: 07 Feb 2015
Posts: 28

PostPosted: Sat Feb 14, 2015 6:48 pm    Post subject: Reply with quote

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
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Sat Feb 14, 2015 6:53 pm    Post subject: Reply with quote

If you are coding in C++, a simple trick for handling the key would be like this:
Code:
for (;;)
{
    if (::GetAsyncKeyState(VK_F1) & 1)
        DoSomethingHere();
    ::Sleep(10);
}


You can read more on the specific bits of the return value here:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646293%28v=vs.85%29.aspx

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
hollow87
Cheater
Reputation: 0

Joined: 07 Feb 2015
Posts: 28

PostPosted: Sat Feb 14, 2015 7:17 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites