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 


Check/Toggle hotkey script

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Tutorials -> Auto Assembler tutorials
View previous topic :: View next topic  
Author Message
DaSpamer
Grandmaster Cheater Supreme
Reputation: 28

Joined: 13 Sep 2011
Posts: 1475

PostPosted: Thu Apr 04, 2019 9:58 am    Post subject: Check/Toggle hotkey script Reply with quote

32 Bit version (64 should be easy to done).

Code:
call _GetAsyncKeyState(VK)

with a given virtual key, sets eax to 0 or 1 (where 1 is pressed);

Code:
call _ToggleHotkey(VK,VaribleADDR)

with given virtualkey and some address to change state (0 to 1 or 1 to 0);
It saves last call status in _GetAsyncKeyStack


I use this for either applying actions when key is down (_GetAsyncKeyState),
or to apply everytime key state has changed.

Don't forget to backup registers and flags (pushfd pushad, popad popfd)

Code:
globalalloc(_GetAsyncKeyStack,255) // hotkey toggle memory stack (Up and down states)
label(_GetAsyncKeyState)
label(_ToggleHotkey)

_GetAsyncKeyState:
   push ebp
   mov ebp,esp // stack
   push ecx

   push [ebp+8]
   call GetAsyncKeyState
    test eax,eax

            je @F

      mov eax,1 //Force it to 1, as we only care if it's pressed or not

@@: // clean up registers
   pop ecx
   pop ebp
ret 4 // returns eax == 1 or eax == 0


_ToggleHotkey:
   push ebp
   mov ebp,esp // stack
   push ebx
   push edx
   mov ebx,[ebp+8]
   push ebx
   //push [ebp+8]
   call _GetAsyncKeyState
   mov dl,byte ptr [_GetAsyncKeyStack+ebx] // last tick state
   cmp al,dl // compare new state versus old state

            je @F // state has not changed so exit


      mov byte [_GetAsyncKeyStack+ebx],al // save new state (as it has changed)
      test al,al // test if key is 0

               je @F // exit if 0

         mov ebx,1
         mov edx,[ebp+c]
         sub ebx,[edx] // quick status swap... variable = 0 -> 1 - 0 = 1 | variable = 1 -> 1-1 = 0
         mov [edx],ebx

@@:
   pop edx
   pop ebx
   pop ebp
ret 8


some example of using
Code:
(...)
globalalloc(hook,512)
label(hookHeader)

label(active)
label(UselessCounter)

label(MoreHotkeysFunction)

label(originalcode)
label(CleanExit)

label(returnToCode)
(...)
hook:
   //jmp backupRegisters
hookHeader:
   push active // push pointer to globalVar
   push 60    // Decimal -> 96 VK_NUMPAD0
   call _ToggleHotkey
   // call _GetAsyncKeyState
   // test eax,eax // check if mouse down -> eax == result, if eax == 0 quit
   
   cmp [active],0
         je @F
         
      mov [ecx+50],999 // upon pressing once VK_NUMPAD0
      
      //call MoreHotkeysFunction
         
@@:
jmp CleanExit


MoreHotkeysFunction:
   push activeLootVac
   push 64
   call _ToggleHotkey

   // toggle Enemies vac
   push activeEnemiesVac
   push 65
   call _ToggleHotkey

   // toggle projectile vac
   push activeProjectileVac
   push 66
   call _ToggleHotkey
   
   push 66 // VK_NUMPAD6
   call _GetAsyncKeyState
   test eax,eax
   je @F // key state is not pressed
      mov eax,[UselessCounter] // key is pressed so as long as it pressed inc useless counter
      inc eax
      mov [UselessCounter],eax
   
}
@@:
ret

CleanExit:
   //jmp restoreRegisters
   
originalcode:
   mov eax,[ecx+50]
jmp returnToCode


active:
dd 0
UselessCounter:
dd 0
         
         
"game.exe"+A2BE7E: // should be frequent called function, otherwise you will have to time your key press according to game events
jmp hook
nop
nop
nop
returnToCode:

_________________
HEY Hitler
Do you get lazy when making trainers?
Well no more!
My CETrainer will generate it for you in seconds, so you won't get lazy! Very Happy

http://forum.cheatengine.org/viewtopic.php?t=564919
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Tutorials -> Auto Assembler tutorials 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