Click here to Skip to main content
15,885,695 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to be able to get keyboard input from games. (I.e. Warcraft 3)
Then based on this input, send keyboard commands back to the game. The game is fullscreen.
I am currently using GetAsyncKeyState() on a timer to tell me what key was last pressed. But then I cant tell if the same key has been pressed twice because the last key pushed will remain the same.
I need to add an event listener somehow for when a key is pushed, instead of a timer to solve that problem, but don't know how.

To send keys back, I was trying to use SendKeys(), but found out it only works on windows related applications?
I don't know if SendInput() is the same?
Sorry if this doesn't make sense, I've tried lots of googling, and it is frustrating.
Posted
Comments
[no name] 7-Jul-12 10:25am    
Non windows and you are using C#? First thing you need to do is forget C# and start learning C++. Then once you have mastered C++ then start learning about global keyhooks.
Sergey Alexandrovich Kryukov 7-Jul-12 11:35am    
This is not really about the language, but yes, it's good to know OS basics, native platform, and there is no a way to use a global hook in .NET only. I explain it in my answer.
--SA
Member 8791890 7-Jul-12 10:43am    
I should have been more clear, I am using windows 7. I meant the program itself doesn't have forms and doesn't accept messages from SendKeys(). If I do I get the error, SendKeys cannot run inside this application because the application is not handling Windows messages. Don't really fancy learning another language.
[no name] 7-Jul-12 13:53pm    
It is a windows program. What else do you think it could be?

Regardless you cannot install a global hook using .NET.

1 solution

No, SendInput is not the same, by far. In brief, SendKeys is very limited (I would say, it's pretty much useless, typically used by some loosers as a workaround on the same application they write if they don't know how to write UI in a legitimate way :-)). As to the SendInput, it is very low-level. When you call it, the input is dispatched to some active window, no matter what it is.

You can easily use this function in C# through P/Invoke, because all the hard work is already done for you. Here:
http://www.pinvoke.net/default.aspx/user32/SendInput.html[^].

The approach alternative to P/Invoke is using C++/CLI. You can create a mixed-mode (managed+unmanaged) project, use any C++ unmanaged API, wrap all the unmanaged code in C++/CLI "ref" or "value" classes/structures and make the public. The output of such project can be uses as a regular .NET assembly, for example, referenced by your C# project.

I hope you understand that using raw Windows API through P/Invoke or C++/CLI breaks platform compatibility of your application.

[EDIT]

You can install a Windows Hook in C# project, but you cannot install it as a global hook. According to Microsoft documentation, the hook should be installed in a separate DLL. It should be an unmanaged DLL, of course.

—SA
 
Share this answer
 
v2
Comments
Member 8791890 7-Jul-12 13:02pm    
I don't think most games listen to the Windows API?
Sergey Alexandrovich Kryukov 7-Jul-12 22:50pm    
Who knows what do you think... :-)
I have no idea what does it mean: "to listen to the API". I a game is written for Windows, it uses Windows API, how else?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900