holy_spirit

I was searching and collect information about how to make keylogger, then I found a topic in codeProject talking about that
http://www.codeproject.com/cs/system/simple_key_log.asp

there is a block of code talking about get the keys that pressed on the keyBoard and store it in a buffer
and that happend only when the function returns -32767
Code Block

foreach(System.Int32 i in Enum.GetValues(typeof(Keys)))

  {
if(GetAsyncKeyState(i) == -32767)
{
keyBuffer += Enum.GetName(typeof(Keys), i) + " ";
}
}


so can you explain what is the -32767 number
Thank you.


Re: Visual C# General what -32767 means ?

Peter Ritchie

GetAsyncKeyState is documented as returning a value with the most significant bit set if the key was pressed (which would be 0x8000). It is also documented as returning a value with the least significant bit set if the key was pressed after the last call to GetAsyncKeyState (which would be 0x8001); but it also documents you shouldn't rely on that. 0x8001 in decimal is -32767. If all you want to test is whether the key is pressed, I would simply compare against negativity. For example:

Code Block

if(GetAsyncKeyState(i) < 0)

{

// key is pressed

}






Re: Visual C# General what -32767 means ?

Zamial

Hmm if it does equal -32767 it indicates a key or selection of keys has been pressed and released since you last called GetAsyncKeyState however it is not 100% guaranteed to produce the results you would want from a perfect key logger.

So he is looping through every key and checking if the key has been pressed since he last checked, if it is then he writes to the buffer.

I guess the timer must be rapid enough it only ever finds 1 key has been pressed or else you would lose the order. Unless the enum also returns the keys in the order they were pressed. My understanding of this is not 100% Smile.

I never wrote one.

That number is special.





Re: Visual C# General what -32767 means ?

holy_spirit

All Clear
Thank you




Re: Visual C# General what -32767 means ?

holy_spirit

back with some questions.
after creating small logger, I want to know some keys current state (worked or not, such as CaosLock, NumLock, ScrollLock ...)
I searched for sth that can help me out, but nothing useful.
any suggestion
Thank you





Re: Visual C# General what -32767 means ?

holy_spirit

make a property that tells you if the shift key is pressed or not

Code Block

public bool IsShifted
{

get

{

if (GetAsyncKeyState(keys.ShiftKey) <0)
return true;


return false;
}
}