CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2003
    Posts
    938
    Hey.
    Code:
    if (nCode == HC_ACTION)
    	{
    		KBDLLHOOKSTRUCT* kbh=(KBDLLHOOKSTRUCT*)lParam;
    		switch(kbh->vkCode)
    		{
    			case 0x41:
    				{
    					INPUT bKey;
    					bKey.type=INPUT_KEYBOARD;
    					KEYBDINPUT kbinp;
    					kbinp.dwExtraInfo=0;
    					kbinp.dwFlags=0;
    					kbinp.time=0;
    					kbinp.wScan=0;
    					kbinp.wVk=0x42;
    					bKey.ki=kbinp;
    					SendInput(1,&bKey,sizeof(INPUT));
    					return 0;
    				}
    		};
    	}
        return CallNextHookEx(NULL, nCode, wParam, lParam);
    Now the Hook proc gets called alrite and all, however i want to modify the input once it was hooked.
    The above code definetly does not work, so i was wondering how can i modify the input that i hook? basically what happens if i press 'a' is i get 'bab' as output.
    Thx
    Last edited by Quell; February 18th, 2007 at 01:56 PM.

  2. #2
    Join Date
    Aug 2005
    Posts
    478

    Re: SetWindowsHookEx->Modding keyboard input

    Basically, that is an initial keydown notification, and you send a b, and then when you callnexthook the window manager still interprets the data as an a, and then you get a keyup and you send a b anyways. You can fix this by sending the b only when it's a keydown, and not calling callnexthookex when it is a keydown you are replacing. I think.
    Windows XP, Visual Studio 2008, SVN

  3. #3
    Join Date
    Aug 2003
    Posts
    938

    Re: SetWindowsHookEx->Modding keyboard input

    eh...i am still having a problem...now i am getting ba:
    Code:
    if (nCode == HC_ACTION)
    	{
    		if(wParam== WM_KEYDOWN)
    		{
    			KBDLLHOOKSTRUCT* kbh=(KBDLLHOOKSTRUCT*)lParam;
    			switch(kbh->vkCode)
    			{
    				case 0x41:
    				{
    						keybd_event(0x42,0,0,0);
    						return 0;
    				}
    			};
    		}
    		else
    		{
    			KBDLLHOOKSTRUCT* kbh=(KBDLLHOOKSTRUCT*)lParam;
    			switch(kbh->vkCode)
    			{
    				case 0x41:
    				{
    						return 0;
    				}
    			};
    		}
    	}
    	return CallNextHookEx(NULL, nCode, wParam, lParam);
    Any idea why?

  4. #4
    Join Date
    Aug 2005
    Posts
    478

    Re: SetWindowsHookEx->Modding keyboard input

    It's possible that you can not do this with WH_KEYBOARD_LL hooks then, and that stopping the hook chain doesn't make the computer forget that it's there.
    Windows XP, Visual Studio 2008, SVN

  5. #5
    Join Date
    Aug 2003
    Posts
    938

    Re: SetWindowsHookEx->Modding keyboard input

    Hmm...is there another way to hook the keyboard input then?I don't want to start injecting code, because that would be versino dependent, so any other possibilites, maybe something in ring 0?

  6. #6
    Join Date
    May 2005
    Posts
    4,954

    Re: SetWindowsHookEx->Modding keyboard input

    Quote Originally Posted by Quell
    Hmm...is there another way to hook the keyboard input then?I don't want to start injecting code, because that would be versino dependent, so any other possibilites, maybe something in ring 0?
    Did you try to simply replace the data you receive in the hook, and call ::CallNextHookEx() with the swapped key, instead of generating keystroke in the hook?

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  7. #7
    Join Date
    Aug 2003
    Posts
    938

    Re: SetWindowsHookEx->Modding keyboard input

    yep, does nothing
    Code:
    if (nCode == HC_ACTION)
    	{
    		if(wParam == WM_KEYDOWN)
    		{
    			KBDLLHOOKSTRUCT* kbh=(KBDLLHOOKSTRUCT*)lParam;
    			switch(kbh->vkCode)
    			{
    				case 0x41:
    				{
    					/*keybd_event(0x42,0,0,0);
    					return 0;*/
    					kbh->vkCode=0x42;
    				}
    			};
    		}
    		/*else
    		{
    			KBDLLHOOKSTRUCT* kbh=(KBDLLHOOKSTRUCT*)lParam;
    			switch(kbh->vkCode)
    			{
    				case 0x41:
    				{
    					return 0;
    				}
    			};
    		}*/
    
    	}
    	return CallNextHookEx(NULL, nCode, wParam, lParam);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured