Global HotKeys

I am trying to make global hotkeys for a program that moves your mouse to a spot on your screen and makes it stay there. Although when I start it by pressing the key combination ALT+S it starts well but then I cannot turn it off with the combination ALT+A. I know it is because of that while loop I use to make the move continually back to that spot but, I have no idea as to what I can do to make it work like I want it to. Could anyone enlighten me with the proper way to do this? It would be greatly appreciated. Here is my code:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "stdafx.h"


void MouseMove (int x, int y )
{  
	double fScreenWidth    = ::GetSystemMetrics( SM_CXSCREEN )-1; 
	double fScreenHeight  = ::GetSystemMetrics( SM_CYSCREEN )-1; 
	double fx = x*(65535.0f/fScreenWidth);
	double fy = y*(65535.0f/fScreenHeight);
	INPUT  Input={0};
	Input.type      = INPUT_MOUSE;
	Input.mi.dwFlags  = MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE;
	Input.mi.dx = fx;
	Input.mi.dy = fy;
	::SendInput(1,&Input,sizeof(INPUT));
}


int WINAPI WinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine,
				   int nCmdShow)
{           
	if (RegisterHotKey(
		NULL,
		1,
		MOD_ALT,
		0x53))  //0x53 is 's'
	{

	}

	MSG msg1 = {0};
	while (GetMessage(&msg1, NULL, 0, 0) == 1)
	{
		if (msg1.message == WM_HOTKEY)
		{
			while (true)
			{
				MouseMove(100, 100);
			}
		}
	} 

	if (RegisterHotKey(
		NULL,
		2,
		MOD_ALT,
		0x41))  //0x41 is 'a'
	{

	}

	MSG msg2 = {0};
	while (GetMessage(&msg2, NULL, 0, 0) == 2)
	{
		if (msg2.message == WM_HOTKEY)
		{
			return 0;
		}
	} 

	return 0;
}



stdafx.h
1
2
3
4
5
6
7
#pragma once


#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <winuser.h> 
you could use threading or peek for a message, and if noone is provided continue remove the mouse (would not use this one)...

you also could just disable the user input for the mouse^^...
Why can't you put a simple interrupt in your while routine?
I want the mouse to stay where I put it for until the user presses ALT+A to make it stop so I don't want an interrupt in the while loop.

@Incubbus I don't understand you
The purpose of having the interrupt in the while() routine is to be able to trap the keypress. If you code it properly it won't interrupt UNTIL the key is pressed, thus the mouse will stay where it was until the press takes place.
I changed my main code to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "stdafx.h"


void MouseMove (int x, int y )
{  
	double fScreenWidth    = ::GetSystemMetrics( SM_CXSCREEN )-1; 
	double fScreenHeight  = ::GetSystemMetrics( SM_CYSCREEN )-1; 
	double fx = x*(65535.0f/fScreenWidth);
	double fy = y*(65535.0f/fScreenHeight);
	INPUT  Input={0};
	Input.type      = INPUT_MOUSE;
	Input.mi.dwFlags  = MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE;
	Input.mi.dx = fx;
	Input.mi.dy = fy;
	::SendInput(1,&Input,sizeof(INPUT));
}


int WINAPI WinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine,
				   int nCmdShow)
{           
	if (RegisterHotKey(
		NULL,
		1,
		MOD_ALT,
		0x53))  //0x53 is 's'
	{

	}

	MSG msg1 = {0};
	while (GetMessage(&msg1, NULL, 0, 0) == 1)
	{
		if (msg1.message == WM_HOTKEY)
		{
			if (RegisterHotKey(
				NULL,
				2,
				MOD_ALT,
				0x41))  //0x41 is 'a'
			{

			}
			while (true)
			{
				MouseMove(100, 100);
				MSG msg2 = {0};
				while (GetMessage(&msg2, NULL, 0, 0) == 2)
				{
					if (msg2.message == WM_HOTKEY)
					{
						return 0;
					}
				} 
			}
		}
	} 



	return 0;
}
But now it just moves to 100, 100 when I press ALT+S then stops without me even pressing ALT+A. I have no idea how to do it correctly. This just stumps me.
I don't see a way to escape from your first while(true) statement. I'm also not overly familiar with key events, so maybe someone else can see something.
Topic archived. No new replies allowed.