Cheat Engine

Main

Forum

About Cheat Engine

About DBVM

Bugtracker

Downloads

Tutorials

GIT

Lua Extensions

Twitter

FAQ

Contribute

Cheat Engine Wiki



Become a Patron! Check it out



Know programming?
Looking for a job?
Try patreon!





Privacy Policy
Contact


Tutorials: Auto Assemble: Keypress

A question that sometimes pops up is people asking to have a cheat's effect only happen while a key is pressed.
If you're using auto assembler scripts, this is something you can do fairly easy yourself.

First you save the state of the program just to be sure(pushad/popad) , then you push the key you want to check on top of the stack and call GetAsyncKeystate.
Then evaluate the return value in AX and check if the key was pressed and/or is pressed
bit 15 is 1 if it is CURRENTLY down
bit 0 is 1 if it has been pressed since the last time you called this function

if the key is not pressed, then just jump over all your game modifying code(e.g: Just let it execute the hp decreasing code) and return to the caller, but if it IS pressed, do what you usually do. (set values, skip over the decrease hp routine, etc...)

example:
origin:
jmp mycode
exit:

mycode:
pushad //I have no idea what registers get modified by GetAsyncKeystate (my guess eax,ebx,ecx but I hate guesing)
pushfd //always a good idea to save the flags

...
push 'X' ;key to watch, for special keys, check out google for virtual key codes
call GetAsyncKeyState
//bit 15 is 1 if it is CURRENTLY down
//bit 0 is 1 if it has been pressed down since last time it was hecked
//to check bit 0, do:
//and ax,1 //and mask with 0000000000000001
//cmp ax,1
//jne notpressedsincelasttime

//note, this is just to keep it simple, there are more optimized ways to do it, but more confusing as well (e.g using AND)
shr ax,#15 //shift bits in the AX register to the right and fill the left side with 0's, so 1000000000000000 changes to 0000000000000001 and since it's on a 16 bit register, there's no bit beyond bit 15
cmp ax,1 //if bit 15 was set to 1 ax now contains the value of either 1 or 0. 1 meaning it's pressed
jne notpressed

//it's pressed
...
...do whatever you want when the key is pressed (e.g. mov [ecx+24],#100 to set health to 100)
...

notpressed:
//cleanup
popfd
popad

originalcodeandothercleanupstuff:
...
jmp exit