Is there any easy way to make it so my program can catch and respond to a certain keypress or keypress combination while it is running in the background?
I have MSVC2005 and vista ultimate
Thanks
Printable View
Is there any easy way to make it so my program can catch and respond to a certain keypress or keypress combination while it is running in the background?
I have MSVC2005 and vista ultimate
Thanks
A key logger?
But not the sort of thing I help with.
haha nono. Thats rstupid.
I'm writing a small simple media player I want to be able to control from the outside while playing a video game etc.
I assume its pretty tricky but who knows. I wanna learn, getting back into programming after a long break.
The problem with that is the game will have focus and will get the key presses.
You would have to capture all key strokes and broadcast them to your app.
Thus your media player would have to distingush between normal key presses (and not respond) and key presses it should respond to.
How do you plan to overcome this issue (which it should respond to)?
Well if it's a media player, then you only need to receive some certain keypresses.
You can set up hotkeys with RegisterHotKey(), handle them through WM_HOTKEY and remove them with UnregisterHotKey().
Here's an example:
It's as simple as that.Code://register your hotkeys in WinMain or in WM_CREATE, whereever you like
//the window must be created already because you need the handle to it
//you can use a random ID you want, that will be received as wParam in
//WM_HOTKEY notification
RegisterHotKey(hwnd,1,MOD_ALT|MOD_SHIFT,VK_UP); //ALT+SHIFT+UP
RegisterHotKey(hwnd,2,MOD_ALT|MOD_SHIFT,VK_DOWN); //ALT+SHIFT+DOWN
//this is in your message handler
case WM_HOTKEY:{
switch(wParam){
case 1:
//ALT+SHIFT+UP pressed
break;
case 2:
//ALT+SHIFT+DOWN pressed
break;
}
break;
}
//whenever you want to unregister the hotkeys (probably when closing the program)
UnregisterHotKey(hwnd,1);
UnregisterHotkey(hwnd,2);
Don't forget to check the return from the RegisterHotKey() as it will fail (return 0) if that combo is already registered.
Any help is appreciated, thanksCode:
Anything u see that im doing obviously wrong? It returns 0 registering the key combo but it shoudlnt already be registered and it does it for different combos.
int main()
{
char ebuff[100];
MSG msg;
HWND CurrentWindow;
if(Initialize()!=0)
allegro_exit();
LoadLibrary("user32.dll");
CurrentWindow=win_get_window();
allegro_message("%d", RegisterHotKey(CurrentWindow,1,MOD_ALT|MOD_SHIFT,VK_UP));
// std::string szCommand = "open \"test.mp3\" type mpegvideo alias test";
// std::string szCommand = "open \"hurtdog.wav\" type waveaudio alias test";
// mciGetErrorString(mciSendString(szCommand.c_str(), NULL, 0, 0), ebuff, 100);
// allegro_message("%s", ebuff);
// std::string szCommand2 = "play test from 0";
// mciSendString(szCommand2.c_str(), NULL, 0, 0);
while(!key[KEY_ESC])
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message==WM_HOTKEY&&msg.wParam==1)
allegro_message("hello");
TranslateMessage(&msg);
DispatchMessage(&msg);
}
ProcessInput();
Display();
if(BUTTON[EXITBUTTON].clonk==YES)
break;
}
UnregisterHotKey(CurrentWindow,1);
allegro_exit();
return 0;
}
END_OF_MAIN();
// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// sort through and find what code to run for the message given
switch(message)
{
case WM_HOTKEY:
{
switch(wParam)
{
case 1:
allegro_message("hello");
break;
}
break;
}
}
// Handle any messages the switch statement didn't
return DefWindowProc (hWnd, message, wParam, lParam);
}
>>It returns 0 registering the key combo but it shoudlnt already be registered and it does it for different combos.
Some other app has already registered that hot key combo (so yours can't)?
Does all the windows related stuff looks good like how i did the message handling?
Been playing with it quite a bit cant seem to get it to work
You are trying to build up a GUI program structure to a console application...
It only appears that way. That end of main thing makes allegro turn it into winmain and compile correctly. Just gotta trust me, its a windows app.
I would write winmain with all its weird arguments and define something like ALLEGRO_NO_MAIN_MAGIC and it would work the same.
Just pretend it says winmain()