Click here to Skip to main content
15,885,067 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: "Shift+Delete" and "Ctrl+X" Pin
Member 802179617-Mar-14 8:02
Member 802179617-Mar-14 8:02 
GeneralRe: "Shift+Delete" and "Ctrl+X" Pin
Richard MacCutchan17-Mar-14 8:06
mveRichard MacCutchan17-Mar-14 8:06 
AnswerRe: "Shift+Delete" and "Ctrl+X" Pin
Theo Buys18-Mar-14 2:10
Theo Buys18-Mar-14 2:10 
GeneralRe: "Shift+Delete" and "Ctrl+X" Pin
Member 802179618-Mar-14 10:12
Member 802179618-Mar-14 10:12 
QuestionLow-Level Keyboard Input Detection Pin
Don Guy13-Mar-14 7:20
Don Guy13-Mar-14 7:20 
AnswerRe: Low-Level Keyboard Input Detection Pin
Richard MacCutchan13-Mar-14 22:11
mveRichard MacCutchan13-Mar-14 22:11 
AnswerRe: Low-Level Keyboard Input Detection Pin
Rage14-Mar-14 4:37
professionalRage14-Mar-14 4:37 
AnswerRe: Low-Level Keyboard Input Detection Pin
Software_Developer16-Mar-14 3:32
Software_Developer16-Mar-14 3:32 
For Windows, here is a global key hook example, prints pressed keycodes to the console. It even works when it's in the background.

And this is a Windows discussion board, also ask in Linux, Android and IOS sections for answers..

C++
#include <windows.h>
#include <stdio.h>

HHOOK hKeyboardHook;

__declspec(dllexport) LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam)
{
 	DWORD SHIFT_key=0;
	DWORD CTRL_key=0;
    DWORD ALT_key=0;

	if  ((nCode == HC_ACTION) &&   ((wParam == WM_SYSKEYDOWN) ||  (wParam == WM_KEYDOWN)))      
	{
		KBDLLHOOKSTRUCT hooked_key = 	*((KBDLLHOOKSTRUCT*)lParam);
		DWORD dwMsg = 1;
		dwMsg += hooked_key.scanCode << 16;
		dwMsg += hooked_key.flags << 24;
		char lpszKeyName[1024] = {0};
		lpszKeyName[0] = '[';

		int i = GetKeyNameText(dwMsg,	(lpszKeyName+1),0xFF) + 1;
		lpszKeyName[i] = ']';

		int key = hooked_key.vkCode;

		SHIFT_key = GetAsyncKeyState(VK_SHIFT);
		CTRL_key = GetAsyncKeyState(VK_CONTROL);
        ALT_key = GetAsyncKeyState(VK_MENU);

		if (key >= 'A' && key <= 'Z')   mmmmmmcjmcfkgkgcgkj.j.g     
		{
			 
			if  (GetAsyncKeyState(VK_SHIFT)>= 0) key +=32;

			if (CTRL_key !=0 && key == 'y' )
			{
               MessageBox(NULL, "CTRL-y was pressed\nLaunch your app here", "H O T K E Y", MB_OK); 
               CTRL_key=0;
			}

			if (CTRL_key !=0 && key == 'q' )
			{
				MessageBox(NULL, "Shutting down", "H O T K E Y", MB_OK); 
               PostQuitMessage(0);
			}



  
			printf("key = %c\n", key);

			SHIFT_key = 0;
			CTRL_key = 0;
			ALT_key = 0;

		}

		printf("lpszKeyName = %s\n",  lpszKeyName );
	}
	return CallNextHookEx(hKeyboardHook,	nCode,wParam,lParam);
}

void MessageLoop()
{
	MSG message;
	while (GetMessage(&message,NULL,0,0)) 
	{
		TranslateMessage( &message );
		DispatchMessage( &message );
	}
}

DWORD WINAPI my_HotKey(LPVOID lpParm)
{
	HINSTANCE hInstance = GetModuleHandle(NULL);
	if (!hInstance) hInstance = LoadLibrary((LPCSTR) lpParm); 
	if (!hInstance) return 1;

	hKeyboardHook = SetWindowsHookEx ( 	WH_KEYBOARD_LL, (HOOKPROC) KeyboardEvent,  	hInstance,  NULL    );
	MessageLoop();
	UnhookWindowsHookEx(hKeyboardHook);
	return 0;
}

int main(int argc, char** argv)
{
	HANDLE hThread;
	DWORD dwThread;

	hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE) 	my_HotKey, (LPVOID) argv[0], NULL, &dwThread);

    //ShowWindow(FindWindowA("ConsoleWindowClass", NULL), false);

	if (hThread) return WaitForSingleObject(hThread,INFINITE);
	else return 1;

}

GeneralRe: Low-Level Keyboard Input Detection Pin
Don Guy17-Mar-14 5:44
Don Guy17-Mar-14 5:44 
QuestionMFC Application has stopped working, Server 2008 R2 Pin
MacRaider413-Mar-14 2:42
MacRaider413-Mar-14 2:42 
AnswerRe: MFC Application has stopped working, Server 2008 R2 Pin
David Crow13-Mar-14 4:19
David Crow13-Mar-14 4:19 
GeneralRe: MFC Application has stopped working, Server 2008 R2 Pin
MacRaider413-Mar-14 4:29
MacRaider413-Mar-14 4:29 
AnswerRe: MFC Application has stopped working, Server 2008 R2 Pin
Richard Andrew x6413-Mar-14 7:51
professionalRichard Andrew x6413-Mar-14 7:51 
QuestionHow to unload WinHttp library? Pin
MarxF12-Mar-14 8:34
professionalMarxF12-Mar-14 8:34 
AnswerRe: How to unload WinHttp library? Pin
jschell12-Mar-14 11:12
jschell12-Mar-14 11:12 
GeneralRe: How to unload WinHttp library? Pin
MarxF12-Mar-14 11:40
professionalMarxF12-Mar-14 11:40 
GeneralRe: How to unload WinHttp library? Pin
Richard MacCutchan12-Mar-14 22:23
mveRichard MacCutchan12-Mar-14 22:23 
GeneralRe: How to unload WinHttp library? Pin
David Crow13-Mar-14 2:26
David Crow13-Mar-14 2:26 
AnswerRe: How to unload WinHttp library? Pin
Jochen Arndt12-Mar-14 22:46
professionalJochen Arndt12-Mar-14 22:46 
QuestionHow can I use winRing0.dll Pin
cedricvictor12-Mar-14 2:49
cedricvictor12-Mar-14 2:49 
AnswerRe: How can I use winRing0.dll Pin
Rage12-Mar-14 13:51
professionalRage12-Mar-14 13:51 
GeneralRe: How can I use winRing0.dll Pin
cedricvictor12-Mar-14 15:39
cedricvictor12-Mar-14 15:39 
GeneralRe: How can I use winRing0.dll Pin
Rage12-Mar-14 22:13
professionalRage12-Mar-14 22:13 
QuestionRelationship between Latency and Frame Rate Pin
Django_Untaken12-Mar-14 0:02
Django_Untaken12-Mar-14 0:02 
AnswerRe: Relationship between Latency and Frame Rate Pin
CPallini12-Mar-14 1:52
mveCPallini12-Mar-14 1:52 

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.