Thread: Using Keyboard accelerators.......

  1. #1
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,403

    Using Keyboard accelerators.......

    I seem to be asking lots of questions recently, i am looking through Petzold and MSDN, so it's not like i'm totally slacking off

    Anyway, I'm trying to use accelerators for menu hotkeys but for some reason I can't get it to work.

    I added in:
    Code:
    HANDLE hAccel;
    at the beginning of WinMain(), and at the end I have:

    Code:
    hAccel = LoadAccelerators (hInst, TEXT("ACCEL"));
    
    	while( GetMessage( &lpMsg, NULL, 0, 0 ) )					/* begin the message loop */
    	{
    		if (!TranslateAccelerator (hWnd, (HACCEL)hAccel, &lpMsg))
    		{
    			TranslateMessage( &lpMsg );
    			DispatchMessage( &lpMsg );
    		}
    	}
    	return( lpMsg.wParam);
    I made the accerlerator in MSVC++ and named it ACCEL, (in the same way I named my menu), i have several lines in the accelerator table each with an ID from my menu resource, but when i run the program none of the keys in the accelerator table do anything. What am I doing wrong?
    Last edited by Clyde; 05-26-2002 at 04:52 PM.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Have you tested the return value from 'LoadAccelerators' to ensure it's returning a valid handle? Is your accelerator resource called "ACCEL" ie is a string or is it ACCEL with a #defined numeric value? If it's the latter then use MAKEINTRESOURCE(ACCEL) as the second parameter of LoadAccelerators.

    Other than that, what about your WM_COMMAND handler?

  3. #3
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,403
    I have tested the return value from LoadAccelerator (if hAccel MessageBox(blah, blah...)), and i notice that when i hit Ctrl+letter that is NOT listed in my accelerator i get a ping sound, whereas if i hit a Ctrl+letter that is listed in my accelerator nothing happens implying that it is regestering.

    The accelerator resource is called "ACCEL", in the same way that I called my menu resource "Menu" (and call it by settingwndclass.lpszMenuName = "Menu")

    I think i've mis-understood how to use accelerators, I have not alterered the lines of code in my WM_COMMAND, in those lines there are the operations used by the menu, (which work fine) all i've done is included the ID of the individual menu items in the accelerator table. Do I have to do something else in addition to that?

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    HIWORD(wParam) of the WM_COMMAND msg is 1 for an accelerator generated msg and 0 if sent by a menu item.

    If you are not testing HIWORD(wParam) then it should not be an issue.

  5. #5
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,403
    aha, that was it, i was testing HIWORD(wParam), thanks a lot Ken.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detecting keyboard and mouse in linux
    By sameer.nalwade in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 04:24 AM
  2. Keyboard port using other that a keyboard
    By antoinelac in forum C++ Programming
    Replies: 4
    Last Post: 06-12-2008, 02:46 PM
  3. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  4. help with keyboard accelerators
    By nnormal in forum Windows Programming
    Replies: 1
    Last Post: 08-06-2004, 11:22 AM
  5. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 03:34 PM