Jump to content
  • Advertisement
Sign in to follow this  
ajm113

Get Key Presses On Console Even When It's Not Focused?

This topic is 3211 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm working on a voice chat on my Winsock chatting app and I was wondering if I can bind a key on the keyboard to a thread running on the console so the user just have to hold a key to talk even when the console/window is not active. Also on the side for bonus points does anyone know a simple app used to playback microphone audio on the speakers using Win32 Multimedia system? I know how to use Fmod, but I'm not sure how to directly get the microphone that's set by default to get the audio from. I know about this tutorial, but I was wondering something a bit more simpler if possible? This tutorial I already know about. If not I'll continue to read through that code and see what I can get going, I was just wondering if someone wrote a tutorial like 90% of where I'm going for then just recording. Thanks, Andrew.

Share this post


Link to post
Share on other sites
Advertisement
There may be a better mechanism for this nowadays, but one simple-enough way is to install a keyboard hook that looks for your hot key and lets the rest pass on through. The good news is that this isn't all that hard and is pretty powerful, but the downside is that you need to create a separate DLL.

Basically, do this in your main program...

Init...
hDllInstance = LoadLibraryEx("your.dll", NULL, 0);
KeyboardProc = GetProcAddress(hDllInstance, "KeyboardProcLL@12");
KeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)KeyboardProc, hDllInstance, 0);

Cleanup...
if(KeyHook)
UnhookWindowsHookEx(KeyHook);

if(hDllInstance)
FreeLibrary(hDllInstance);


Then create a DLL (ie "your.dll") with the usual DLL barebones plus this in it...

LRESULT CALLBACK KeyboardProcLL(int nCode, WPARAM wParam, LPARAM lParam)
{
if(nCode == HC_ACTION)
{
KBDLLHOOKSTRUCT * KeyInfo = (KBDLLHOOKSTRUCT *)lParam;

if(wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)
; //Key is down; Virtual Key Code is in KeyInfo->vkCode
else
; //Key is up; Virtual Key Code is in KeyInfo->vkCode
}

return(CallNextHookEx(NULL, nCode, wParam, lParam));
}


That function gets called whenever a key is pressed or released, similar to a window message handler, but only for keyboard events in this case. You'll want to detect your modifier key and create a global to remember its up/down state, then check for that to be down whenever your hotkey's letter key is being pressed. If you want to block the currently focused program from seeing your hotkey, just omit the call to CallNextHookEx() when your hotkey is used (but continue calling it in other cases).

From there, you'll need to decide how you want the DLL and your main program to communicate that hotkey event, or if you want to just do everything in the DLL and have the main program serve as a simple launcher.

Share this post


Link to post
Share on other sites
Quote:
Original post by Endurion
Wouldn't simply GetAysncKeyState do the trick as well?
Where you have to be careful not to choose a common key.

Then you'd have to poll continuously in the background. RegisterHotKey posts a message to your window/message queue of choice, meaning you don't need to stay awake looking for the keypress.

EDIT: This is of course assuming that the OP's application doesn't already have a non-idling loop.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!