Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,

Can someone please tell me how to add a hotkey to your c++ api application.

Thanks.
Posted
Updated 14-Mar-11 23:48pm
v2
Comments
LloydA111 6-Mar-11 13:20pm    
To "your" application? I think you have posted this in the wrong place.
Sergey Alexandrovich Kryukov 6-Mar-11 13:53pm    
It must be grammar mistake.
However, there is no question here, it depends in UI library used.
--SA

This is a simple example which opens notepad.exe if you press ctrl+shift+o.

hotkey.cpp:
#include <windows.h>
#include <shellapi.h>
#include "resource.h"

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	switch(msg){
		case WM_COMMAND:
			switch(LOWORD(wParam)){
				case ID_OPENNOTEPAD:
				ShellExecute(0,0,"notepad.exe",0,0,SW_SHOW);
				break;
			}break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hwnd,msg,wParam,lParam);
	}return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
{
	WNDCLASS wc = {0};
	wc.lpfnWndProc = (WNDPROC)MainWndProc;
	wc.hInstance = GetModuleHandle(0);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszClassName = "hotkeyWndClass";
	wc.hCursor = LoadCursor(0,IDC_ARROW);
	RegisterClass(&wc);

	ShowWindow(CreateWindow("hotkeyWndClass","hotkey",WS_CAPTION|WS_SYSMENU,CW_USEDEFAULT,0,CW_USEDEFAULT,0,0,0,hInstance,0),SW_SHOW);

	MSG msg;
	HANDLE hAccelTable = LoadAccelerators(hInstance,MAKEINTRESOURCE(IDACCEL));
	while(GetMessage(&msg,0,0,0))
		if(!TranslateAccelerator(msg.hwnd,hAccelTable,&msg)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	return 0;
}


resource.h:
#define ID_OPENNOTEPAD	101
#define IDACCEL		10000


resource.rc:
#include <windows.h>
#include "resource.h"

IDACCEL ACCELERATORS
BEGIN
	"O",	ID_OPENNOTEPAD,VIRTKEY,CONTROL, SHIFT
END
 
Share this answer
 
v2
Comments
Dalek Dave 15-Mar-11 5:48am    
Good Answer.
Nuri Ismail 15-Mar-11 11:50am    
Excellent example. My 5
In your resource script (.rc file) you'll add your accelerator, something like this:

IDACCEL ACCELERATORS DISCARDABLE 
BEGIN
    "O",            ID_OPEN,                VIRTKEY, CONTROL, NOINVERT
    "S",            ID_SAVE,                VIRTKEY, CONTROL, NOINVERT
END


Then in your WinMain function where your GetMessage loop is, add the following:
MSG msg;
HACCEL hAccelTable = LoadAccelerators(hInstance,MAKEINTRESOURCE(IDACCEL));

while(GetMessage(&msg,0,0,0))
    if(!TranslateAccelerator(hwndMain,hAccelTable,&msg)){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }


Then in the function where you handle hwndMain's messages, you'll get your accelerator notification as WM_COMMAND message. LOWORD(wParam) will be your identifier (e.g. ID_OPEN, ID_SAVE).
 
Share this answer
 
Comments
[no name] 7-Mar-11 1:32am    
Why did my answer get downvoted?
Dark_Coder 14-Mar-11 15:51pm    
thanks Thaddeus Jones but can u pelase tell me what heater should i add and can you write a code when prees ctr+shift+o to open notepad.exe thanks man so mutch.
sorry i posted it twice.
[no name] 15-Mar-11 5:10am    
Added a reply to a new answer.
Dark_Coder 18-Mar-11 12:27pm    
thanks man 5++++++
Dark_Coder 14-Mar-11 15:50pm    
thanks Thaddeus Jones but can u pelase tell me what heater should i add and can you write a code when prees ctr+shift+o to open notepad.exe
thanks man so mutch.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900