Dealing with the Kinect SDK is not difficult - on the Internet you can quickly find a lot of training videos, which details the process of creating a Kinect application and handling key points.
The first problem I encountered is that not all applications, especially games, work with a standard message loop. It is about solving this problem and will be discussed in this article.
If windowed applications run on a message loop, then games often read clicks directly from devices. Accordingly, the solution to the problem would be to write your own driver, but it would take too much time, especially if there is no experience in creating drivers, but we are completely interested in something else.
GlovePIE is a standalone application with its own scripting language, allowing you to immediately write emulators for various devices, be it a keyboard or a joystick. The second feature of this application is that it also works with a message loop, which allows us to emulate keystrokes using the PostMessage function.
')
We need a small and utterly obvious script, since it will process messages from our application, and not direct clicks:
A = A
S = S
Space = Space
We save it under any name with the extension .PIE, then it will be useful to us when running GlovePIE in the code. The next step is to write your own class in C #, which will send messages to the program, thereby emulating pressing the appropriate keys.
using System; using System.Diagnostics; using System.Threading; using System.Runtime.InteropServices; class KeyEmulator { const UInt32 WM_KEYDOWN = 0x0100; const UInt32 WM_KEYUP = 0x0101; Process GlovePIE; public KeyEmulator() { if(GlovePIE == null) { GlovePIE = new Process(); GlovePIE.StartInfo = new ProcessStartInfo(“path_to_glovepie\\PIEFree.exe”, “-emulator.PIE”); if(!GlovePIE.Start()) throw new Exception(“Can't start emulator process”); } } }
In the constructor, you can add additional initialization parameters or even by default assume that the necessary script is running and only join the process. It remains to add a function to the class that would send a signal:
[DllImport("user32.dll")] static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam); public bool KeyDown(int key_id) { bool result = PostMessage(GlovePIE.MainWindowHandle, WM_KEYDOWN, (IntPtr)key_id, (IntPtr)0); Thread.Sleep(99); return result; }
You must specify the key code that we want to emulate. The message will be sent to GlovePIE, and that, in turn, will create an emulation of a direct keystroke on the keyboard.
Perhaps it doesn’t look so beautiful from the outside: a third-party program that will need to be run along with the main application. But still it is a simple and affordable way to send messages to any applications.