VK_SHIFT sticks down till i press it manually

This is a discussion on VK_SHIFT sticks down till i press it manually within the Windows Programming forums, part of the Platform Specific Boards category; okay well im working on something where i need to use VK_SHIFT well ill show u what i have right ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    7

    Angry VK_SHIFT sticks down till i press it manually

    okay well im working on something where i need to use VK_SHIFT well ill show u what i have right now...
    Code:
    #include <windows.h>           // Win32 API function support
    
    
    
    INT APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nShowCmd)
    {
    	// TODO: Now we will perform what we want to do 
    	{
    	    HWND hParent = FindWindow(NULL, "Untitled - Notepad");  // Find window
    	    if (hParent)     // Valid window handle?
    		{
    		    SetForegroundWindow(hParent);    // Show the window
    		    SetFocus(hParent);     // Set window focus to new window
    		    // Loop through twenty-five
    		    for (int l = 0; l < 25; l++) 
    			{
    
    	      keybd_event(VK_SHIFT, NULL, KEYEVENTF_EXTENDEDKEY, NULL);    // Key down
    	      keybd_event(0x32, NULL, KEYEVENTF_EXTENDEDKEY, NULL);    // Key down
    		         keybd_event(0x32, NULL, KEYEVENTF_KEYUP, NULL);          // Key up
    				 		         keybd_event(VK_SHIFT, NULL, KEYEVENTF_KEYUP, NULL);          // Key up
    			}
    		}
    		else 
    			return 0;      // Invalid window handle; exit
    	}
    
    
    
    	return 0;
    }

    yeah and that will go to a notepad thingy and type a bunch of @@@@@@@@@ and umm then shift is still being held down like if i type something after the program ends shift is stuck down untill i hit it again and i was wondering how to fix this?

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    keybd_event(VK_SHIFT, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, NULL);
    As per the MSDN sample.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    huh? i need it to do shift then A then turn shift off but it just keeps shift on forever!

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Don't you hate it when people ignore your advice? Do exactly what anonytmouse said:

    Code:
    	      keybd_event(VK_SHIFT, NULL, KEYEVENTF_EXTENDEDKEY, NULL);    // Key down
    	      keybd_event(0x32, NULL, NULL, NULL);    // Key down
    		  keybd_event(0x32, NULL, KEYEVENTF_KEYUP, NULL);          // Key up
    		  keybd_event(VK_SHIFT, NULL, KEYEVENTF_KEYUP|KEYEVENTF_EXTENDEDKEY, NULL);          // Key up
    Note: I also removed the extended key flag from the second line, it's not necessary.

    I also think it's a good idea to use FindWindow() with the class name rather than the window text:

    Code:
    HWND hParent = FindWindow("Notepad", NULL);  // Find window
    EDIT: You also don't need to specify extended key for SHIFT, I think that just makes it right shift instead of left shift. So, the following is fine:

    Code:
    keybd_event(VK_SHIFT, NULL, NULL, NULL);
    keybd_event(0x32, NULL, NULL, NULL);
    keybd_event(0x32, NULL, KEYEVENTF_KEYUP, NULL);
    keybd_event(VK_SHIFT, NULL, KEYEVENTF_KEYUP, NULL);
    Last edited by bennyandthejets; 10-18-2003 at 06:31 PM.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    thansks! and sorry i didnt mean to ignore him, i just didnt know where to put that, :/ im kinda new to this thanks!

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    No probs. Good luck with the programming.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manually programming Press any key in vis C++
    By vanceypants in forum C++ Programming
    Replies: 1
    Last Post: 06-06-2002, 04:23 AM

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