[RESOLVED] [C++] Win32 Program Wide Keyboard Shortcut - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23

Thread: [RESOLVED] [C++] Win32 Program Wide Keyboard Shortcut

  1. #16
    Join Date
    Feb 2011
    Posts
    20

    Re: [C++] Win32 Program Wide Keyboard Shortcut

    Thanks for all your help then.

  2. #17
    Join Date
    Feb 2011
    Posts
    20

    Talking Re: [C++] Win32 Program Wide Keyboard Shortcut

    I found it!

    With the help of http://www.cplusplus.com/forum/windows/36860 I changed my search from "shortcut" to "hotkey" I found something about keyboard accelerators (http://social.msdn.microsoft.com/For...2-617d69557057) which led me here: http://msdn.microsoft.com/en-us/libr...ting_acc_table

    After figuring out the way it worked, I have this (Spread across several files)
    Code:
    /*Keyboard Accelerators*/
    ShortAccel ACCELERATORS {
    	VK_ESCAPE, IDA_ESC, VIRTKEY
    }
    
    /*---*/
    //Load accelerators
    HACCEL haccel;
    haccel=LoadAccelerators(hinst, _T("ShortAccel"));
    if(!haccel) //Make sure it loaded
    	MessageBox(hwnd, _T("Unable to load accelerator table"), _T("Error"), MB_ICONERROR);
    
    
    while(GetMessage(&msg, NULL, 0, 0) > 0){
    	if(!TranslateAccelerator(FindWindow(_T("SettingsWindow"),_T("Settings")),haccel,&msg)){
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    }
    
    /*---*/
    #define IDA_ESC 4001
    
    /*---*/
    LRESULT fnSettingsProc_OnCommand(lpWndEventArgs wea){
    	switch(LOWORD(wea->wParam)){
    		case IDA_ESC: //Name given in accelerator table
    			DestroyWindow(wea->hwnd);
    			break;
    	}
    	return 0;
    }
    It works prefectly.

  3. #18
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,877

    Re: [C++] Win32 Program Wide Keyboard Shortcut

    You may be surprised, but code suggested by Mark hardly differs much from the last code you showed. What translate accelerator does is sending WM_COMMAND with corresponding id in case virtual key can be found in the table.

    Code:
    	while(GetMessage(&msg, NULL, 0, 0) > 0){
    		if (msg.message == WM_KEYDOWN &&
    			LOWORD(msg.wParam) == VK_ESCAPE)
    			SendMessage(FindWindow(_T("SettingsWindow"),_T("Settings")), WM_COMMAND, IDA_ESC, 0);
    		if(!IsDialogMessage(glbWin.vScrollContent, &msg)){
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    		}
    	}
    Best regards,
    Igor

  4. #19
    Join Date
    Feb 2011
    Posts
    20

    Re: [C++] Win32 Program Wide Keyboard Shortcut

    I see what you mean. I think I'll stick with the method I found though, as it is easier to expand.

  5. #20
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,877

    Re: [C++] Win32 Program Wide Keyboard Shortcut

    ...and you're 100% right. My point was just to make a bond between what you've found yourself and what you've been told of before.
    Best regards,
    Igor

  6. #21
    Join Date
    Feb 2011
    Posts
    20

    Re: [C++] Win32 Program Wide Keyboard Shortcut

    And that I do appreciate. I wouldn't have known that is how it worked if it wasn't for your telling me.

  7. #22
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: [RESOLVED] [C++] Win32 Program Wide Keyboard Shortcut

    Quote Originally Posted by Marc G View Post
    You can do something as follows for your message loop:
    Code:
    	while(GetMessage(&msg, NULL, 0, 0) > 0){
    		if (msg.message == WM_KEYDOWN &&
    			LOWORD(msg.wParam) == VK_ESCAPE)
    			MessageBox(hwnd, _T("Cancel"), _T(""), MB_OK);
    		if(!IsDialogMessage(glbWin.vScrollContent, &msg)){
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    		}
    	}
    of course, instead of displaying a messagebox, forward the escape command to the correct window.
    This can done in the window callback handler. ie: in WindowProc. And for removing that behavior instead of showing a msgbox just drop the value bu setting a NULL.

    Code:
    LRESULT CALLBACK wndProc (HWND hW, UINT msg, WPARAM wP, LPARAM lP)
    {
      switch (msg)
      {
          case WM_KEYDOWN:
          {
              if (wParam == VK_ESCAPE)
                 wParam = NULL;
          }
      }
      return 0;
    }
    just an alternative for the above code.
    Last edited by hypheni; March 3rd, 2011 at 11:40 PM.

  8. #23
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,877

    Re: [RESOLVED] [C++] Win32 Program Wide Keyboard Shortcut

    just an alternative for the above code.
    No, it's not. The code in the message loop works with focus set anywhere in the app. Unlike your "alternative" which requires to have a focus on the particular window.
    Best regards,
    Igor

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center