Click here to Skip to main content
       

Windows API

 
AnswerRe: How to write a Windows terminal server PinmemberBernhard Hiller10-May-12 4:19 
QuestionEvent Log and RenderingInfo in a Spanish environment PinmemberKevin Derhak27-Mar-12 4:26 
QuestionRegConnectRegistry : Access Denied Pinmembersan8022-Feb-12 23:43 
AnswerRe: RegConnectRegistry : Access Denied PinprotectorPete O'Hanlon22-Feb-12 23:47 
GeneralRe: RegConnectRegistry : Access Denied Pinmembersan8023-Feb-12 3:09 
GeneralRe: RegConnectRegistry : Access Denied PinprotectorPete O'Hanlon23-Feb-12 3:59 
GeneralRe: RegConnectRegistry : Access Denied Pinmembersan8024-Feb-12 1:31 
QuestionSendInput() does not work after GetMessage() Pinmembersashok_bg9-Feb-12 2:46 
Hello everyone,
 
I was trying to make an application, that registers a hotkey and when the hotkey is pressed, simulates a keyboard event or mouse click.
 
To do so, I have chosen to use the RegisterHotkey(), SendInput() and GetMessage() windows functions.
 
The problem is that as soon as I declare a call to GetMessage() - SendInput() stops working.
 
In the code below, I made an experiment - the program is waiting for 3 seconds (to give me some time) and as soon as I start it, I bring notepad to focus and the "KeyDown(97)" types "1" in the notepad window.
 
However, this behavior does not work when the MessageLoop() is entered.
 
Thank you for your help.
 
Best Regards,
 
Alex
 
 
#define _WIN32_WINNT 0x0500
 
#include "stdafx.h"
#include <Windows.h>

 
//This function simulates a keyboard button press and release. Argument is VirtualKey
//This function works fine, before the GetMessage() function is declared/called
void KeyDown(int vk){
 
	printf("Called KeyDown with parameter %d \n",vk);
 
	INPUT	Input[1];
 
	Input[0].type  = INPUT_KEYBOARD;
	Input[0].ki.wVk= vk;
 
	printf("Sending Input\n");
	SendInput(1, Input, sizeof(Input));
}
 

//This function is blocking and waiting for a message to arrive
void MessageLoop(){
	MSG msg = {0};
	while (GetMessage(&msg, NULL, 0, 0) != 0)
    {
 
		//if the arrived message is a hotkey event
        if (msg.message == WM_HOTKEY)
        {
			//some debugging
			printf("msg.message %d, msg.lParam %d, msg.wParam %d \n",msg.message,msg.lParam,msg.wParam);
 
			//press the numeric '1' on the keyboard - doesn't work
			KeyDown(97);
        }
 
		TranslateMessage(&msg);
		DispatchMessage(&msg);
    }
}
 
int main(int argc, char* argv[])
{
 
	//This part works, only when the GetMessage() function is in a separate function ex: MessageLoop----
	Sleep(3000);
	KeyDown(97);
 
	//Register an ALT-CTR-A hotkey with ID - 1. First argument is NULL to make the hotkey global (it is a hWnd)
	RegisterHotKey(
		NULL,
		1,
		MOD_CONTROL | MOD_ALT,
		0x41
	);  //0x41 is 'a'

	//Enter the message loop that blocks and waits for a message to arrive for this application

	//Note that if I put the 'while (GetMessage(&msg, NULL, 0, 0) != 0)' code here, 
	//instead of putting it in a separate function, 
	//the 'KeyDown(97)' call on the 2nd line of the main() function will not work !
	MessageLoop();
 
	//clean up
	UnregisterHotKey(NULL,1);
 
	return 0;
}
 

AnswerRe: SendInput() does not work after GetMessage() PinmemberDhiraj Pallin18-Feb-12 16:25 
GeneralRe: SendInput() does not work after GetMessage() Pinmembersashok_bg20-Feb-12 21:30 
AnswerRe: SendInput() does not work after GetMessage() PinmemberJohnCz27-Apr-12 9:24 
QuestionQuestion about (CX?) destructor? PinmemberSuper Lloyd24-Jan-12 4:19 
AnswerRe: Question about (CX?) destructor? PinmemberAlbert Holguin3-Feb-12 5:18 
AnswerRe: Question about (CX?) destructor? PinmemberAescleal14-Apr-12 21:22 
QuestionThe Metro Tile expands: and then ... what ? PinmemberBillWoodruff13-Jan-12 0:35 
AnswerRe: The Metro Tile expands: and then ... what ? PinprotectorPete O'Hanlon13-Jan-12 0:56 
GeneralRe: The Metro Tile expands: and then ... what ? PinmemberBillWoodruff13-Jan-12 6:07 
AnswerRe: The Metro Tile expands: and then ... what ? PinmemberJeremy Hutchinson18-Jan-12 8:33 
GeneralRe: The Metro Tile expands: and then ... what ? PinmemberBillWoodruff2-Feb-12 13:59 
QuestionLoadLibrary error 127 on W7 X64 [modified] PinmemberDusanDusan13-Nov-11 7:31 
AnswerRe: LoadLibrary error 127 on W7 X64 PinmemberRichard Andrew x6419-Jan-12 9:46 
GeneralRe: LoadLibrary error 127 on W7 X64 PinmemberDusanDusan19-Jan-12 21:03 
QuestionMetro interaction with another application running in Win8 ? PinmemberBillWoodruff16-Oct-11 18:10 
AnswerRe: Metro interaction with another application running in Win8 ? PinmvpNishant Sivakumar17-Oct-11 5:16 
GeneralRe: Metro interaction with another application running in Win8 ? PinmemberBillWoodruff17-Oct-11 15:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 2 Aug 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid