Click here to Skip to main content
12,898,392 members (55,482 online)

Windows API

 
AnswerRe: How to write a Windows terminal server Pin
Bernhard Hiller10-May-12 4:19
memberBernhard Hiller10-May-12 4:19 
QuestionEvent Log and RenderingInfo in a Spanish environment Pin
Kevin Derhak27-Mar-12 4:26
memberKevin Derhak27-Mar-12 4:26 
QuestionRegConnectRegistry : Access Denied Pin
san8022-Feb-12 23:43
membersan8022-Feb-12 23:43 
AnswerRe: RegConnectRegistry : Access Denied Pin
Pete O'Hanlon22-Feb-12 23:47
protectorPete O'Hanlon22-Feb-12 23:47 
GeneralRe: RegConnectRegistry : Access Denied Pin
san8023-Feb-12 3:09
membersan8023-Feb-12 3:09 
GeneralRe: RegConnectRegistry : Access Denied Pin
Pete O'Hanlon23-Feb-12 3:59
protectorPete O'Hanlon23-Feb-12 3:59 
GeneralRe: RegConnectRegistry : Access Denied Pin
san8024-Feb-12 1:31
membersan8024-Feb-12 1:31 
QuestionSendInput() does not work after GetMessage() Pin
sashok_bg9-Feb-12 2:46
membersashok_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() Pin
Dhiraj Pallin18-Feb-12 16:25
memberDhiraj Pallin18-Feb-12 16:25 
GeneralRe: SendInput() does not work after GetMessage() Pin
sashok_bg20-Feb-12 21:30
membersashok_bg20-Feb-12 21:30 
AnswerRe: SendInput() does not work after GetMessage() Pin
JohnCz27-Apr-12 9:24
memberJohnCz27-Apr-12 9:24 
QuestionQuestion about (CX?) destructor? Pin
Super Lloyd24-Jan-12 4:19
memberSuper Lloyd24-Jan-12 4:19 
AnswerRe: Question about (CX?) destructor? Pin
Albert Holguin3-Feb-12 5:18
memberAlbert Holguin3-Feb-12 5:18 
AnswerRe: Question about (CX?) destructor? Pin
Aescleal14-Apr-12 21:22
memberAescleal14-Apr-12 21:22 
QuestionThe Metro Tile expands: and then ... what ? Pin
BillWoodruff13-Jan-12 0:35
memberBillWoodruff13-Jan-12 0:35 
AnswerRe: The Metro Tile expands: and then ... what ? Pin
Pete O'Hanlon13-Jan-12 0:56
protectorPete O'Hanlon13-Jan-12 0:56 
GeneralRe: The Metro Tile expands: and then ... what ? Pin
BillWoodruff13-Jan-12 6:07
memberBillWoodruff13-Jan-12 6:07 
AnswerRe: The Metro Tile expands: and then ... what ? Pin
Jeremy Hutchinson18-Jan-12 8:33
memberJeremy Hutchinson18-Jan-12 8:33 
GeneralRe: The Metro Tile expands: and then ... what ? Pin
BillWoodruff2-Feb-12 13:59
memberBillWoodruff2-Feb-12 13:59 
QuestionLoadLibrary error 127 on W7 X64 Pin
DusanDusan13-Nov-11 7:31
memberDusanDusan13-Nov-11 7:31 
AnswerRe: LoadLibrary error 127 on W7 X64 Pin
Richard Andrew x6419-Jan-12 9:46
memberRichard Andrew x6419-Jan-12 9:46 
GeneralRe: LoadLibrary error 127 on W7 X64 Pin
DusanDusan19-Jan-12 21:03
memberDusanDusan19-Jan-12 21:03 
QuestionMetro interaction with another application running in Win8 ? Pin
BillWoodruff16-Oct-11 18:10
memberBillWoodruff16-Oct-11 18:10 
AnswerRe: Metro interaction with another application running in Win8 ? Pin
Nishant Sivakumar17-Oct-11 5:16
mvpNishant Sivakumar17-Oct-11 5:16 
GeneralRe: Metro interaction with another application running in Win8 ? Pin
BillWoodruff17-Oct-11 15:21
memberBillWoodruff17-Oct-11 15:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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
Web02 | 2.8.170424.1 | Last Updated 6 Apr 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid