Resolved Key Intercept

RukusDM

Member
Joined
May 12, 2020
Messages
5
Programming Experience
1-3
Hello. Im building a voice recognition program (Which is working) that will do certain functions in a game, but i'd like to have a key that will activate recognition, then disable the recognition when it is released. For example the Left Control Key. Many years back i did this in c++, however i cant recall how it was done.
Im using C# in Microsoft Visual Studio 2019 (free version). Any help is appreciated.
 
Sorry, i wasn't clear. The application would be minimized and not have focus. The voice stuff works fine, i just need to get over this hurdle. Thanks
 
additionally, it is a c# windows forms application. I did this quite a few years back for Flight Simulator, where you would talk to your virtual copilot to set autopilot altitude, speed, heading etc. Also you could call for a checklist for startup, taxi and pre-takeoff. Its been so many years, I've lost the code lol.
 
What you're asking is contradicting. You are saying you want to use voice recognition to perform tasks while in game. Irrelevant what those tasks are... you then say you want to do it when the game window has no focus or is not the parent window to the screen. Whats the point of that?

If you want to use it in game, then you would need to get the handle of the game window, hook it and listen for key strokes from the game window and send it back to your form. This would be done from your own C# winforms app.
 
I think what our OP was doing was calling the Win32AP GetAsyncKeyState() in C++ and likely just PInvoking the same from within WinForms. If I'm not mistaken this API can be called and it'll give keyboard state even if the calling program doesn't have the input focus. The next alternative is to install a global keyboard hook -- which was common during the Windows XP days, but have become harder to get working with tighter Windows security.

Anyway, my understanding of the OP's intent is that he is looking for basically a walkie-talkie button -- you push it down to talk -- just like some modern VOIP systems for games.
 
OP's app could provide a listener I guess, and listen for keystrokes in the game window, but this is done by hooking. I may stand corrected here but I don't think this is possible by using a specific API without hooking, at least it wasn't possible a few years ago when I was plugging a DirectX based app for my old job. Where I was required to hook the window, and listen for key events from that fullscreen application. I'd need to dig up the folder from when i worked on this project to see what API's I used. (If I still have it.)
 
Here's a quick demo:
C#:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ConsoleTest
{
    class ConsoleShell
    {
        [DllImport("user32.dll")]
        static extern short GetAsyncKeyState(Keys vKey);

        public static void Main()
        {
            do
            {
                if (IsKeyPressed(Keys.LShiftKey))
                    Console.WriteLine($"{DateTime.Now}: LSHIFT pressed");

                if (IsKeyPressed(Keys.RShiftKey))
                    break;
            } while (true);

            bool IsKeyPressed(Keys key) => (GetAsyncKeyState(key) & 0x8000) != 0;
        }
    }
}

Compile and run the console program above. Satisfy yourself that keyboard the Left Shift key presses are being detected. No put the focus on another window and start type. When you press the left shift key, the console program will still report the keypress even though the window does not have the input focus.
 
Thanks guys for your input. I'll look at the code. In my previous app it didn't depend on which other app had focus, it just received keystroke information and didn't have focus. As i had mentioned, i don't recall how id did it then (8 to 10 years ago). What id like to do is just look for a particular key press and activate the voice recognition while that key is held down.
 
Here's a quick demo:
C#:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ConsoleTest
{
    class ConsoleShell
    {
        [DllImport("user32.dll")]
        static extern short GetAsyncKeyState(Keys vKey);

        public static void Main()
        {
            do
            {
                if (IsKeyPressed(Keys.LShiftKey))
                    Console.WriteLine($"{DateTime.Now}: LSHIFT pressed");

                if (IsKeyPressed(Keys.RShiftKey))
                    break;
            } while (true);

            bool IsKeyPressed(Keys key) => (GetAsyncKeyState(key) & 0x8000) != 0;
        }
    }
}

Compile and run the console program above. Satisfy yourself that keyboard the Left Shift key presses are being detected. No put the focus on another window and start type. When you press the left shift key, the console program will still report the keypress even though the window does not have the input focus.

Thank you! That did the trick :)
 
Back
Top Bottom