I know there is a question for windows forms but it doesnt work in the console, or at least i couldnt get it to work. I need to capture key presses even though the console doesnt have focus
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
You can create a global keyboard hook in a console application, too.
Here's complete, working code:
http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx
You create a console application, but must add a reference to System.Windows.Forms for this to work. There's no reason a console app can't reference that dll.
I just created console app using this code and verified that it gets each key pressed, whether or not the console app has the focus.
EDIT
The main thread will run Application.Run() until the application exits, e.g. via a call to Application.Exit(). The simplest way to do other work is to start a new Task to perform that work. Here's a modified version of Main() from the linked code that does this
NOTE
Possibly provide a means to exit the Console app e.g. when a special key combo is pressed depending on your specific needs. In the