Alex
Alex

Reputation: 45

C++ GetAsyncKeyState and GetCursorPos vs Windows Messaging

I am working on a application that involves remote control. The keyboard and mouse state gets updated about 100 times a second, saved on arrays, sent on the internet, and reproduced. Perfect reproducing timing is required. Since now I only coded the keyboard part and it was actually easier to program than windows messaging. All I had to do is call GetAsyncKeystate every 9 milliseconds on the host, and then, on the client, use SendInput every 9 milliseconds to get perfect timing. The other side of the medal is, I will have to manually check if the host window is highlighted, and if is not, avoid calling GetAsyncKeyState. But now that i'm about to code the mouse part, I have a doubt about what method to use, since perfect timing for mouse will be difficult to achieve even without window messaging. That's why I am asking to programmers that are more experienced than me: In this case, is it better to use a combination of GetAsyncKeyState and GetCursorPos or is it better to use Windows Messaging? What are the positives and negatives of both? Thanks in advance.

Upvotes: 0

Views: 839

Answers (2)

CodeBlocks
CodeBlocks

Reputation: 253

GetAsyncKeyState and GetCursorPos is your best choice. The simpler the code, the faster your program will be able to be. Considering that sending windows messages through functions like SendMessage() gives you the option to fiddle with a lot of different aspects of different programs, this usually means that there are more processes happening inside the SendMessage() function and that speed is taken away in order to provide practicality for other applications.

I would also like to point out that you will never be able to get perfect timing since you are in fact collecting data from the source computer, passing it to an array and sending it online.

Summary: Pros of using GetAsyncKeyState and GetCursorPos: You won't find any functions retrieving data faster than these functions given that they are functions that only look to do small basic tasks. SendMessage type functions will be slightly slower since they have more coding inside them, allowing them to work with more than just mouse and keyboard functionality.

Cons... I don't really see any.

Upvotes: 0

You'll want to use a Windows hook. See SetWindowsHookEx and related documentation. This can be used for keyboard events as well.

On recent Windows versions there is also a newer, asynchronous input capture technology available whose name escapes me at the moment. Google for that as well.

EDIT:

I remember now: The other technology is known as event hooking. See the SetWinEventHook function.

Upvotes: 1

Related Questions