catching keypresses

This is a discussion on catching keypresses within the Windows Programming forums, part of the Platform Specific Boards category; Is there any easy way to make it so my program can catch and respond to a certain keypress or ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    43

    catching keypresses

    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

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,853
    A key logger?

    But not the sort of thing I help with.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    43
    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.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,853
    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)?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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:
    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);
    It's as simple as that.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,853
    Don't forget to check the return from the RegisterHotKey() as it will fail (return 0) if that combo is already registered.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    43
    Code:
    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);
    }
    Any help is appreciated, thanks
    Last edited by elmutt; 07-31-2007 at 02:54 PM.

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,853
    >>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)?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    43
    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

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    You are trying to build up a GUI program structure to a console application...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    43
    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()
    Last edited by elmutt; 08-01-2007 at 09:32 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Catching key strokes in a ListView
    By @nthony in forum Windows Programming
    Replies: 1
    Last Post: 07-28-2006, 02:40 AM
  2. Help getting multiple keypresses in a DOS compiler
    By Nongan in forum Game Programming
    Replies: 2
    Last Post: 04-01-2005, 09:07 PM
  3. vVv - Keypresses (again)
    By Skarr in forum Linux Programming
    Replies: 6
    Last Post: 08-06-2002, 06:01 AM
  4. Catching bad input
    By Asmodan in forum C++ Programming
    Replies: 3
    Last Post: 05-28-2002, 06:24 PM
  5. Replies: 0
    Last Post: 12-16-2001, 09:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21